Razorpay Subscription Integration for SaaS Billing
How to integrate Razorpay Subscriptions into a SaaS: plan setup, checkout, signature and webhook verification, a single subscription state, and running Apple in-app purchase alongside it.
Billing is the part of a SaaS that nobody wants to rebuild twice. If a customer pays and the app still shows them locked out, you get a support ticket at best and a lost customer at worst. A careful Razorpay subscription integration avoids that by treating payments as events, verifying everything and keeping one source of truth for what each customer has paid for.
I integrated Razorpay subscriptions on the web for Client Mitra, a multi-tenant CRM for photography studios, alongside Apple in-app purchase on iOS. This post covers the architecture, the webhook handling and the iOS wrinkle most Indian SaaS founders hit late.
TL;DR
Create plans in Razorpay, start subscriptions from your server, and open checkout on the client. Verify the checkout signature, but treat webhooks as the source of truth. Store one internal subscription record per organization with a status, plan and period end, updated idempotently by every provider. If you ship on iOS, add Apple in-app purchase with server-side receipt verification.
key takeaways
- Webhooks, not the checkout success callback, should be the source of truth for whether a subscription is active.
- Keep one internal subscription record per organization that every payment provider updates, so the rest of the app never asks which provider was used.
- Verify every signature and make webhook handlers idempotent, because Razorpay retries and events can arrive out of order.
- If you ship an iOS app, plan Apple in-app purchase from the start, since App Store review will require it for digital subscriptions.
How Razorpay subscriptions fit together
Razorpay's subscription model has three objects worth understanding before you write code. The Razorpay Subscriptions documentation has the full reference.
| Object | What it is | Where you create it |
|---|---|---|
| Plan | Price, currency, billing interval (monthly, yearly) | Once, in the dashboard or via API |
| Subscription | A customer's instance of a plan with total cycles and start date | Your server, when the user clicks subscribe |
| Payment | Each charge against the subscription | Razorpay, on each billing cycle |
The flow in a typical SaaS looks like this:
- User picks a plan on your pricing page.
- Your server creates a Razorpay subscription for that plan and returns its ID.
- The client opens Razorpay Checkout with the subscription ID.
- The user authorizes the mandate or card payment.
- Checkout returns
razorpay_payment_id,razorpay_subscription_idandrazorpay_signature. - Your server verifies the signature and marks the subscription as pending activation.
- Razorpay sends webhooks as the subscription activates and renews, and your server updates status.
Store the Razorpay subscription ID against the organization the moment you create it, before checkout opens. If the user closes the tab mid-payment, you still know which organization the later webhook belongs to.
Webhooks are the source of truth
The checkout success handler runs in the user's browser. Browsers close, networks drop, and users hit back. If your app only activates plans on that callback, some paying customers will stay locked out.
Treat the callback as a fast path for good UX ("Payment received, activating...") and let webhooks make the real decision.
Verify the signature on the raw body
Razorpay signs webhook payloads with your webhook secret in the X-Razorpay-Signature header. Compute an HMAC SHA256 of the raw request body and compare. The key detail: use the raw body, not a re-serialized JSON object. In Express, that means capturing the raw buffer for the webhook route before the JSON parser touches it.
Make handlers idempotent
Razorpay retries webhooks that do not get a 2xx response, and events can arrive out of order. Store each processed event ID, and skip duplicates. When applying a status change, compare the event's timestamp or the subscription's current period against what you already have, so a late "charged" event never overrides a newer "cancelled".
Events to handle
Map provider events to your own statuses:
subscription.activatedandsubscription.charged: set status active, updatecurrentPeriodEnd.subscription.pending: payment retry in progress; keep access but show a banner.subscription.halted: retries exhausted; restrict paid features.subscription.cancelledandsubscription.completed: set the end date and downgrade at period end.
One internal subscription record
The rest of your app should never call Razorpay to ask "is this customer paid?" It should read your own record:
{
organizationId,
plan: "pro",
status: "active", // trialing | active | past_due | cancelled | expired
provider: "razorpay", // or "apple"
providerSubscriptionId,
currentPeriodEnd,
cancelAtPeriodEnd: false
}
Feature checks, seat limits and the billing page all read this. When you add a second provider, only the code that writes this record changes. A nightly job that compares records against the provider is a cheap safety net for missed webhooks.
The iOS problem: Apple in-app purchase
Here is the part many teams discover at App Store review. If your iOS app sells digital subscriptions, Apple generally requires in-app purchase for that purchase inside the app. You cannot simply open Razorpay checkout in the iOS build.
The practical setup is Razorpay on web and Android, Apple IAP on iOS, both writing the same internal subscription record. On the server, verify Apple transactions and process App Store Server Notifications rather than trusting the device. The App Store Server API documentation covers transaction verification.
How I built this in practice
In the Client Mitra case study I describe the full product I built solo. Subscriptions run through Razorpay on the web and Apple in-app purchase on iOS, with server-side receipt verification using @apple/app-store-server-library. App Store review required native Sign in with Apple, an IAP paywall and server verification before the iOS app was approved.
The native Android and iOS apps are a single Flutter wrapper around the web app using flutter_inappwebview, which adds native sign-in, IAP, FCM push, local notifications and Crashlytics. The backend is Node/Express with MongoDB and Mongoose, JWT auth and tenant isolation in middleware, so subscription state is scoped per studio and roles control who can see billing. Earlier, on the X4ET B2B marketplace I built solo, Razorpay handled payments too, so the webhook and signature patterns here come from more than one production system.
Common mistakes
- Activating plans only in the browser success callback instead of on verified webhooks.
- Verifying webhook signatures against parsed and re-serialized JSON rather than the raw body.
- Processing the same webhook twice because event IDs are not stored.
- Letting out-of-order events downgrade a customer who has already renewed.
- Scattering provider-specific checks through the app instead of one internal subscription record.
- Planning iOS billing after the app is built, then failing App Store review.
keep reading
FAQ
How long does a Razorpay subscription integration take?+
A clean integration with plans, checkout, webhooks, upgrade and cancel flows typically takes one to two weeks of development. Adding a second provider like Apple in-app purchase adds roughly another one to two weeks.
Which Razorpay webhook events should a SaaS handle?+
At minimum handle subscription activated, charged, pending, halted, cancelled and completed. Map each event to a status change on your internal subscription record and ignore events you have already processed.
Can I use Razorpay for subscriptions inside an iOS app?+
For digital subscriptions bought inside an iOS app, Apple generally requires in-app purchase. A common setup is Razorpay on the web and Android, Apple IAP on iOS, both updating one internal subscription record.
How do I verify a Razorpay payment on the server?+
Compute an HMAC SHA256 of the payment and subscription IDs using your key secret and compare it with the signature returned by checkout. For webhooks, verify the X-Razorpay-Signature header against the raw request body using your webhook secret.
Devang Gandhi
AI & full stack developer at Passdn, building CRMs, SaaS, AI agents and Flutter apps for clients in the US, UK, Australia, UAE and India. More about me