What it is
A minimal, working Razorpay checkout: a React form that collects a name, email and amount, and a small Express server that creates the Razorpay order. It's the smallest amount of code that gets the real Razorpay payment window (UPI, cards, netbanking) to open from your own page. This page walks through how the two halves fit together, and then through the changes I made when I used the same pattern for the paid downloads on this website, because the tutorial version is not safe to take payments with as it stands.
Why there has to be a server
Razorpay gives you two keys: a public key ID and a private key secret. Creating an order needs the secret, and anything in a React app is visible to anyone who opens the browser's developer tools. So the order is created on the server, and the browser only ever sees the key ID.
The flow
- The user fills in the form and presses Pay Now.
- React posts the amount to
/api/create-orderon the Express server. - The server calls
razorpay.orders.create()with the secret and returns the order, including itsid. - React opens Razorpay Checkout with the key ID and that order ID, pre-filling the user's name and email.
- The user pays inside Razorpay's window, and Razorpay calls your
handlerfunction withrazorpay_payment_id,razorpay_order_idandrazorpay_signature.
The server side of the tutorial is about 30 lines:
app.post('/api/create-order', async (req, res) => {
const { amount } = req.body;
const options = {
amount: amount * 100, // Razorpay expects amount in paise
currency: "INR",
receipt: "receipt#" + Date.now(),
payment_capture: 1
};
const response = await razorpay.orders.create(options);
res.json(response);
});
Amounts are in paise, the smallest unit of the rupee, which is why the rupee amount is multiplied by 100. Both keys come from a .env file loaded by dotenv, so they're never committed to Git.
Three problems before this can take real money
The tutorial works well in Razorpay's test mode, but it has three gaps that matter as soon as real payments are involved. All three are fixed in the payment code this website runs.
1. The browser decides the price
The amount comes from the request body, and the request body comes from the user. Anyone can open the network tab, replay the request with amount: 1, and pay ₹1 for a ₹3,000 product. For a donation form where the user picks the amount, that's fine. For anything with a price, the browser should send what it is buying, and the server should look up how much it costs. On this site the request carries only the item's type and ID, and the price comes from a server-side catalogue:
const { type, id } = req.body || {};
const item = getPaidItem(type, id); // server-side price list
if (!item) return res.status(400).json({ error: 'Unknown item' });
const order = await razorpay.orders.create({
amount: item.price * 100,
currency: 'INR',
receipt: `receipt_${Date.now()}`,
notes: { type, id }, // remembered on the order itself
});
2. "Payment successful" is never verified
In the tutorial, the handler runs in the browser and simply shows an alert. Anything that runs in the browser can be faked, so if the "success" step unlocks a download, someone can call that step without paying at all. Razorpay solves this with the razorpay_signature: an HMAC-SHA256 of order_id|payment_id, signed with your key secret. Only Razorpay and your server know the secret, so only a genuine payment can produce a matching signature. This site sends the three values to a /verify-payment route, which recomputes the signature and compares the two in constant time:
const expected = crypto
.createHmac('sha256', keySecret)
.update(`${razorpay_order_id}|${razorpay_payment_id}`)
.digest('hex');
const valid =
expected.length === razorpay_signature.length &&
crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(razorpay_signature));
Only after that check passes does the server fetch the order from Razorpay, read the item from the order's notes (not from the request), and return the download link. The paid links live only on the server, so they're never part of the JavaScript the browser downloads.
3. Decimal amounts can break the order
Razorpay requires the amount as a whole number of paise. Because JavaScript numbers are binary floating point, 19.99 * 100 evaluates to 1998.9999999999998, not 1999, and an order created with that amount is rejected. Prices on this site are whole rupees, so it never comes up here. If you do take decimal amounts from users, round explicitly: Math.round(Number(amount) * 100), and check the result is a positive integer before calling Razorpay.
Smaller details that save debugging time
- Load the checkout script.
window.Razorpayonly exists afterhttps://checkout.razorpay.com/v1/checkout.jshas loaded, so add it topublic/index.html. - Keep test and live keys apart. An order created with live keys can't be paid in a checkout opened with test keys. This site's server returns the key ID along with each order, so the browser always opens checkout on the same account the order was created on.
- Show Razorpay's real error. The Razorpay Node SDK rejects with an object shaped like
{ statusCode, error: { description } }, not a JavaScriptError, soerror.messageis undefined. Readerror.error.descriptionto see messages like an invalid amount or wrong key.
Running it
# backend/.env
RAZORPAY_KEY_ID=rzp_test_...
RAZORPAY_KEY_SECRET=...
cd backend && npm install && node server.js # port 5001
cd frontend && npm install && npm start # REACT_APP_RAZORPAY_KEY_ID in frontend/.env
Use test-mode keys from the Razorpay dashboard while developing. Test mode accepts Razorpay's published test cards and test UPI IDs, so you can go through the whole flow, including failures, without moving any money.