Test an Order Service With a Fake Payment Gateway
Last updated: November 29, 2026
Quick Overview
Design an order service that charges customers through an external payment gateway, and write unit tests using a fake gateway that cover duplicate submits, timeouts, and declines.
0
0
0 solved
Design an order service that charges customers through an external payment gateway, and write unit tests using a fake gateway that cover duplicate submits, timeouts, and declines.
Google's testing culture prefers fakes with real behavior over brittle mocks that assert call sequences. This question checks dependency injection, idempotency in payment flows, and whether the tests catch the bugs that matter, like double charging after a timeout.
What the Interviewer Expects
- Injects the gateway through an interface
- Builds a fake with realistic behavior rather than strict mocks
- Uses idempotency keys to prevent double charges
- Tests retry after an ambiguous timeout
- Explains when a mock is still the better tool
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- What does the fake not catch that an integration test would?
- How do you keep the fake in sync with the real gateway?
- How would you test concurrency between two submits of the same order?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
place_order(order_id, account, cents)charges once and marks the order PAID.- Re-submitting a paid order does not charge again.
- A timeout is ambiguous (the charge may have happened): retrying m...
High-Level Design
OrderService takes a PaymentGateway protocol in its constructor. Production passes the real client; tests pass FakeGateway, which tracks balances, honors idempotency keys, and can be told to fai...