Razorpay Gateway (Express.js)
Developer-ready Express.js middleware for integrating Razorpay payments with modular REST endpoints, secure signature verification, and webhook automation. Designed for scalability and easy extension.
Overview
A modular Razorpay payment gateway built with Express.js, enabling seamless order creation, payment verification, and webhook handling for real-time payment updates. Includes strong HMAC SHA256 verification for security and a flexible architecture for extending with subscriptions, refunds, or custom workflows.
Installation
What This Does
Installs a preconfigured Razorpay payment integration with clean route separation, secure webhook verification, and reusable payment services.
Files & Folders Created
| File / Path | Description |
|---|---|
| /src/config/razorpay.js | Initializes Razorpay SDK and loads credentials from .env |
| /src/services/paymentService.js | Core logic for order creation, verification, and webhooks |
| /src/routes/payment.js | REST routes for order creation, verification, and fetching details |
| /server.js.example | Express server integrating payment routes |
| /.env.example | Example config for Razorpay keys and webhook secret |
Files to be modified
| File / Path | Description |
|---|---|
| server.js | Adds /api/payment route and webhook middleware |
| .env | Adds Razorpay credentials (RAZORPAY_KEY_ID, RAZORPAY_KEY_SECRET, WEBHOOK_SECRET) |
Configuration
# Razorpay Configuration RAZORPAY_KEY_ID=your_razorpay_key_id RAZORPAY_KEY_SECRET=your_razorpay_key_secret PORT=8000 WEBHOOK_SECRET=your_webhook_secret
Frontend Integration
Integrate directly with your frontend to initiate secure payment orders, verify signatures after checkout, and track payment statuses in real-time. Perfect for integrating with React, Next.js, or Vue apps.
Creates a new Razorpay order. Accepts amount, currency, metadata, and optional custom options.
Verifies payment using the signature returned by Razorpay after successful payment.
Fetches full Razorpay order details using the order ID.
Receives and verifies Razorpay webhooks for automatic payment events (e.g., payment captured, refund, etc.).
Example
1// Example frontend usage (Next.js)
2'use client';
3import { useState } from 'react';
4
5export default function RazorpayCheckout() {
6 const [loading, setLoading] = useState(false);
7
8 async function handlePayment() {
9 setLoading(true);
10 const res = await fetch('/api/payment/order', {
11 method: 'POST',
12 headers: { 'Content-Type': 'application/json' },
13 body: JSON.stringify({ amount: 49900 }) // amount in paise
14 });
15 const data = await res.json();
16
17 const options = {
18 key: process.env.NEXT_PUBLIC_RAZORPAY_KEY_ID,
19 amount: data.order.amount,
20 currency: data.order.currency,
21 order_id: data.order.id,
22 handler: async (response) => {
23 await fetch('/api/payment/verify', {
24 method: 'POST',
25 headers: { 'Content-Type': 'application/json' },
26 body: JSON.stringify(response)
27 });
28 alert('Payment verified successfully!');
29 }
30 };
31
32 const razorpay = new window.Razorpay(options);
33 razorpay.open();
34 setLoading(false);
35 }
36
37 return (
38 <button
39 onClick={handlePayment}
40 disabled={loading}
41 className="bg-emerald-500 hover:bg-emerald-600 px-4 py-2 rounded-md text-white"
42 >
43 {loading ? 'Processing...' : 'Pay ₹499'}
44 </button>
45 );
46}Usage
1// Example service usage (server-side)
2const { createOrder, verifyPaymentSignature } = require('./src/services/paymentService');
3
4(async () => {
5 const order = await createOrder({ amount: 10000, metadata: { user: 'John' } });
6 console.log('Created order:', order);
7})();