KatInterview

Build a Charge-Once Card Payment Service Behind a Gateway

System DesignModel answer
HardModel answerSystem Designidempotencydistributed-systemspayments

Last updated

Design an internal payment service that charges a card at most once per order despite retries, timeouts and crashes, and reconciles its records with an external payment provider.

Scenario

Your company runs an order service that must collect money from customers. You own a new internal payment service that sits between the order service and an external card gateway. The gateway exposes a charge API and supports idempotency keys, but it is a remote system that can be slow, return errors, or drop responses.

The order service calls you with:

process_payment(order_id, amount, payment_token)

Design this service so that each order results in exactly one successful charge, even when:

  • the order service retries the same call (possibly concurrently),
  • your own servers crash mid-request,
  • the network drops the gateway's response after the card was actually charged.

Requirements

Functional

  • At most one successful charge per order_id, no matter how many times the call is repeated.
  • Repeated calls must be safe and return a consistent answer.
  • Every payment has a tracked status: Pending, Succeeded or Failed.

Non-functional

  • Strong consistency: your stored state must eventually match what the gateway actually did.
  • Fault tolerance: the system must survive partial failures anywhere in the request/response path.

What to cover in your answer

  1. Assumptions about scale, gateway behavior and money representation.
  2. The API contract, including what callers see when the outcome is not yet known.
  3. Data model and status lifecycle.
  4. The request flow, step by step.
  5. How you handle the case where the gateway charged the card but you never received the acknowledgment.
  6. How idempotency is enforced across services, not just inside your database.
  7. How you detect and repair mismatches between your records and the provider's.
  8. Key trade-offs and common mistakes.

Hints

Hint 1

Networks cannot deliver exactly once. Think of the guarantee as repeated attempts combined with idempotent execution, and ask where the idempotency must be enforced.

Hint 2

Write your intent to your own database before calling the gateway, and introduce a status for 'we do not know what happened'. A timeout must never be treated as a failure.

Hint 3

Reuse the same idempotency key for every retry of an order, and add a background reconciler plus a periodic comparison against the provider's settlement data as a safety net.