Razorpay payment integration patterns for Next.js + Supabase. Use when building checkout, subscriptions, refunds, or any payment flow.
import Razorpay from "razorpay";
const razorpay = new Razorpay({
key_id: process.env.RAZORPAY_KEY_ID!,
key_secret: process.env.RAZORPAY_KEY_SECRET!,
});
const order = await razorpay.orders.create({
amount: priceInPaise, // Rs 499 = 49900 paise
currency: "INR",
receipt: `receipt_${orderId}`,
notes: { user_id: userId, plan: "pro" },
});
const options = {
key: process.env.NEXT_PUBLIC_RAZORPAY_KEY_ID,
amount: order.amount,
currency: order.currency,
order_id: order.id,
name: "Your App",
prefill: { email, contact: phone },
handler: (response) => verifyPayment(response),
};
new window.Razorpay(options).open();
import crypto from "crypto";
function verifySignature(orderId: string, paymentId: string, signature: string): boolean {
const expected = crypto
.createHmac("sha256", process.env.RAZORPAY_KEY_SECRET!)
.update(orderId + "|" + paymentId)
.digest("hex");
return expected === signature;
}
// POST /api/v1/webhooks/razorpay
// 1. Verify x-razorpay-signature header
// 2. Parse event type
// Key events: payment.captured, payment.failed, subscription.charged,
// subscription.cancelled, refund.processed
// 3. Update database, notify user