๐ Easy Way to Integrate Razorpay Payment Gateway (Step-by-Step Guide)


You can easily go through the Razorpay official payment documentation for detailed insights, but hereโs a simplified step-by-step guide to help you integrate it quickly.
Accepting payments online is essential for modern web applications, and Razorpay makes it super easy. In this guide, I'll walk you through the simplest way to integrate Razorpay into your website step by step.
By the end of this tutorial, you'll have a working Razorpay payment gateway in your application! ๐
๐น Step 1: Create a Razorpay Account
First, sign up on Razorpay and log in to your dashboard.
Go to the API Keys section under Settings.
Generate Live and Test Mode keys.
Save the
KEY_ID
andKEY_SECRET
โyou'll need them later.
๐น Step 2: Install Razorpay SDK
For Node.js (Backend), install the Razorpay package:
shCopyEditnpm install razorpay
For Frontend (React/Vanilla JS), load the Razorpay checkout script:
htmlCopyEdit<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
๐น Step 3: Create a Razorpay Order (Backend)
In your Node.js backend, set up a /create-order API route:
jsCopyEditconst Razorpay = require("razorpay");
const express = require("express");
const app = express();
app.use(express.json());
const razorpay = new Razorpay({
key_id: "YOUR_KEY_ID",
key_secret: "YOUR_KEY_SECRET",
});
app.post("/create-order", async (req, res) => {
const options = {
amount: req.body.amount * 100, // Amount in paise
currency: "INR",
receipt: "order_rcptid_11",
};
try {
const order = await razorpay.orders.create(options);
res.json({ orderId: order.id });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.listen(5000, () => console.log("Server running on port 5000"));
โ This API will generate an order for Razorpay to process the payment.
๐น Step 4: Integrate Razorpay in Frontend
In React, call the API and open Razorpay's checkout window:
jsCopyEditconst handlePayment = async () => {
const response = await fetch("http://localhost:5000/create-order", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ amount: 500 }), // Amount in INR
});
const { orderId } = await response.json();
const options = {
key: "YOUR_KEY_ID",
amount: 50000, // Amount in paise
currency: "INR",
name: "Your Business Name",
description: "Test Transaction",
order_id: orderId,
handler: function (response) {
alert("Payment Successful! Payment ID: " + response.razorpay_payment_id);
},
theme: { color: "#3399cc" },
};
const rzp = new window.Razorpay(options);
rzp.open();
};
return <button onClick={handlePayment}>Pay Now</button>;
โ Clicking "Pay Now" will open Razorpay's checkout page.
๐น Step 5: Payment Status - Understanding Capture, Pending & Successful Payments
Once the payment is done, Razorpay returns a payment status. Here's how it works:
Status | Meaning |
Created | Order is created, but payment not started |
Authorized | Payment is successful, but needs to be captured (only for manual capture mode) |
Captured | Payment is successfully received |
Failed | Payment was not successful |
Refunded | Payment was refunded |
โ Capturing Payment (Backend)
If your Razorpay account is set to manual capture, you must capture the payment using an API:
jsCopyEditapp.post("/capture-payment", async (req, res) => {
const paymentId = req.body.paymentId;
try {
const captureResponse = await razorpay.payments.capture(paymentId, 50000, "INR");
res.json({ success: true, response: captureResponse });
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
});
โ If your Razorpay settings use "Auto Capture," this step is not needed.
๐น Step 6: Verify Payment on Server
Once the payment is done, Razorpay sends back a payment ID. Verify it on your backend:
jsCopyEditconst crypto = require("crypto");
app.post("/verify-payment", (req, res) => {
const { razorpay_order_id, razorpay_payment_id, razorpay_signature } = req.body;
const generated_signature = crypto
.createHmac("sha256", "YOUR_KEY_SECRET")
.update(razorpay_order_id + "|" + razorpay_payment_id)
.digest("hex");
if (generated_signature === razorpay_signature) {
res.json({ success: true, message: "Payment verified successfully" });
} else {
res.status(400).json({ success: false, message: "Payment verification failed" });
}
});
๐น Step 7: Understanding Razorpay Payment Flow with an Image
To make it easier, hereโs a payment flow diagram explaining the process:
User clicks "Pay Now" โ Razorpay Checkout opens
User completes payment โ Razorpay sends
order_id
Razorpay verifies payment โ Sends response
Backend verifies payment using signature
Payment is marked as Captured/Pending/Failed
๐ Done!
You now have a working Razorpay payment gateway integrated into your website! ๐
๐ฅ Whatโs Next?
Save the payment details in your database.
Handle webhooks for automated responses.
Switch to Live Mode before launching.
Got questions? Let me know in the comments! ๐ฌ
Subscribe to my newsletter
Read articles from Ravi Vira directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
