**The design is not ready to build as written: the payment steps (3–6) can charge a customer two or three times, and can take money while leaving no order record.** The fix is a rewrite of the Flow section, not of the whole design. The module layout, the thin routes and the single Postgres instance are right for 200–2,000 orders a day and should stay.

## Must fix before building

**1. Retrying the charge on timeout can charge the customer more than once.**
A timeout means "we don't know", not "it failed".
- Case: the provider takes the charge at 4.9 s and its response arrives at 5.1 s.
- Today's design: the service retries, and the customer is charged twice (up to four times with 3 retries).
- Expected: one charge.
- Fix: send the provider an idempotency key (the order id) on every attempt, so a retry returns the original result.

**2. Rolling back on failure deletes the only record that a charge was attempted.**
The `PENDING` row lives inside an open transaction, so nobody else ever sees it, and a rollback erases it.
- Case: all attempts time out, but one actually succeeded at the provider. Or the process crashes between the charge and the commit.
- Today's design: the customer is charged, the order row is gone, and there is nothing to find or refund against.
- Expected: an order row that says payment is unresolved.
- Fix: commit `PENDING` before calling the provider, call the provider outside any transaction, then update to `PAID` or `FAILED` in a second short transaction.
- This also stops a database connection being held for up to 20 s per checkout.

**3. Nothing resolves orders left in `PENDING`.**
Once fix 2 is in, a timeout or crash leaves a `PENDING` order. The design needs a small scheduled job (or the provider's webhook) that asks the provider for the outcome and settles the order.

**4. `POST /orders` has no protection against the client sending it twice.**
- Case: the user double-clicks, or the mobile app retries after a dropped connection.
- Today's design: two orders, two charges.
- Expected: one order, with the second request returning the first one's result.
- Fix: a client-supplied idempotency key with a unique constraint.

**5. Stock is checked but never reserved, and the oversell outcome is undecided.**
- Case: one unit left, two orders arrive within the 5-minute cache window.
- Today's design: both pass the check, both are charged, one cannot ship.
- Expected: not stated in the design.
- This needs a decision from you. Either decrement stock in your own database inside the order transaction, or accept overselling and write down the refund/backorder process.
- I recommend the decrement: it is one conditional `UPDATE`, and the 5-minute cache then only matters for restocks.

## Proposed flow

```mermaid
sequenceDiagram
    participant C as Client
    participant S as order-service
    participant DB as Postgres
    participant P as Payment provider
    C->>S: POST /orders (cart, idempotency key)
    S->>DB: Transaction 1: reserve stock, insert order PENDING, commit
    S->>P: Charge (key = order id), retry on timeout
    alt charged
        S->>DB: Transaction 2: mark PAID
        S->>C: 201 order confirmed
        S-->>C: confirmation email (after commit)
    else declined
        S->>DB: Transaction 2: mark FAILED, release stock
        S->>C: payment error
    else still unknown
        S->>C: 202 payment pending
        Note over S,P: scheduled job asks provider and settles the order
    end
```

## Fix before launch, not before coding

- **Tests.** One happy-path integration test misses every bug above. Add integration tests against a fake provider for: timeout then success, decline, crash after charge, duplicate request, and last-unit race.
- **Email.** A failed send must not fail or roll back a paid order. Send after commit and retry from a `confirmation_sent` flag.
- **Backups.** Nightly backups mean a disk failure can lose up to 24 hours of paid orders. Turn on continuous WAL archiving (point-in-time recovery).

## What to do tomorrow

Rewriting the Flow section and making the stock decision is about half a day. The module skeleton, the `OrderCheckout` class shell and the fake payment provider for tests don't depend on those answers and can start in the morning.

VERDICT: NOT READY
