Design a payment system for an online shop.
System designSenior
In shortA payment service that records every movement in an append-only, double-entry ledger, drives each payment through an explicit state machine, calls the payment provider idempotently, reconciles with the provider's reports, and treats webhooks and retries as normal, not exceptional.
Model answer
Requirements: exactly-once money movement as seen by the customer, auditability, eventual consistency is acceptable for reporting, latency in seconds is fine. Volumes are usually modest; correctness dominates.
Components:
- Checkout/Order service creates a payment intent with an idempotency key per order attempt.
- Payment service owns a state machine:
CREATED → AUTHORIZING → AUTHORIZED → CAPTURED(plusFAILED,CANCELED,REFUNDED,DISPUTED). Every transition is persisted before and after external calls, so a crash resumes from a known state. - Provider adapter (Stripe, Adyen…) calls the provider with its own idempotency key derived from the payment id, handles 3-D Secure redirects, and exposes a uniform interface.
- Webhook handler verifies signatures, dedupes by event id, and advances the state machine; webhooks may arrive before, after or instead of the synchronous response.
- Ledger: append-only, double-entry records (every movement debits one account and credits another; entries always sum to zero). Balances are derived, never overwritten.
- Reconciliation job compares the ledger with the provider’s settlement reports daily and flags mismatches.
Failure handling: timeouts mean “unknown”: query the provider by idempotency key before retrying; outbox events notify the order service; refunds are new ledger entries, not edits.
Security: card data never touches your servers (provider-hosted fields or SDK tokenization), keeping PCI scope small; strict access to the ledger; audit logs.
What they're looking for
Clear requirements (correctness over latency), a state machine, idempotency at every hop, a double-entry ledger, provider integration (redirects, 3-D Secure, webhooks), reconciliation, and failure handling. Bonus for PCI scope reduction with tokenization.
Common mistakes
- Updating a balance column instead of keeping a ledger.
- Assuming the provider call either succeeds or fails, never times out.
- Storing card numbers on your own servers.