import crypto from "node:crypto";
function verifyVestrapaySignature(rawBody, signatureHeader, secret) {
const expected =
"sha256=" + crypto.createHmac("sha256", secret).update(rawBody).digest("hex");
const a = Buffer.from(signatureHeader ?? "", "utf8");
const b = Buffer.from(expected, "utf8");
return a.length === b.length && crypto.timingSafeEqual(a, b);
}
// Express: app.use("/webhooks/vestrapay", express.raw({ type: "application/json" }))
app.post("/webhooks/vestrapay", (req, res) => {
const rawBody = req.body; // Buffer
if (!verifyVestrapaySignature(rawBody, req.get("X-VestraPay-Signature"), process.env.VESTRAPAY_WEBHOOK_SECRET)) {
return res.status(401).end();
}
const payload = JSON.parse(rawBody.toString("utf8"));
res.status(200).end();
// enqueue: verify payload.data.reference, then fulfill once
});