Payments with Razorpay

Accept UPI, cards and netbanking with Razorpay: create orders on the server, verify signatures and handle webhooks reliably.

Advanced · about 5 hours · difficulty 4/5

Learn first: Error Handling, Validation and API Hardening, Web Security Essentials

Payments turn a project into a product. In India, Razorpay is a common gateway that supports UPI, cards, netbanking and wallets. Stripe and Cashfree follow the same ideas.

The flow

  1. Your server creates an order with the amount in paise and a receipt id, and saves it as PENDING.
  2. The client opens Razorpay Checkout with the order id; the user pays by UPI or card.
  3. The client receives a payment id and signature; your server verifies the signature with your secret before trusting it.
  4. A webhook from Razorpay confirms the final status (captured or failed), and you update the order.

Rules that prevent real losses

  • Never trust an amount sent from the client; compute it on the server.
  • Verify webhook signatures and make handlers idempotent, since webhooks can arrive more than once or out of order.
  • Treat the webhook as the source of truth; the user may close the tab before redirecting.
  • Store amounts as integers in paise.
  • Use test mode keys during development and keep live keys in production secrets only.

Pitfalls

  • Marking an order paid based on the client callback alone.
  • Not handling pending UPI payments that settle minutes later.
  • Logging card or full payment details.

You are ready to move on when users can pay a mock Rs 99 premium fee in test mode, the order is marked paid only after signature verification, and duplicate webhooks do not double-credit.

Key concepts

  • Orders and amounts in paise
  • Checkout integration
  • Signature verification
  • Webhooks as source of truth
  • Idempotent webhook handlers
  • Test vs live mode
  • Stripe or Cashfree

Mini project

Add a Rs 99 'premium profile' purchase to your portal using Razorpay test mode: server-created orders, client checkout, signature verification and an idempotent webhook handler that updates the order status.

Check yourself

Why must your server verify the payment signature and rely on webhooks instead of trusting the success callback in the browser?

Free resources