Design a payment gateway like Stripe with refunds and merchant webhooks

Quick Overview

Design a payment gateway that charges cards, issues full and partial refunds, reports payment status and notifies merchants of state changes through webhooks. It tests how you prevent double charges on retries, keep card data out of most of the system, deliver every notification after a crash and reconcile your records with the bank.

Design a payment gateway like Stripe with refunds and merchant webhooks

Role: Software Engineer

Category: System Design

Difficulty: hard

Interview Round: Other

Design a payment gateway similar to Stripe: the service merchants call to charge their customers, which in turn talks to acquiring banks and card networks on the merchants' behalf. The system handles payment state changes and notifies merchants of those changes. The gateway must support these functional requirements: 1. Clients can charge a customer's credit card or other payment method. 2. Clients can issue full or partial refunds. 3. The system notifies merchants asynchronously (webhooks) when a payment's state changes. 4. Merchants can query the status of a specific transaction. Propose the non-functional requirements yourself, then work through the six parts. ```hint Start from the failures List what can go wrong between the merchant, your services and the bank: a retry after a timeout, a crash between two writes, a record that disagrees with the bank's. Each one should map to a part of your design. ``` ```hint Name the source of truth Decide which record is authoritative for a payment's state inside your system, and which one is authoritative outside it. ``` ### Clarifying Questions to Ask - Do merchants authorize and capture in one step, or can they authorize now and capture later (for example when goods ship)? - What transaction volume and peak checkout rate should the design handle? - Are payment methods other than cards in scope? ### Part 1 — Requirements, core entities and API State the non-functional requirements of a payment gateway and how you would rank them when they conflict. Then name the core entities you would store and sketch a REST API for creating a payment, reading its status and issuing a refund. ```hint Price each kind of error Compare what a merchant loses when a payment is briefly rejected with what it loses when a payment is charged twice or lost. ``` #### What This Part Should Cover - The non-functional requirements and which one wins when two conflict - Entities that track a payment's lifecycle, its audit trail and card data - An API with merchant authentication, a way to make retries safe, and amounts that cannot suffer rounding errors ### Part 2 — High-level design Lay out the high-level components the gateway needs. Focus on the overall architecture for now; the later parts fix its weak points. ```hint Split by who is waiting Separate the work the customer waits on at checkout from the work that can finish after the response. ``` #### What This Part Should Cover - The synchronous path from the merchant's request to the bank's approval or decline, including fraud checks and protocol translation - The asynchronous path that tells merchants about state changes - Which component holds the authoritative state of a payment ### Part 3 — Preventing double charges Network timeouts are inevitable, and a client that does not get a response will retry. Explain the technique and the components that make sure the system never charges a customer twice. Then explain how a refund stays correct when a merchant sends two partial refunds for the same payment at the same moment. ```hint Label the attempt Ask which party can give one checkout attempt a label that every retry of it repeats. ``` #### What This Part Should Cover - How the server recognizes a retry as the same attempt - What it returns for a retry that arrives while the first request is still running, and for one that arrives after it finished - What happens if the component that remembers attempts loses data, or if the call to the bank itself times out - How two concurrent partial refunds are kept from refunding more than was captured ### Part 4 — PCI-DSS compliance In the high-level design, raw card numbers pass through the API gateway and the payment service. Explain how to change the design so that it is PCI-DSS compliant without putting the whole platform in audit scope. ```hint Count the services that see a card number Every service that touches a raw card number is in audit scope. Look for a way to make that set as small as possible. ``` #### What This Part Should Cover - Where the raw card number enters the system and where it is stored - What the rest of the platform handles in its place - How the real card number reaches the bank when a payment goes out ### Part 5 — Reliable merchant notifications After a payment changes state, the payment service writes the new state to the database and publishes an event that triggers the merchant's webhook. Explain what goes wrong if the service crashes between those two steps, and the technique and components that guarantee merchants always hear about a payment event. ```hint One commit The database and the message broker do not share a transaction. Think about what you can commit atomically in the database alone. ``` #### What This Part Should Cover - Why separate writes to the database and the broker can lose an event - How an event still reaches the broker after a crash - The delivery guarantee merchants get and how duplicates and failed deliveries are handled ### Part 6 — Automated reconciliation Explain the components involved in automated reconciliation and how it works: how the gateway confirms that the money it recorded actually moved, and what it does when the bank's records disagree with its own. ```hint Choose the matching key Think about which field pairs a bank record with a ledger row without ambiguity, and why timestamps and amounts are unreliable for this. ``` #### What This Part Should Cover - Where the bank's record of moved funds comes from and when it arrives - How records are matched and what state a matched payment moves to - How mismatches are handled ### What a Strong Answer Covers - A stated position on consistency versus availability for money movement, carried through the whole design - Safe retries end to end, from the client's request to the call to the bank - Raw card data confined to a small, isolated part of the system - No lost merchant notifications after a crash, with duplicates handled - Reconciliation against the bank's records, and a refund flow that stays correct under concurrent partial refunds ### Follow-up Questions - A call to the acquiring bank times out and you do not know whether the card was charged. What does the gateway do, and what does it tell the merchant? - How would you add a second acquiring bank and fail over to it when the first is slow or down? - The main ledger outgrows a single PostgreSQL instance. How would you shard it, and what changes for one merchant far larger than the rest? - A merchant's webhook endpoint is down for a day. How does delivery behave, and how does the merchant catch up?

Overview: Design a payment gateway that charges cards, issues full and partial refunds, reports payment status and notifies merchants of state changes through webhooks. It tests how you prevent double charges on retries, keep card data out of most of the system, deliver every notification after a crash and reconcile your records with the bank.

Nov 24, 2026
hardSoftware EngineerOtherSystem Design
0
0

Design a payment gateway similar to Stripe: the service merchants call to charge their customers, which in turn talks to acquiring banks and card networks on the merchants' behalf. The system handles payment state changes and notifies merchants of those changes.

The gateway must support these functional requirements:

  1. Clients can charge a customer's credit card or other payment method.
  2. Clients can issue full or partial refunds.
  3. The system notifies merchants asynchronously (webhooks) when a payment's state changes.
  4. Merchants can query the status of a specific transaction.

Propose the non-functional requirements yourself, then work through the six parts.

Clarifying Questions to Ask Guidance

  • Do merchants authorize and capture in one step, or can they authorize now and capture later (for example when goods ship)?
  • What transaction volume and peak checkout rate should the design handle?
  • Are payment methods other than cards in scope?

Part 1 — Requirements, core entities and API

State the non-functional requirements of a payment gateway and how you would rank them when they conflict. Then name the core entities you would store and sketch a REST API for creating a payment, reading its status and issuing a refund.

What This Part Should Cover Guidance

  • The non-functional requirements and which one wins when two conflict
  • Entities that track a payment's lifecycle, its audit trail and card data
  • An API with merchant authentication, a way to make retries safe, and amounts that cannot suffer rounding errors

Part 2 — High-level design

Lay out the high-level components the gateway needs. Focus on the overall architecture for now; the later parts fix its weak points.

What This Part Should Cover Guidance

  • The synchronous path from the merchant's request to the bank's approval or decline, including fraud checks and protocol translation
  • The asynchronous path that tells merchants about state changes
  • Which component holds the authoritative state of a payment

Part 3 — Preventing double charges

Network timeouts are inevitable, and a client that does not get a response will retry. Explain the technique and the components that make sure the system never charges a customer twice. Then explain how a refund stays correct when a merchant sends two partial refunds for the same payment at the same moment.

What This Part Should Cover Guidance

  • How the server recognizes a retry as the same attempt
  • What it returns for a retry that arrives while the first request is still running, and for one that arrives after it finished
  • What happens if the component that remembers attempts loses data, or if the call to the bank itself times out
  • How two concurrent partial refunds are kept from refunding more than was captured

Part 4 — PCI-DSS compliance

In the high-level design, raw card numbers pass through the API gateway and the payment service. Explain how to change the design so that it is PCI-DSS compliant without putting the whole platform in audit scope.

What This Part Should Cover Guidance

  • Where the raw card number enters the system and where it is stored
  • What the rest of the platform handles in its place
  • How the real card number reaches the bank when a payment goes out

Part 5 — Reliable merchant notifications

After a payment changes state, the payment service writes the new state to the database and publishes an event that triggers the merchant's webhook. Explain what goes wrong if the service crashes between those two steps, and the technique and components that guarantee merchants always hear about a payment event.

What This Part Should Cover Guidance

  • Why separate writes to the database and the broker can lose an event
  • How an event still reaches the broker after a crash
  • The delivery guarantee merchants get and how duplicates and failed deliveries are handled

Part 6 — Automated reconciliation

Explain the components involved in automated reconciliation and how it works: how the gateway confirms that the money it recorded actually moved, and what it does when the bank's records disagree with its own.

What This Part Should Cover Guidance

  • Where the bank's record of moved funds comes from and when it arrives
  • How records are matched and what state a matched payment moves to
  • How mismatches are handled

What a Strong Answer Covers Guidance

  • A stated position on consistency versus availability for money movement, carried through the whole design
  • Safe retries end to end, from the client's request to the call to the bank
  • Raw card data confined to a small, isolated part of the system
  • No lost merchant notifications after a crash, with duplicates handled
  • Reconciliation against the bank's records, and a refund flow that stays correct under concurrent partial refunds

Follow-up Questions Guidance

  • A call to the acquiring bank times out and you do not know whether the card was charged. What does the gateway do, and what does it tell the merchant?
  • How would you add a second acquiring bank and fail over to it when the first is slow or down?
  • The main ledger outgrows a single PostgreSQL instance. How would you shard it, and what changes for one merchant far larger than the rest?
  • A merchant's webhook endpoint is down for a day. How does delivery behave, and how does the merchant catch up?