The design is not safe to build as written, because steps 3 to 6 can charge a customer twice or charge them with no order on record. Both are fixed by rewriting those four steps; the code structure, the single Postgres instance and the overall scope are right for 200 to 2,000 orders a day and need no change.

## Must fix before coding the payment step

**1. Retrying on timeout can double-charge.** A timeout means "no answer", not "the charge failed".
- **Case:** the provider completes the charge at 5.1 s, the service gives up at 5 s and retries, and the provider charges again.
- **Expected:** one charge.
- **Fix:** send an idempotency key derived from the order id on every charge attempt, so the provider treats retries as the same charge.

**2. A rollback erases the only record of an order that may have been paid.** The `PENDING` insert sits inside a transaction that stays open during the payment call.
- **Case:** the charge succeeds, then the commit fails or the process crashes (or all attempts time out while one actually went through). The rollback removes the order row.
- **Expected:** an order row that says payment is unresolved, so someone or something can settle it.
- **Fix:** commit `PENDING` first, call the provider outside any transaction, then update the status in a second short transaction. Add a `FAILED` status and a `PAYMENT_UNKNOWN` status for exhausted timeouts.
- **Side benefit:** the current design holds a database connection for up to 20 s per order (four attempts at 5 s); this removes that.

**3. Nothing settles orders stuck in `PENDING` or `PAYMENT_UNKNOWN`.** Once fix 2 is in, these rows exist and need an owner. A small scheduled job that asks the provider for the charge status and marks the order `PAID` or `FAILED` is enough at this scale.

```mermaid
flowchart TD
    A[POST /orders] --> B[Check stock]
    B --> C[Insert order as PENDING and commit]
    C --> D[Charge with idempotency key, retry on timeout]
    D -->|charged| E[Mark PAID, commit]
    D -->|declined| F[Mark FAILED, return error]
    D -->|all attempts timed out| G[Mark PAYMENT_UNKNOWN]
    E --> H[Send confirmation email]
    G --> I[Scheduled job asks provider, sets PAID or FAILED]
```

## Decide before building, smaller in size

- **Overselling.** The stock check reserves nothing and the cache can be 5 minutes stale. Two customers buying the last unit within the same window both pass the check and both get charged. Either decrement a local stock count in the same transaction as the `PENDING` insert, or accept overselling and write down the refund or backorder path.
- **Client retries of `POST /orders`.** The request can now take up to 20 s, so clients will time out and resend, creating a second order and a second charge. Accept a client-supplied idempotency key and return the existing order on a repeat.
- **Email failure.** State that a failed confirmation email does not fail the order, and how it is retried; a logged failure plus a resend from the scheduled job would do.
- **Test plan.** One happy-path integration test leaves every money-losing path untested. Add integration tests for:
  - timeout followed by success
  - decline
  - all attempts timing out
  - crash between charge and status update
  - duplicate `POST /orders`
  - two orders for the last unit

## What you can start tomorrow

The module layout, the thin routes, the `OrderCheckout` class, logging and the inventory cache can all be built as designed. Only the payment step waits on the edits above.

VERDICT: READY WITH CHANGES
