Payment Gateway

Payment Processing System

Design the core of a payment gateway: authorize, capture, void and refund through a processor per payment method, a strict payment state machine, idempotency keys that make retries safe, and an append-only audit trail.

Overview

A payment gateway sits between a merchant's code and the networks that move money. The merchant asks it to authorize an amount (check the card and hold the money), later to capture it (actually take the money), to void an authorization it no longer wants, or to refund some or all of a captured payment. Each payment method (card, wallet, bank transfer) talks to a different processor, but the merchant sees one interface.

Interviewers use this problem to test correctness under retries. Networks time out, and a merchant that did not see the reply will send the request again. The gateway must never charge twice for one intent, never refund more than it captured, and never capture a payment that was voided. A strong answer puts the rules in a state machine, makes every write carry an idempotency key, and keeps money in integer minor units (cents).

The design has three parts that change at different speeds: the payment lifecycle (almost never), the processors (a new one every few months), and the retry rules (once, carefully). Keeping them in separate classes is the point of the exercise.

Scoping the problem

Ask these questions before you design anything, because each answer adds a state, a field or a rule.

  • One step (charge) or two (authorize, then capture)? Two steps add states, void and expiry. The usual answer is both: charge is authorize plus capture.
  • Partial capture? Several captures? This decides what capture stores. The usual answer is one capture, up to the authorized amount.
  • Partial refunds? Several? Usually yes, any number, but never more than captured in total, so refunds must be tracked as a running total.
  • Which payment methods? Each one is a processor behind one interface. Card and wallet are enough to start.
  • How do clients retry? This decides how idempotency keys work. Every write carries a key the client chooses.
  • Do we store card numbers? No, because storing them brings heavy compliance duties. Store the processor's token and the last four digits.
  • Currencies? Minor units differ by currency, so each payment has one currency, in integer minor units.

Functional requirements

  • authorize(key, amount, currency, method, token): the processor for the method approves and holds the amount, or declines
  • capture(key, payment_id, amount): take up to the authorized amount, once
  • void(key, payment_id): cancel an authorization that was not captured and release the hold
  • refund(key, payment_id, amount): any number of partial refunds; the total never exceeds the captured amount
  • Every write carries an idempotency key: a retry returns the first result; the same key with a different request is refused
  • Every state change is appended to an audit trail

Non-functional requirements

  • Correctness under retries: one intent never reaches the processor twice
  • Money is integer minor units; no floating point
  • Thread safe: a lock per key, a lock per payment, one short gateway lock, always in that order
  • A new payment method is a new Processor class; the gateway does not change
  • The gateway stores processor tokens, never card numbers

Interview questions

  • The gateway runs on many servers. How do idempotency keys still work?
  • The processor times out during capture. What state is the payment in?
  • Why authorize and capture separately? What if the merchant never captures?
  • How would you add a new payment method, say bank transfer?
  • Why integer cents? And how do you store card details?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio