The 3-Step Payment Lifecycle
A payment gateway integration involves three distinct steps:
order_id.order_id.Node.js Webhook Verification Implementation:
import crypto from "crypto";
export const handleRazorpayWebhook = async (req, res) => {
const secret = process.env.RAZORPAY_WEBHOOK_SECRET;
const signature = req.headers["x-razorpay-signature"];
// Cryptographically verify payload authenticity
const expectedSignature = crypto
.createHmac("sha256", secret)
.update(JSON.stringify(req.body))
.digest("hex");
if (expectedSignature !== signature) {
return res.status(400).json({ error: "Invalid cryptographic signature" });
}
const event = req.body.event;
if (event === "payment.captured") {
const payment = req.body.payload.payment.entity;
// Mark order as paid in database
await updateOrderStatus(payment.order_id, "PAID", payment.id);
}
res.status(200).json({ status: "ok" });
};Always respond with HTTP 200 immediately to prevent the gateway from retrying the webhook unnecessarily.