Lesson 8 of 16
45. Design a Payment System (Stripe / UPI)

45.7 How does a double-entry ledger work?

3 min read+10 XP

a capture of ₹1,248 debits bank clearing and credits the merchant and fees, summing to zero; a refund is a new reversing transaction.

Figure: every transaction's lines add up to zero; history is never edited.

  • Every movement is a transaction of lines that sum to zero. Money doesn't appear or vanish; it moves between accounts. A ₹1,248 payment with a 2% fee: bank clearing +1,24,800, merchant −1,22,304 (we owe the merchant), fees −2,496 (our revenue). In the chapter's code, the ledger's total is zero after every step.
  • Unbalanced transactions are rejected outright; in the code, a 100 vs −90 posting raises an error.
  • Append-only: a refund is a new reversing transaction, never an edit. History stays complete for audits.
  • Balances are derived by summing lines (cached and periodically verified), so a wrong balance can always be traced to the entries that made it.
  • Integers only (paise), never floating point.
  • Accounts per merchant make payouts simple: the payout job pays the merchant's balance and posts the matching transaction.

Memory code: "Every rupee comes from somewhere and goes somewhere, and the lines always add to zero."