The design is not safe to build exactly as written, because steps 3 to 6 can charge a customer twice or charge them and leave no order record. The fix is contained to the Flow section; the code structure and the single-Postgres setup are fine for 2,000 orders a day.

## Must change before writing the payment code

**1. Retrying the charge on timeout can double-charge (step 4).**
A timeout means "no answer", not "not charged". For example, the provider charges the card in 6 s, the service gives up at 5 s and retries, and the customer is charged twice for one order.
- **Fix:** send an idempotency key (the order id) with every charge attempt, so the provider treats retries as the same charge.
- **To check today:** confirm your provider supports idempotency keys; nearly all do.

**2. Rolling back erases the only record of an order that may have been charged (steps 3 and 6).**
The `PENDING` row lives inside a transaction that is still open during the charge. For example, all three attempts time out but the first one actually succeeded: the service rolls back, the client gets an error, and the customer is charged with no order in the database. A crash between the charge and the commit does the same.
- **Fix:** commit the `PENDING` order first, call the provider outside any transaction, then update the order to `PAID` or `FAILED` in a second short transaction.
- **Add a state for "outcome unknown":** when timeouts are exhausted, mark the order (for example `PAYMENT_UNKNOWN`) and have a small job ask the provider what happened.
- **Side benefit:** no database connection is held for up to 15 s per checkout.

**3. `POST /orders` has no protection against the client retrying.**
For example, a user double-taps "Pay" or the mobile app retries on a flaky network: two orders, two charges.
- **Fix:** the client sends an idempotency key per checkout attempt, with a unique constraint on it; a repeat returns the existing order.

**4. Only the happy path has an integration test.**
The failures above are where money is lost. Add integration tests against a fake provider for: a decline, a timeout followed by success, timeouts exhausted, and a duplicate `POST`.

```mermaid
flowchart TD
    A[POST /orders with idempotency key] --> B{Key seen before?}
    B -- yes --> C[Return existing order]
    B -- no --> D[Check stock in inventory cache]
    D --> E[Insert order as PENDING and commit]
    E --> F[Charge provider, order id as idempotency key, retry timeouts up to 3 times]
    F -- success --> G[Mark PAID, commit, send email]
    F -- declined --> H[Mark FAILED, return error]
    F -- still timing out --> I[Mark PAYMENT_UNKNOWN, reconcile with provider later]
```

## Decide, but does not block starting

- **Overselling:** step 2 checks stock but nothing reserves it, and the cache can be 5 minutes stale. Two customers can both pay for the last unit. At 200 orders a day this may be acceptable, but the design should say what happens then (refund or backorder) and who decrements stock.
- **Confirmation email:** if the send fails after commit, it is lost. A failed-send log and a resend path are enough at this scale.
- **Backups:** nightly backups on a single instance mean a failure can lose up to a day of paid orders. Turning on point-in-time recovery closes that gap.

You can start tomorrow on the modules, routes and order model while the Flow section is amended; do not build steps 3 to 6 as currently written.

VERDICT: READY WITH CHANGES
