**The design is not ready to build as written: the checkout flow in steps 3–6 can charge a customer more than once, or charge them and keep no record of the order.** The code structure section is sound, and the fix is a rewrite of the Flow section rather than a new architecture, roughly half a day of design work.

## Blocking problems

**1. Retrying on timeout can double-charge (step 4).**
A timeout means "we don't know", not "it failed".
- Case: the provider takes 6 s to process the charge. The service gives up at 5 s and retries, and the retry also succeeds.
- Result: the customer is charged twice for one order. Expected: one charge.
- Fix: send an idempotency key (the order id) on every charge attempt. Confirm that your provider supports this before building.

**2. Rollback after a charge erases the only record of it (steps 3, 6).**
The `PENDING` row lives inside an uncommitted transaction, so it never exists for anyone else and vanishes on rollback.
- Case: all four attempts time out, but the first one actually went through at the provider. The service rolls back and returns an error.
- Result: the customer is charged, there is no order row, and nothing to refund against. Expected: an order in a known state that someone can resolve.
- The same happens if the process crashes or the commit fails after a successful charge.
- Fix: commit `PENDING` before calling the provider, then update the status in a second short transaction. Add an "unknown" outcome and a small job that asks the provider what happened to stale `PENDING` orders.

**3. The transaction is held open for up to 20 s (steps 3–5).**
Four attempts at 5 s each hold a database connection the whole time. A slow provider can then exhaust the connection pool and take down every endpoint, not just checkout. The fix for problem 2 also fixes this.

**4. `POST /orders` has no protection against duplicate submission (step 1).**
- Case: the client's request times out on a slow mobile network and the app retries, or the user double-clicks.
- Result: two orders and two charges. Expected: one.
- Fix: the client sends an idempotency key, with a unique constraint on it; a repeat returns the original order.

**5. Nothing reserves or decrements stock (step 2).**
The 5-minute cache is fine for an early check, but the flow never reduces stock.
- Case: one unit is left and two customers check out within the same 5 minutes. Both pass the check and both are charged.
- Fix: either keep a reservation count in your own database, decremented in the same transaction as the order insert, or explicitly accept overselling and write down what happens next (refund, backorder, who is notified). The design currently takes neither position.

## Should fix, not blocking

- **Tests cover only the happy path.** Every problem above is a failure path. Add integration tests for: timeout then success, all timeouts, decline, duplicate submit, and a crash between charge and status update.
- **Email failure is undefined (step 5).** If the send fails after commit, does the client get an error for a paid order? Send it outside the request, with retry.
- **Nightly backups mean up to 24 hours of paid orders can be lost.** Turn on continuous archiving (point-in-time recovery) for a system that holds payment records.
- **Declines and timeouts are not distinguished in step 6.** A decline is final; a timeout is not. They need different statuses and different client responses.

## What is fine

Single Postgres, no queue, and the cached stock check are all appropriate for 2,000 orders a day. The module boundaries, thin routes and order id on every log line are good; keep them.

## Proposed flow

```mermaid
flowchart TD
    A[POST /orders with idempotency key] --> B{Key seen before?}
    B -- yes --> C[Return original order]
    B -- no --> D[Check stock, reserve it, insert order PENDING, commit]
    D --> E[Charge provider, order id as idempotency key, retry on timeout]
    E -- success --> F[Mark PAID, queue confirmation email]
    E -- declined --> G[Mark FAILED, release stock, return error]
    E -- still unknown --> H[Leave PENDING, tell client payment is being confirmed]
    H --> I[Background job asks provider, then marks PAID or FAILED]
```

## Decisions you need to make before building

1. Does the payment provider support idempotency keys and a "look up charge by key" call? Problems 1 and 2 depend on it.
2. Reserve stock or accept overselling, and if overselling, what the customer experience is.
3. What the client shows when the payment outcome is unknown.

If you settle these three and rewrite the Flow section tonight, building can start tomorrow, beginning with the modules and the failure-path tests.

VERDICT: NOT READY
