Razorpay payments
Community guide, checked against Razorpay's docs and official SDKs. When something here disagrees with https://razorpay.com/docs, the docs win.
The flow (never skip a step)
- Server creates an order. The amount comes from your database or price list, never from the browser. Amounts are integers in the smallest unit: ₹299 is
29900paise. - Browser opens Checkout with the public
key_idand theorder_id. Never sendkey_secretto the browser. - Server verifies the signature, then confirms the capture. The signature proves the response came from Razorpay; it does not prove the money is captured. Fetch the payment and mark the order paid only if it is
capturedfor that order and amount. If it is onlyauthorized, show "processing". - Webhook confirms the payment (
order.paidorpayment.captured). Mark the order paid on whichever of steps 3 or 4 arrives first, with a conditional update, so the second one does nothing.
Payments made without an order_id cannot be captured and are refunded automatically. Always create an order first. Subscriptions, Payment Links and mobile apps follow the same shape.
Before writing code
Find the stack in use (package.json, requirements.txt, composer.json, pom.xml, go.mod, Gemfile, .csproj, Cargo.toml, pubspec.yaml), check what already exists, then read only the reference you need:
| Task | Read |
|---|---|
| Next.js App Router setup (files to create) | references/nextjs-app-router.md |
| Express, Python, PHP, Java, Go, Ruby, .NET or Rust | references/stacks.md |
| Orders, Checkout options, payment states | references/orders-and-checkout.md |
| Signature checks and webhooks | references/verification-and-webhooks.md |
| Capture settings and refunds | references/capture-and-refunds.md |
| Recurring billing | references/subscriptions.md |
| Invoices or payment requests without a checkout page | references/payment-links.md |
| UPI QR codes (counter or per order) | references/qr-codes.md |
| Saved cards (tokens) or bank transfers to a customer identifier | references/saved-cards-and-smart-collect.md |
| Marketplace splits to sellers (Route) | references/route.md |
| Sending money out: vendors, salaries, cashback (RazorpayX) | references/payouts.md |
| Android, iOS, React Native or Flutter apps | references/mobile-apps.md |
| Settlements, fees and daily reconciliation | references/settlements-and-reconciliation.md |
| API errors, retries, late payments, missed webhooks, disputes | references/errors-and-recovery.md |
| Connecting the official Razorpay MCP server | references/mcp-server.md |
| Test mode, local webhooks, go-live checklist | references/testing-and-go-live.md |
| Reviewing an existing integration | The review checklist below |
Environment variables
RAZORPAY_KEY_ID=rzp_test_xxxxxxxxxxxxxx # public, also exposed to the browser
RAZORPAY_KEY_SECRET=xxxxxxxxxxxxxxxxxxxxxxxx # server only
RAZORPAY_WEBHOOK_SECRET=xxxxxxxxxxxxxxxx # server only, set when creating the webhook
NEXT_PUBLIC_RAZORPAY_KEY_ID=rzp_test_xxxxxxxxxxxxxx # Next.js only
rzp_test_keys are test mode andrzp_live_keys take real money. Use one mode end to end: create the order and open Checkout with keys from the same mode.- The webhook secret is a separate value you choose in the Dashboard. It is not the key secret.
- Never commit real keys. Keep them in env files that git ignores and in the hosting provider's env settings.
Rules
- Amounts in paise, as integers. Convert rupees to paise once, on the server, rounding to an integer. Never multiply twice.
- Set the price on the server. Look up the price from the product or plan id the client sends. Ignore any amount the client sends.
- Verify the Checkout signature on the server, then require a captured payment: HMAC SHA256 of
order_id + "|" + razorpay_payment_idwithkey_secret, using theorder_idyou stored, not the one the browser sends back. A failed check means reject. A passed check is not enough to deliver: fetch the payment and requirestatus: "captured", a matchingorder_idand amount. - Verify webhooks against the raw body. Read the body as raw text before any JSON parsing and HMAC it with the webhook secret. Never verify re-serialised JSON: it changes the bytes and the check fails. references/stacks.md shows how to get the raw body in each framework.
- Compare signatures in constant time (for example
crypto.timingSafeEqualafter checking lengths). The Node and .NET SDK helpers use plain equality. - Webhooks arrive at least once and out of order. Record the
x-razorpay-event-idin the same database transaction as the event's effects, so a crash rolls both back and the retry runs again. Every status change is conditional on the current state: onlycreatedorfailedcan becomepaid. A refunded order never becomes paid again. - Return 2xx fast. Any non-2xx response counts as a failure. Razorpay retries with backoff for 24 hours, then disables the webhook. Queue slow work (emails, access) in the same transaction as an outbox job and run it in a worker.
- Capture every payment. Authorized payments that are never captured are refunded automatically (default timeout 3 days). Turn on auto-capture in the Dashboard or capture through the API.
- Keep the secret server side. Only
key_idandorder_idgo to the browser. Code that useskey_secretmust never ship in a client bundle. - Store every id. Save
order_id,payment_id, status and amount in your database so you can reconcile, refund and answer support questions. - Confirm before moving money. Before running anything that captures, refunds, creates a payout, transfers or reverses funds, requests an instant settlement, cancels a subscription, accepts or contests a dispute or sends a payment request on a real account (through code, a script or the MCP server), show the user the action, the mode, the ids and the amount in rupees, then wait for an explicit yes.
- Investigate before retrying. After a timeout or a 5xx, fetch the order, payment or refund first and act only on what is missing. A
payment.failedis not always final: banks can authorize a payment up to 3 days later. - Protect the endpoints. Order and verify routes need the signed in user (an order belongs to one user), input validation and rate limiting. The samples in this skill show the payment logic; wire them into your own auth, database and queue before production.
Review checklist (existing integrations)
Flag each item that fails, with file and line:
- Order created on the server with an amount the server computed
- Amount is an integer in paise; no float maths and no double conversion
-
key_secretandRAZORPAY_WEBHOOK_SECRETnever reach client code or logs - Checkout signature verified on the server, using the stored
order_id - Order marked paid only after the payment is fetched and found
capturedfor that order and amount - Webhook verified against the raw body with the webhook secret, in constant time
- Event id stored in the same transaction as the event's effects; a processing error returns 5xx
- Status updates are conditional (
WHERE status IN (...)); a refunded order cannot become paid - Order and verify routes check the signed in user, validate input and are rate limited
- Fulfilment runs once, whether the verify call, the webhook or a reconciliation job gets there first
- Webhook returns 2xx quickly and does not depend on event order
- Capture is automatic or the code captures within the timeout
- Test and live keys come from env vars, not code
- Payment failures are shown to the user with a retry option; a failed payment does not close the order for good
- Refund and capture actions cannot run twice from a double click or a retry
- A daily job reconciles settled payments against the database and flags gaps
- Payouts send a stored
X-Payout-Idempotencykey and reuse it on retries - Mobile apps hold only
key_id; the server verifies every payment
Beyond this skill
For products not described here (for example Magic Checkout, Optimizer or POS), follow https://razorpay.com/docs directly. The signature, webhook and confirm rules above still apply.