**design.md is not safe to build as written, but the fixes are confined to steps 2–6 of the flow and can be made today.** The module layout and the single Postgres instance are fine for 2,000 orders a day. The payment step can charge a customer more than once, or charge them and leave no order record.

## Must fix before writing the payment code

**1. Retrying on timeout can charge the customer several times (step 4).**
A timeout means "no answer", not "the charge failed".
- Case: the provider takes 6 s to process a £50 charge. The service gives up at 5 s and retries, and both charges succeed.
- Wrong outcome: the customer pays £100 for one order, and up to £200 if all four attempts land.
- Expected: one £50 charge.
- Fix: send the provider an idempotency key derived from the order id on every attempt, so repeats are recognised as the same charge.

**2. A rollback can erase the only record of a charged order (steps 3–6).**
The `PENDING` insert sits in the same transaction as the charge call, so any failure after the charge deletes the order row.
- Case: the charge succeeds, then the service crashes or the commit fails. The same happens if all attempts time out but one actually went through.
- Wrong outcome: the customer is charged, no order row exists, and there is nothing to refund or reconcile against.
- Expected: an order row that says payment state is unknown, which a job can resolve.
- Fix: commit `PENDING` first, call the provider outside any transaction, then record the result in a second short transaction. Add a small job that asks the provider about orders stuck in `PENDING`.
- Side benefit: the current design holds a database transaction open for up to 20 s (four attempts at 5 s each), which this removes.

```mermaid
flowchart TD
    A[POST /orders with idempotency key] --> B[Check stock]
    B --> C[Insert order as PENDING and commit]
    C --> D[Charge via payment provider, idempotency key = order id]
    D -->|success| E[Mark PAID and commit]
    D -->|declined| F[Mark FAILED and commit, return error]
    D -->|timeout after retries| G[Leave PENDING, return 'payment being confirmed']
    E --> H[Send confirmation email]
    G --> I[Reconciliation job asks provider, marks PAID or FAILED]
```

**3. `POST /orders` itself can create duplicate orders (step 1).**
- Case: the request takes 12 s because of payment retries, the client times out at 10 s, and the user presses "Pay" again.
- Wrong outcome: two orders and two charges.
- Expected: the second request returns the first order.
- Fix: the client sends an idempotency key, with a unique constraint on it.

## Decide before building, smaller in size

- **Stock is never reserved or decremented (step 2).** Two customers buying the last unit within the same 5-minute cache window both pass the check and both get charged. Either decrement a local count in the order transaction, or accept overselling and write down the refund or backorder path.
- **A failed email should not fail the order (step 5).** State that sending is best-effort or retried, and that it happens after the commit.
- **The test plan covers only the happy path.** Add integration tests for: timeout then success, decline, crash between charge and commit, and a duplicate `POST`.
- **Nightly backups can lose up to a day of paid orders.** Continuous WAL archiving for point-in-time recovery closes that gap; decide whether it goes in now or is logged as known debt.

VERDICT: READY WITH CHANGES
