DEV Community

Gaurav Guha
Gaurav Guha

Posted on Originally published at thefrontkit.com

How to integrate Razorpay payments into a Next.js app

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_xxx in 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
Enter fullscreen mode Exit fullscreen mode

Step 2: Install the SDK

npm install razorpay
Enter fullscreen mode Exit fullscreen mode

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!,
});
Enter fullscreen mode Exit fullscreen mode

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,
  };
}
Enter fullscreen mode Exit fullscreen mode

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>
  );
}
Enter fullscreen mode Exit fullscreen mode

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>;
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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');
}
Enter fullscreen mode Exit fullscreen mode

In the Razorpay dashboard:

  1. Go to Settings > Webhooks > Add new
  2. Set the URL to https://your-app.com/api/webhooks/razorpay
  3. Subscribe to payment.captured and payment.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)