Lesson 10 of 16
42. Design E-commerce Cart and Checkout (Amazon / Flipkart)

42.9 How does the payment hand-off work?

3 min read+10 XP

Payment is asynchronous and unreliable: UPI approvals take time, gateways time out, webhooks arrive late or twice. Model the order as a state machine with a place for every outcome:

created → payment_pending → paid → confirmed; payment_failed → cancelled; no payment in time → expired; payment after expiry → refund_due.

Figure: every payment outcome has a state to go to; nothing is left ambiguous.

In the chapter's code:

  • A success webhook moves an order to confirmed and confirms its stock hold; a duplicate webhook is ignored.
  • A failure webhook cancels the order and releases stock.
  • A customer who takes too long: the order expires and stock is released; when the money arrives later, the order goes to refund_due, never shipped without stock.
  • Illegal moves (cancelling a confirmed order through this path) are refused.

Also:

  • Verify webhooks (signature, timestamp, Chapter 24) and reconcile periodically by asking the gateway for the status of every payment_pending order, in case a webhook never arrives.
  • Show the shopper the truth: "payment processing" until confirmed, never "order placed" on a guess.

Memory code: "Every payment outcome has a state, and late money goes back."