Fixing Razorpay 400 BAD_REQUEST_ERROR: Paise Integer Subunits & Order Validation
Resolve Razorpay 400 BAD_REQUEST_ERROR on /v1/orders and Standard Checkout in Node.js. Fix IEEE-754 floating-point paise conversion (19.99 * 100 = 1998.9999999999998), minimum 100 paise rules, and Test/Live order_id mismatches.
Razorpay's `/v1/orders` and Standard Checkout APIs throw `400 BAD_REQUEST_ERROR` under four primary conditions in Node.js: (1) passing a floating-point number for `amount` due to JavaScript IEEE-754 arithmetic (e.g., `19.99 * 100` evaluates to `1998.9999999999998` instead of integer `1999` paise, triggering `'The amount must be an integer'`), (2) passing an amount below `100` paise (`₹1.00`), (3) passing a `receipt` string longer than 40 characters, or (4) creating an `order_id` on the backend using Live keys (`rzp_live_*`) while initializing the frontend checkout modal with a Test key (`rzp_test_*`), which returns `'The id provided does not exist'`.
Verified Root Causes in Production Environments
- JavaScript IEEE-754 Floating-Point Multiplication Drift: Computing `priceInr * 100` in JavaScript without `Math.round()` produces decimals like `149.90 * 100 -> 14989.999999999998` or GST totals like `149 * 1.18 * 100 -> 17582.000000000004`, which Razorpay rejects.
- Sub-Unit Minimum Violation (< 100 Paise): Passing `amount: 99` or passing INR instead of paise (`amount: 1` for ₹1 instead of `100` paise) fails Razorpay's minimum ₹1.00 (`100` paise) validation rule.
- Test vs Live Mode Key Mismatch (`The id provided does not exist`): Generating `order_xxxx` with `rzp_test_*` on the server while `NEXT_PUBLIC_RAZORPAY_KEY_ID` on the client is set to `rzp_live_*` (or vice versa).
1. Fix IEEE-754 Float Errors With a Strict Paise Integer Sanitizer
In Indian SaaS billing—especially when adding 18% GST (`amount * 1.18`) or applying percentage promo codes—JavaScript's 64-bit floating-point numbers routinely introduce microscopic mantissa fractions. For example, in Node.js V8, `19.99 * 100` equals `1998.9999999999998`, and `149 * 1.18 * 100` equals `17582.000000000004`.
Because Razorpay's Orders API strictly validates `Number.isInteger(amount)` in currency subunits (paise for `INR`, where `100` paise = `₹1.00`), sending an unrounded float immediately throws `BAD_REQUEST_ERROR: The amount must be an integer`. Always wrap your subunit conversion in `Math.round(Number(amountInr) * 100)` and enforce `amountPaise >= 100`.
import Razorpay from 'razorpay';
const razorpay = new Razorpay({
key_id: process.env.RAZORPAY_KEY_ID!,
key_secret: process.env.RAZORPAY_KEY_SECRET!,
});
export interface CreateSafeOrderInput {
amountInr: number; // e.g., 149 or 175.82 (with 18% GST)
receiptId: string;
notes?: Record<string, string>;
}
export async function createSafeRazorpayOrder({ amountInr, receiptId, notes }: CreateSafeOrderInput) {
// 1. Convert INR to integer paise safely (eliminates IEEE-754 float drift)
const amountPaise = Math.round(Number(amountInr) * 100);
if (!Number.isInteger(amountPaise) || amountPaise < 100) {
throw new Error(
`Invalid Razorpay amount: ${amountPaise} paise. Minimum allowed is 100 paise (₹1.00).`
);
}
// 2. Enforce Razorpay's strict 40-character maximum limit on receipt strings
const safeReceipt = String(receiptId).trim().slice(0, 40);
// 3. Ensure notes object contains at most 15 key-value pairs (strings <= 256 chars)
const safeNotes: Record<string, string> = {};
if (notes) {
Object.entries(notes)
.slice(0, 15)
.forEach(([k, v]) => {
safeNotes[k.slice(0, 256)] = String(v).slice(0, 256);
});
}
return razorpay.orders.create({
amount: amountPaise,
currency: 'INR',
receipt: safeReceipt,
notes: safeNotes,
});
}2. Fix 'BAD_REQUEST_ERROR: The id provided does not exist' in Next.js Checkout
The second most searched Razorpay `BAD_REQUEST_ERROR` is `The id provided does not exist`, triggered when opening the `new window.Razorpay(options)` modal in the browser. Developers often spend hours debugging their database only to realize the Order ID *was* created successfully, but under a different environment key mode.
In Next.js, `NEXT_PUBLIC_RAZORPAY_KEY_ID` is inlined into the client bundle at `next build` time, whereas `RAZORPAY_KEY_ID` on the server is read at runtime. If your Docker build baked `rzp_test_123` into the frontend bundle while your production container `.env` injects `rzp_live_456` on the backend, the Live order ID does not exist in Razorpay's Test database. Fix this by returning `keyId: process.env.RAZORPAY_KEY_ID` directly from your `/api/orders/create` backend response so the client modal and server order always use the exact same key.
import { NextRequest, NextResponse } from 'next/server';
import { createSafeRazorpayOrder } from '@/lib/razorpay-order-sanitizer';
export async function POST(req: NextRequest) {
try {
const { planPriceInr, userId } = await req.json();
const order = await createSafeRazorpayOrder({
amountInr: planPriceInr * 1.18, // Include 18% GST safely
receiptId: `vexlo_${userId}_${Date.now()}`,
notes: { userId, plan: 'vexlo_pro_monthly' },
});
// Return the active server RAZORPAY_KEY_ID alongside order.id so client & server never mismatch!
return NextResponse.json({
orderId: order.id,
amount: order.amount,
currency: order.currency,
keyId: process.env.RAZORPAY_KEY_ID,
});
} catch (err: any) {
console.error('[Razorpay Order Error]:', err?.error || err);
return NextResponse.json(
{ error: err?.error?.description || err.message },
{ status: 400 }
);
}
}3. Quick Lookup Table of Razorpay /v1/orders 400 BAD_REQUEST_ERROR Descriptions
When catching errors from the `razorpay` Node.js SDK, inspect `err.statusCode` (`400`) and `err.error.description` to pinpoint which field failed schema validation.
{
"The amount must be an integer.": "You passed a float (e.g., 149.50 or 1998.9999999999998) or a string with decimals. Wrap in Math.round(inr * 100).",
"The amount must be atleast INR 1.00": "You passed an amount < 100 paise (e.g., passing 50 thinking it means ₹50 instead of 5000 paise).",
"The id provided does not exist": "Frontend Razorpay checkout key (rzp_test vs rzp_live) does not match the backend key that created order_id, OR you passed payment_id into the order_id field.",
"receipt: length must be <= 40.": "Your custom receipt string (e.g., UUID + timestamp + prefix) exceeded 40 characters. Slice to .slice(0, 40)."
}Pre-Deployment Verification Checklist
- Always compute `amount` in paise using `Math.round(Number(inrAmount) * 100)`.
- Verify `amount >= 100` (₹1.00 minimum for INR orders).
- Truncate custom `receipt` strings to `.slice(0, 40)` before calling `razorpay.orders.create()`.
- Pass `keyId: process.env.RAZORPAY_KEY_ID` from your order-creation API response to `new window.Razorpay({ key: data.keyId, order_id: data.orderId })` so Test and Live keys never mismatch.
Deploy on Vexlo Cloud in 60s (₹149/mo UPI)
Automated Docker & Nixpacks builds, managed PostgreSQL/Redis, and AI Deployment Doctor. Instant INR UPI for India or USD/EUR/GBP across 17 global currencies.
Frequently Asked Questions
Why does Razorpay say 'The amount must be an integer' when I multiply 19.99 by 100 in Node.js?
In JavaScript's IEEE-754 floating-point representation, `19.99 * 100` evaluates to `1998.9999999999998`, which is not an integer. Always wrap the calculation in `Math.round(amountInr * 100)`.
Why does Razorpay Checkout say 'The id provided does not exist' when the order_id is in my console log?
Your backend created the order using a Live key (`rzp_live_*`) while your frontend checkout script initialized with a Test key (`rzp_test_*`), or vice versa. Return `process.env.RAZORPAY_KEY_ID` from your backend order creation endpoint and pass it directly into the checkout options.
What is the maximum length of the receipt parameter in Razorpay Orders API?
Razorpay enforces a maximum length of 40 characters on the `receipt` string. Passing a 41+ character string throws a 400 `BAD_REQUEST_ERROR`.