Architecture · Guides · SaaS
How to verify Razorpay webhook signatures, and why you must.
Why payment webhooks need signature checks, how Razorpay's HMAC SHA-256 verification works, PHP and Node examples, and mistakes like parsing before verifying.
Key takeaways
- A webhook is just an HTTP request. Anyone who finds your URL can send a fake "payment captured" message.
- Razorpay signs each webhook with HMAC SHA-256 using your webhook secret and sends the result in the
X-Razorpay-Signatureheader. - Compute the HMAC over the raw request body, then compare in constant time.
- Verify first, parse second, and make your handler idempotent.
Why verification is not optional
When a customer pays through a Razorpay payment link or checkout, your server learns about it through a webhook: Razorpay sends an HTTP POST to a URL you configured. Your code then marks the booking as paid.
The problem is that the URL is only an address. If someone discovers it, they can send a request that looks like a successful payment. Without verification, your system might confirm a booking nobody paid for.
How the signature works
When you create a webhook in the Razorpay dashboard, you set a webhook secret. For each event, Razorpay computes:
HMAC_SHA256(secret, raw_request_body)
as a hex string and sends it in the X-Razorpay-Signature header. Only someone who knows the secret can produce a matching value. Your server repeats the calculation and compares.
(Checking the signature on a payment *checkout* callback in your front end is a different flow, using the order ID, payment ID and your API key secret. This article is about webhooks.)
Example in PHP
$payload = file_get_contents('php://input'); // raw body
$signature = $_SERVER['HTTP_X_RAZORPAY_SIGNATURE'] ?? '';
$expected = hash_hmac('sha256', $payload, $webhookSecret);
if (!hash_equals($expected, $signature)) {
http_response_code(400);
exit('Invalid signature');
}
$event = json_decode($payload, true);
Example in Node.js
const crypto = require('crypto');
// use a raw-body parser for this route
const expected = crypto
.createHmac('sha256', process.env.WEBHOOK_SECRET)
.update(req.rawBody)
.digest('hex');
const ok = crypto.timingSafeEqual(
Buffer.from(expected),
Buffer.from(req.headers['x-razorpay-signature'] || '')
);
Make sure the two buffers have equal length before calling timingSafeEqual, because it throws otherwise.
The mistakes that break verification
- Verifying a re-serialised body. If your framework parses the JSON and you stringify it again, whitespace and key order can change and the hash will not match. Use the raw bytes as received.
- Using the wrong secret. The webhook secret is not your API key secret. Keep them separate.
- Comparing with
==. Use a constant-time comparison such ashash_equalsto avoid timing leaks. - Parsing before verifying. Reject bad signatures before doing anything with the data.
- Logging the secret or full payloads containing personal data.
After verifying: be idempotent
Providers may send the same event more than once, for retries or network reasons. Your handler should be safe to run twice: look up the booking by the payment link or order ID, and do nothing if it is already confirmed. Store the event ID if one is provided.
Return a 2xx response quickly. Do slow work, such as sending messages, in a queue, so the provider does not time out and retry.
How Zentry uses it
In Zentry, the WhatsApp bot creates a pending booking and sends a Razorpay payment link. Razorpay then tells Zentry the link was paid, Zentry checks the signature, and only then confirms the booking and sends the confirmation in the chat. The same approach applies to subscription webhooks for plan activation, charges, failures and cancellations. See WhatsApp booking automation for small businesses.
Testing
- Send a deliberately wrong signature and confirm a 400.
- Send the same valid event twice and confirm there is one booking.
- Test with Razorpay's test mode before going live.
Frequently asked questions
What if I verify the signature but the amount is wrong?
Also check that the amount and currency match what you expected for that booking. A valid signature proves the sender, not that the data fits your order.
Can I skip verification in development?
Keep it on everywhere. Use test mode secrets in development instead of disabling the check.
Do all payment providers work this way?
Most use a similar HMAC scheme with a header, but details differ. Read each provider's current documentation.
Zentry confirms bookings only from signature-checked Razorpay webhooks.
See Zentry