Module 40: System Design — Digital Wallet & Payment System

Comprehensive architectural breakdown of an Enterprise Payment and Digital Wallet System (Stripe, PayPal, Adyen), covering Double-Entry Bookkeeping, the Payment State Machine, Idempotency, and End-of-Day Reconciliation.


Prerequisites

Before tackling payment systems, review these core modules:


ELI5 (30-Second Summary)

Imagine a bank where money is kept in magical locked boxes. If Alice wants to send <span class="math-inline">50 to Bob, the bank never just erases </span>50 from Alice's account and writes +<span class="math-inline">50 in Bob's account. Why? Because if the power cuts out mid-way, Alice loses </span>50 and Bob receives nothing!

Instead, banks use Double-Entry Bookkeeping, an accounting law invented in the 1400s: every single transaction must have at least two entries where Total Debits = Total Credits. Money can never appear or vanish out of thin air.

Even if network cables are cut, external payment gateways (Visa, MasterCard) crash, or servers reboot, a Payment System uses strict mathematical ledgers, unique idempotency keys, and automated reconciliation accountants to ensure not a single penny is ever lost or duplicated!


Physical Analogy

Think of a Physical Two-Pan Balance Scale in a Gold Vault:

Gold Vault Analogy Payment System Concept Technical Description
Gold removed from Left Pan (Debit) Account Debit Entry Reduces asset balance or records an expense.
Gold added to Right Pan (Credit) Account Credit Entry Increases liability or records offsetting credit.
Scale Must Balance to Zero Double-Entry Bookkeeping Invariant Σ Debits - Σ Credits = 0. Absolute invariant across the ledger.
Embossed Serial Number on Gold Bar Idempotency-Key Header Client-assigned token preventing double-charging across network retries.
Vault Guard Checking Balance Log Daily End-of-Day Reconciliation Background auditing service verifying internal DB balances match bank settlement files.

The Problem It Solves

Payment systems operate under the strictest regulatory and correctness requirements in software engineering:

  1. Zero Data Loss & Zero Double-Charging: Charging a customer twice or dropping funds results in severe financial fines, fraud audits, and loss of business trust.
  2. Unreliable External Banking Networks: Card processing networks (Visa, Stripe, Adyen) can take 5 to 30 seconds to confirm charges, or time out entirely. Systems must handle in-flight transactions safely.
  3. Auditability & Traceability: Every penny entering, moving through, or exiting the platform must be cryptographically auditable for tax authorities and financial compliance (SOX, PCI-DSS).

Visual Architecture

Payment Execution, Reconciliation & Double-Entry Ledger

sequenceDiagram
    autonumber
    actor User as Buyer
    participant Client as Web / Mobile App
    participant PayGW as Payment Gateway Service
    participant IdemStore as Redis (Idempotency Store)
    participant Ledger as Double-Entry Ledger DB
    participant PSP as Payment Service Provider (Stripe/Adyen)
    participant Recon as Reconciliation Engine

    User->>Client: Click "Pay $50.00"
    Client->>PayGW: POST /v1/payments (Header: Idempotency-Key: req_99)
    PayGW->>IdemStore: SETNX req_99 "PROCESSING" EX 300
    PayGW->>Ledger: INSERT Payment State = PENDING
    PayGW->>PSP: Dispatch Charge ($50.00 to Visa/Mastercard)
    alt PSP Confirms Success
        PSP-->>PayGW: Charge Succeeded (charge_id: ch_visa_123)
        PayGW->>Ledger: Atomic TX: Debit Buyer -$50, Credit Seller +$50, Status=SUCCESS
        PayGW->>IdemStore: Update req_99 "COMPLETED" + Response JSON
        PayGW-->>Client: 200 OK (Payment Receipt)
    else PSP Times Out
        PSP-->>PayGW: Network Timeout / No Response
        PayGW->>Ledger: Mark Payment State = UNKNOWN_PENDING
        PayGW-->>Client: 504 Gateway Timeout (Advising Client to Retry with same key)
    end

    Note over Recon,PSP: Nightly Settlement (02:00 AM)
    Recon->>PSP: Download Bank Settlement CSV / MT940 File
    Recon->>Ledger: Verify Internal Ledger Rows == Bank Records
    Recon-->>Recon: Flag Discrepancies for Automated Dispute/Refund

Real Numbers & Financial Throughput

Consider an e-commerce platform processing $10 Billion Annually:


Deep Dive: Under the Hood

1. Double-Entry Bookkeeping

In financial ledgers, account balances are never stored as simple mutable integer columns (UPDATE users SET balance = balance + 50). Instead:

Σ Debits - Σ Credits = 0

2. The Payment State Machine

Payment records transition through a strict finite state machine: INITIALIZED → PENDING → AUTHORIZED → CAPTURED (or FAILED / REFUNDED).

3. End-of-Day Reconciliation

No distributed system is immune to network glitches. At 02:00 AM every night:

  1. The Payment Service Provider (e.g., Stripe, Chase Paymentech) generates a settlement file (settlement_20261123.csv).
  2. The internal Reconciliation Engine reads every row in the settlement file and compares it against the internal ledger database.
  3. If an entry is missing internally (e.g., PSP charged customer but webhook dropped), the reconciliation engine generates an internal adjusting journal entry.

Trade-offs & Decision Matrix

Mechanism Single-Entry Mutable Balances Double-Entry Immutable Ledger Distributed Saga Orchestration
Auditability Zero (no historical trace of changes) Perfect (immutable cryptographic trail) High (event log history)
Data Correctness High risk of money creation/loss Mathematically verified (debit = credit) Requires compensating transactions
Write Throughput High (single row update) Moderate (append-only rows) High (asynchronous decoupled services)
Regulatory Compliance Fails financial audits (SOX/PCI) 100% GAAP / IFRS compliant Compliant with proper ledger service

Common Pitfalls & Anti-Patterns

  1. Floating-Point Currency Calculations: Using standard IEEE 754 floats (float or double in Java/Python/JS) causes precision errors (0.1 + 0.2 = 0.30000000000000004). Always store currency as integers in the lowest fractional unit (cents: 1000 = </span>10.00$) or arbitrary-precision SQL types (NUMERIC(18, 4)).
  2. Mutating Balances In-Place: Executing UPDATE accounts SET balance = balance - 10 destroys audit history. Ledgers must be append-only.
  3. Trusting Webhooks Blindly Without Signature Verification: Attackers can spoof PSP webhook calls (POST /stripe/webhook with payment_succeeded). Always verify the cryptographic HMAC signature (Stripe-Signature header) using the secret key before processing.

Try It Yourself

A runnable, zero-dependency Python simulation demonstrating a Double-Entry Bookkeeping ledger, atomic debit/credit balance verification, payment state transitions, and idempotency protection is included in this directory.

Run the simulation:

python demo.py

Production References & Further Reading


Top 5 Interview Questions with Model Answers

Q1: Why is Double-Entry Bookkeeping mandatory in payment systems instead of updating account balances directly?

Model Answer: Directly updating balances (UPDATE accounts SET balance = balance - 50) creates a single point of failure with zero auditability. If an update fails mid-way, money appears or vanishes without trace. Double-Entry Bookkeeping enforces that every movement of money is recorded as an immutable journal entry with matching Debits and Credits (Σ Debits = Σ Credits).

  1. It guarantees that money cannot be created or destroyed out of thin air.
  2. It provides an unalterable audit trail for financial regulators (SOX, GAAP).
  3. Any balance discrepancy can be diagnosed by checking the mathematical sum of historical entries.

Q2: Why should monetary values never be stored or manipulated using floating-point data types?

Model Answer: IEEE 754 floating-point representations (such as float or double) cannot represent fractional base-10 decimals precisely. For example, adding 0.1 + 0.2 in floating-point arithmetic results in 0.30000000000000004. In financial systems handling millions of transactions, rounding errors accumulate into significant financial discrepancies. Instead:

Q3: How do you handle an external Payment Service Provider (PSP) network timeout?

Model Answer:

  1. When a timeout occurs, the payment must transition to an indeterminate state: UNKNOWN_PENDING (never mark it as failed immediately, because the bank may have processed the charge!).
  2. Return an advisory status to the client with the instruction that the transaction is in-flight.
  3. An asynchronous reconciliation worker queries the PSP's status API (GET /v1/charges/{idempotency_key}) or awaits an incoming signed PSP Webhook to learn the definitive outcome.
  4. If confirmed successful, finalize the ledger transaction; if confirmed failed, transition to FAILED and release any authorization holds.

Q4: How does End-of-Day Reconciliation work, and what happens when a discrepancy is detected?

Model Answer: Every night, payment processors provide settlement batch files (CSV or MT940 format) listing every settled transaction. The Reconciliation Engine:

  1. Parses the settlement file and matches each record against the internal ledger database by transaction ID / idempotency key.
  2. Three-Way Matching: Compares (a) Client Purchase Orders, (b) Internal Ledger Records, and (c) Bank Settlement Records.
  3. If a discrepancy is found (e.g., customer was charged by Visa, but internal server crashed before recording the order), the system flags the transaction for automatic remediation (triggering order fulfillment or issuing an automatic refund).

Q5: How do you prevent replay attacks and fraudulent webhooks from external payment processors?

Model Answer:

  1. HMAC Cryptographic Signatures: PSPs (Stripe/PayPal) compute an HMAC-SHA256 signature of the webhook payload concatenated with a timestamp using a shared secret key, sent in the header (Stripe-Signature). The server computes the hash independently and rejects mismatched requests.
  2. Timestamp Tolerance: Verify that the webhook timestamp is within a 5-minute tolerance window to prevent replay attacks where an attacker captures and re-sends valid historical payloads.
  3. Idempotent Webhook Handlers: Process webhook IDs inside an atomic idempotency key check so duplicate deliveries are ignored safely.

Check Yourself Quiz

1. What is the fundamental mathematical equation of Double-Entry Bookkeeping? A) Assets = Liabilities
B) Sum of Debits - Sum of Credits = 0
C) Revenue = Profit
D) Cash = Debt

Answer: B. Every financial journal entry must balance exactly to zero: total debits must equal total credits.
2. Why must currencies be stored as integers or fixed-point decimals? A) Because computers cannot store decimal points.
B) Because floating-point data types suffer from rounding inaccuracies that accumulate into financial errors.
C) Because banks only accept whole dollar amounts.
D) Because integers encrypt the balance.

Answer: B. Binary floating-point cannot accurately represent decimal fractions like 0.1, creating unacceptable rounding discrepancies.
3. What should a payment system do when an external card processor times out? A) Mark the payment as FAILED and refund immediately.
B) Mark the state as PENDING and resolve the terminal outcome via webhooks or polling.
C) Delete the customer's account.
D) Retry 1,000 times in a tight while loop.

Answer: B. The charge may have succeeded at the bank. Moving to PENDING allows safe asynchronous verification.
4. How does an API Idempotency Key prevent double charging? A) By hashing the customer's credit card number.
B) By using an atomic key-value check to recognize repeated submissions and return the original cached response.
C) By disabling the submit button.
D) By shutting down the API gateway.

Answer: B. The atomic key check detects repeat submissions and safely returns the prior result without re-executing charges.
5. What is the purpose of nightly reconciliation in payment engineering? A) To back up SQL databases to tape.
B) To compare internal ledger transactions against bank settlement files and identify any discrepancies.
C) To calculate employee bonuses.
D) To wipe out transaction history.

Answer: B. Nightly reconciliation validates internal data against official banking records to ensure absolute accounting fidelity.