You're building a Next.js app targeting Indian customers and need a payment gateway that supports UPI, net banking, and other local methods. Razorpay is the go-to choice for Indian SaaS and e-commerce. Here's how to integrate it.
Why Razorpay for India
Indian customers expect specific payment methods:
- UPI (Google Pay, PhonePe, Paytm): the dominant retail method
- Net banking: still 15-20% of transactions
- Cards: domestic Rupay and international Visa/Mastercard
- EMI: for higher-ticket purchases
- Wallets: Paytm, Mobikwik, etc.
Razorpay supports all these methods, while Stripe in India only supports cards and a limited set of others. For a ₹999/month SaaS, this can mean a 10-15% conversion difference.
Step 1: Set Up Razorpay Account
Sign up at razorpay.com. Grab these keys from the dashboard:
-
RAZORPAY_KEY_ID(e.g.,rzp_test_xxxin test mode) -
RAZORPAY_KEY_SECRET(server-side only)
Add them to .env.local:
NEXT_PUBLIC_RAZORPAY_KEY_ID=rzp_test_xxx
RAZORPAY_KEY_SECRET=xxx
RAZORPAY_WEBHOOK_SECRET=xxx
NEXT_PUBLIC_SITE_URL=http://localhost:3000
Step 2: Install the SDK
npm install razorpay
Create a client wrapper at lib/razorpay.ts:
import Razorpay from 'razorpay';
export const razorpay = new Razorpay({
key_id: process.env.NEXT_PUBLIC_RAZORPAY_KEY_ID!,
key_secret: process.env.RAZORPAY_KEY_SECRET!,
});
Step 3: Create the Order (Server-Side)
Razorpay's flow requires creating an order on the server, then opening the checkout modal on the client.
'use server';
import { razorpay } from '@/lib/razorpay';
export async function createRazorpayOrder(amountInPaise: number) {
const order = await razorpay.orders.create({
amount: amountInPaise, // ₹999 = 99900
currency: 'INR',
receipt: `order_${Date.now()}`,
});
return {
orderId: order.id,
keyId: process.env.NEXT_PUBLIC_RAZORPAY_KEY_ID,
};
}
Step 4: Trigger the Checkout Modal (Client-Side)
Load the Razorpay script in app/layout.tsx:
import Script from 'next/script';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<Script src="https://checkout.razorpay.com/v1/checkout.js" />
{children}
</body>
</html>
);
}
Open the modal from a button:
'use client';
export function PayButton({ amountInPaise }: { amountInPaise: number }) {
async function handlePay() {
const order = await createRazorpayOrder(amountInPaise);
const rzp = new (window as any).Razorpay({
key: order.keyId,
amount: amountInPaise,
currency: 'INR',
order_id: order.orderId,
handler: async (response: any) => {
await verifyPayment(response);
},
});
rzp.open();
}
return <button onClick={handlePay}>Pay ₹{amountInPaise / 100}</button>;
}
Step 5: Verify the Payment
Verify the signature on the server before granting access:
'use server';
import crypto from 'crypto';
export async function verifyPayment(response: any) {
const body = `${response.razorpay_order_id}|${response.razorpay_payment_id}`;
const expectedSignature = crypto
.createHmac('sha256', process.env.RAZORPAY_KEY_SECRET!)
.update(body)
.digest('hex');
if (expectedSignature !== response.razorpay_signature) {
throw new Error('Invalid signature');
}
// Payment verified, grant access
}
Step 6: Set Up Webhooks
Create app/api/webhooks/razorpay/route.ts:
import crypto from 'crypto';
export async function POST(req: Request) {
const body = await req.text();
const signature = req.headers.get('x-razorpay-signature');
const expectedSignature = crypto
.createHmac('sha256', process.env.RAZORPAY_WEBHOOK_SECRET!)
.update(body)
.digest('hex');
if (expectedSignature !== signature) {
return new Response('Invalid signature', { status: 400 });
}
// Process webhook events
return new Response('OK');
}
In the Razorpay dashboard:
- Go to Settings > Webhooks > Add new
- Set the URL to
https://your-app.com/api/webhooks/razorpay - Subscribe to
payment.capturedandpayment.failed
Common Gotchas
- Signature verification: Always verify signatures on both client and server
- Amount units: Razorpay uses paise (1 INR = 100 paise)
- Test vs live keys: Don't mix them
- Webhooks: Required for cases where the client-side handler doesn't fire
- KYC: Live mode requires business verification
Remember, Razorpay's subscription flow requires eMandate setup, which has different flows for cards vs UPI. Test thoroughly.
This is a shorter version of a guide I wrote at Frontkit Studio. The full post has the complete walkthrough.
Top comments (0)