Guide ·

Razorpay Payment Gateway Integration in Node.js: Orders, Signatures and Webhooks

Integrate Razorpay with a Node.js backend the safe way: server-side orders, signature checks, webhooks, duplicate events, refunds and a go-live checklist.

By Ritesh Singh, full-stack developer

Adding a payment button takes an afternoon. Building a payment flow you can trust takes more care. The common failures are not exotic: the amount is set in the browser and can be changed, an order is marked paid because the frontend said so, the customer closes the tab before the success screen and the order stays unpaid, or a webhook arrives twice and the stock is reduced twice. This guide walks through a Razorpay integration with a Node.js backend in the order I build it.

1. Decide what your backend owns

One rule shapes everything else: the browser or app never decides what was paid. Your backend calculates the amount from its own database, creates the order with Razorpay, checks every result it receives, and is the only place that marks an order as paid. The frontend only opens the checkout and shows the outcome.

In your own database, keep an orders table with at least: your order ID, the amount in paise, the currency, a status (created, paid, failed, refunded), the Razorpay order ID and the Razorpay payment ID. Every later step reads and updates this row.

2. Keep the keys on the server

Razorpay gives you a key ID and a key secret, with separate pairs for test mode (rzp_test_…) and live mode (rzp_live_…). The key ID is public and goes to the checkout. The key secret signs and verifies everything, so it lives only in your server's environment variables, never in frontend code, a mobile app build or a Git repository. You will also set a separate webhook secret when you create the webhook in the dashboard.

3. Create the order on the server

When the customer clicks Pay, the frontend calls your own API, for example POST /api/checkout, with only your order ID or cart ID. The backend loads the cart, works out the total itself, and creates a Razorpay order with the official razorpay npm package: razorpay.orders.create({ amount, currency: "INR", receipt: yourOrderId }). The amount is an integer in the smallest unit, so ₹499 is 49900 paise; keep money as integers everywhere.

Save the returned order ID (it starts with order_) on your order row, then send the frontend only what the checkout needs: the key ID, the Razorpay order ID, the amount and the customer's name, email and phone for prefill.

4. Open the checkout

On the web, load Razorpay's checkout.js and open it with those values. In a Flutter or React Native app, use Razorpay's mobile SDK with the same order ID. When the payment succeeds, the checkout hands back three values: razorpay_payment_id, razorpay_order_id and razorpay_signature. Post them straight to your backend. Do not show "Payment successful" yet, and do not trust these values until the server has checked them.

5. Verify the payment signature

The signature proves the result came from Razorpay and belongs to your order. On the server, compute an HMAC-SHA256 of order_id + "|" + razorpay_payment_id using your key secret, as a hex string, and compare it with razorpay_signature. Use the order ID you saved in step 3, not the one the browser sent back, and compare with crypto.timingSafeEqual rather than ===.

If it matches, mark the order paid inside a database transaction, but only if it is still in created status. That one condition stops the same payment from being processed twice. If you want to be strict, also fetch the payment from the API and confirm its amount, currency and status (captured) match your order. Signing and hashing rules are where most payment and bank integrations break; I cover the same pitfalls for encrypted bank APIs in my bank API integration checklist.

6. Add webhooks for everything the browser misses

Step 5 only runs if the customer's browser or app stays open long enough to call your backend. Webhooks cover those cases: Razorpay calls your server directly when something happens. Subscribe at least to payment.captured, payment.failed and order.paid, plus refund events if you issue refunds.

Three details matter in the webhook handler:

  • Verify the raw body. Razorpay sends an X-Razorpay-Signature header: an HMAC-SHA256 of the exact request body using your webhook secret. In Express, give the webhook route express.raw({ type: "application/json" }) so you hash the original bytes. If express.json() parses the body first, re-serialised JSON will not match and every webhook will fail verification.
  • Expect duplicates. Webhooks can arrive more than once and in any order. Store each event ID (sent in the x-razorpay-event-id header) and skip events you have already handled.
  • Answer fast. Reply with a 2xx status as soon as the event is verified and stored, then send emails, invoices or stock updates from a background job. A slow or failing response makes Razorpay retry.

7. Handle captures, failures and refunds

Turn on automatic capture in the Razorpay dashboard unless you have a reason not to. A payment that is only authorised and never captured is not settled to you and is refunded to the customer automatically after a few days, which looks to them like a failed order.

When a payment fails, keep the order in created status so the customer can try again with the same order, and record the failure reason for support. For refunds, call the refund API from an admin action in your dashboard, store the refund ID, and update the order when the refund webhook confirms it. Never ask staff to refund from the Razorpay dashboard without updating your own records, or your reports and Razorpay's will drift apart.

8. Reconcile every day

Even with webhooks, add a small scheduled job. Once a day, or every hour for busy shops, find orders still in created status that are older than about 30 minutes, fetch their payments from Razorpay, and fix any that were actually paid. Keep a payment log with every API request, response and webhook, without card or bank details, so support can answer "I paid but my order shows pending" in seconds. If the same API also serves a mobile app, this logic lives in one place for both; I explain that structure in my guide on one API for a Flutter app and web dashboard.

Go-live checklist

  • Live key secret and webhook secret are in server environment variables only, and the frontend uses the live key ID.
  • The amount is calculated on the server in paise, never taken from the browser.
  • Every checkout result is verified with the payment signature before an order is marked paid.
  • The webhook URL is HTTPS, uses the raw body for signature checks, skips duplicate event IDs and replies quickly.
  • Paid status can only be set once per order, inside a transaction.
  • Automatic capture is on, or your code captures payments itself.
  • Refunds go through your admin panel and are recorded on the order.
  • A reconciliation job checks old pending orders, and payment logs exist for support.
  • You have run a full test-mode payment, a failed payment, a closed-tab payment and a refund.

Need a payment gateway added to your website or app? I build Node.js and PHP backends with payment, bank and SMS integrations for web dashboards and mobile apps. See my Node.js and PHP backend development service or fintech and bank API integration.

Related services

Hire me for this

Want this done for your product?

Start your project →