Menu

1 / 3

How I made FoodIO's Razorpay payments not charge twice

DEV Community: nextjs·Anastand·9 days ago
#NqUUlfGT
#devtonextjs#devto#dev#order#razorpay
Reading 0:00
15s threshold

I'm building FoodIO, a food-ordering app, to learn backend work that has real consequences. It's Next.js (App Router), TypeScript, Postgres on Neon and Drizzle ORM. Customers pay with Razorpay, in test mode for now.

Payments scared me most. A bug here is not a broken button. It's a customer charged twice, or a customer who paid and got nothing.

My goal was "exactly-once". Then I realised that over a network you can't promise a message arrives exactly once. Requests get retried, users double-click, tabs close halfway. So I aimed for each step being safe to repeat (idempotent). If a step runs twice, the effect still happens once.

Below are the four places my flow could break, and the guard I added for each one. All code is from the real repo. At the end is what I got wrong and what is still missing.

The flow

Browser FoodIO server (Next.js) Postgres Razorpay ─────── ─────────────────────── ──────── ──────── 1.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.