KatInterview

Design a Payment Processing Service with an External PSP

System DesignModel answer
MediumModel answerSystem Designpaymentsidempotencydistributed-systems

Last updated

Design a backend payment service that collects money via a third-party payment provider, guarantees exactly-once charging, and maintains an auditable money ledger.

The Problem

Your company's product services — checkout, billing, subscriptions — need a single internal service to handle collecting money from customers. Build the backend payment service that sits between those product services and an external Payment Service Provider (PSP).

The PSP handles raw card and bank data. Your system stores only the tokens and references the PSP hands back.

Core Operations

  • Charge: a calling service requests that a specific amount and currency be collected from a customer, tied to an order or invoice.
  • Settlement: the PSP communicates the final result either synchronously in its response or asynchronously via a webhook.
  • Refund: a settled charge may be reversed, fully or partially.
  • Lookup: internal services and support tooling can retrieve the current state and full history of any payment.

Hard Requirements

  • A customer must never be billed twice for the same order, even if the caller, your service, or the PSP retries the request.
  • All monetary values must be represented exactly — no floating-point rounding anywhere on the money path.
  • Every state change must be durably recorded and auditable after the fact.

Clarifying Questions to Raise

Before diving into the design, a strong candidate would ask:

  • Is this for one internal product or a multi-tenant merchant platform?
  • What charge patterns are needed — one-time, recurring, usage-based, or mixed?
  • What are the expected peak transaction rates, and which currencies and regions must be covered?
  • Does the customer wait synchronously for a payment result (checkout screen), or is async completion acceptable?
  • Does the system only collect money, or does it also need to pay out (to sellers, partners, etc.)?
  • Is there one PSP or multiple, with routing or failover logic?

Work through these with your interviewer before committing to scope. The design below assumes one company's product, a mix of one-time and recurring charges, a single primary PSP, and a peak load in the low thousands of transactions per second.

Hints

Hint 1

Start by drawing the payment state machine explicitly — list every state a payment can be in and every transition. The tricky one is the 'outcome unknown' state you land in when a PSP call times out without a response.

Hint 2

Exactly-once charging requires idempotency at three independent layers: the API your callers hit, the internal workflow that drives the charge, and the request your service makes to the PSP. Think about what idempotency key you use at each layer and where it's stored.

Hint 3

Don't conflate the payment record with the ledger. The payment record tracks what happened operationally; the ledger records money movement in double-entry form. Reconciliation means comparing your ledger entries against the PSP's settlement reports — think about what discrepancies can arise and how you detect them.