Appearance
Tài liệu kỹ thuật Webhook
Method
Method: POST
Header
json
{
'Content-Type: application/json',
'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1c2VyIjoiMDkwMjUwNjA5OSIsImltZWkiOiI0MDE0NC1iZmNiNzJjMGIyMjczNjZiZy'
}
Params POST
json
{
"transactions": [
{
"id": "transaction1",
"gateway": "bank1",
"transactionDate": "2024-07-20",
"transactionNumber": "txn001",
"accountNumber": "1234567890",
"content": "Payment for invoice #123",
"transferType": "credit",
"transferAmount": 100.00
}
]
}
Response
json
{
"success": true,
}
Code mẫu
php
<?php
// Đặt nội dung JSON từ webhook vào biến
$requestBody = file_get_contents('php://input');
// Chuyển đổi nội dung JSON thành mảng
$data = json_decode($requestBody, true);
if (json_last_error() === JSON_ERROR_NONE) {
// Kiểm tra xem có thuộc tính 'transactions' không
if (isset($data['transactions']) && is_array($data['transactions'])) {
// Xử lý từng giao dịch
foreach ($data['transactions'] as $transaction) {
// Xử lý giao dịch ở đây (ví dụ: lưu vào cơ sở dữ liệu)
// $transaction['id']
// $transaction['gateway']
// $transaction['transactionDate']
// $transaction['transactionNumber']
// $transaction['accountNumber']
// $transaction['content']
// $transaction['transferType']
// $transaction['transferAmount']
}
// Phản hồi thành công
$response = [
'success' => true,
'message' => 'Transactions processed successfully'
];
http_response_code(200);
} else {
// Phản hồi lỗi nếu không có 'transactions'
$response = [
'success' => false,
'message' => 'Invalid payload, transactions not found'
];
http_response_code(400);
}
} else {
// Phản hồi lỗi nếu JSON không hợp lệ
$response = [
'success' => false,
'message' => 'Invalid JSON'
];
http_response_code(400);
}
// Thiết lập header Content-Type là application/json
header('Content-Type: application/json');
// Xuất phản hồi JSON
echo json_encode($response);
js
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const PORT = process.env.PORT || 3000;
// Sử dụng body-parser để phân tích nội dung JSON
app.use(bodyParser.json());
app.post('/webhook', (req, res) => {
const data = req.body;
// Kiểm tra xem có thuộc tính 'transactions' không
if (data && Array.isArray(data.transactions)) {
data.transactions.forEach((transaction) => {
// Xử lý giao dịch ở đây (ví dụ: lưu vào cơ sở dữ liệu)
// transaction.id
// transaction.gateway
// transaction.transactionDate
// transaction.transactionNumber
// transaction.accountNumber
// transaction.content
// transaction.transferType
// transaction.transferAmount
});
// Phản hồi thành công
res.status(200).json({
success: true,
message: 'Transactions processed successfully',
});
} else {
// Phản hồi lỗi nếu không có 'transactions'
res.status(400).json({
success: false,
message: 'Invalid payload, transactions not found',
});
}
});
// Khởi động server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
python
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/webhook', methods=['POST'])
def webhook():
data = request.get_json()
# Kiểm tra xem có thuộc tính 'transactions' không
if data and 'transactions' in data and isinstance(data['transactions'], list):
transactions = data['transactions']
for transaction in transactions:
# Xử lý giao dịch ở đây (ví dụ: lưu vào cơ sở dữ liệu)
# transaction['id']
# transaction['gateway']
# transaction['transactionDate']
# transaction['transactionNumber']
# transaction['accountNumber']
# transaction['content']
# transaction['transferType']
# transaction['transferAmount']
pass
# Phản hồi thành công
return jsonify({
'success': True,
'message': 'Transactions processed successfully'
}), 200
else:
# Phản hồi lỗi nếu không có 'transactions'
return jsonify({
'success': False,
'message': 'Invalid payload, transactions not found'
}), 400
if __name__ == '__main__':
app.run(port=5000)
ruby
require 'sinatra'
require 'json'
post '/webhook' do
request.body.rewind
data = JSON.parse(request.body.read)
# Kiểm tra xem có thuộc tính 'transactions' không
if data && data['transactions'].is_a?(Array)
data['transactions'].each do |transaction|
# Xử lý giao dịch ở đây (ví dụ: lưu vào cơ sở dữ liệu)
# transaction['id']
# transaction['gateway']
# transaction['transactionDate']
# transaction['transactionNumber']
# transaction['accountNumber']
# transaction['content']
# transaction['transferType']
# transaction['transferAmount']
end
# Phản hồi thành công
content_type :json
status 200
{ success: true, message: 'Transactions processed successfully' }.to_json
else
# Phản hồi lỗi nếu không có 'transactions'
content_type :json
status 400
{ success: false, message: 'Invalid payload, transactions not found' }.to_json
end
end
# Khởi động server
set :port, 4567
.htaccess Rewrite URL (Nếu lỗi Header)
.htaccess
# Thêm quy tắc cho header Authorization vào file .htaccess
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]