Headers Webhook gửi về Server bạn
| Header | Mô tả |
|---|---|
| X-BankGateway-Event | PAYMENT_PAID (thanh toán thật) hoặc PAYMENT_TEST (bấm Test 1-Click). |
| X-BankGateway-Timestamp | Unix timestamp (ms) lúc gửi Webhook. Nên từ chối nếu lệch quá 5 phút. |
| X-BankGateway-Signature | HMAC-SHA256 hex của chuỗi "${timestamp}.${rawBody}" ký bằng Secret Key của bạn. |
Code mẫu xác thực Webhook an toàn (Node.js / PHP / Python)
import crypto from "node:crypto";
export function verifyBankGatewayWebhook(req, secretKey) {
const ts = req.headers["x-bankgateway-timestamp"];
const sig = req.headers["x-bankgateway-signature"];
const rawBody = typeof req.body === "string" ? req.body : JSON.stringify(req.body);
if (!ts || !sig || Math.abs(Date.now() - Number(ts)) > 5 * 60 * 1000) {
return false; // Chặn Replay Attack quá 5 phút
}
const expected = crypto
.createHmac("sha256", secretKey)
.update(`${ts}.${rawBody}`)
.digest("hex");
return crypto.timingSafeEqual(Buffer.from(sig), Buffer.from(expected));
}