**The design is close, but steps 3–6 can charge a customer twice or charge them with no order on record, so those need to change before you write the checkout code.** The fixes are small edits to the doc, not a rethink, and the module layout can be built tomorrow as written.

## Fix before building

**1. Retrying on timeout can double-charge.**
A timeout means "we don't know", not "the charge failed".
- **Case:** the provider takes 6 s and succeeds, the service gives up at 5 s and retries, and the second charge also succeeds.
- **Result:** the customer is charged twice for one order; the expected result is one charge.
- **Fix:** send an idempotency key (the order id) on every charge attempt, so the provider treats retries as the same charge.

**2. Rolling back on failure erases the only record of a charge.**
The `PENDING` row lives in an uncommitted transaction while the payment call is made.
- **Case:** the charge succeeds, then the service crashes or the commit fails.
- **Result:** the customer is charged and the database has no order row at all; the expected result is an order you can find and either complete or refund.
- **Fix:** commit the `PENDING` order first, charge outside any transaction, then update to `PAID` or `FAILED` in a second short transaction.
- **Also needed:** a small periodic job that asks the provider about orders stuck in `PENDING` and resolves them.
- **Side benefit:** the current design holds a database connection open for up to 15–20 s of payment retries, and this removes that.

**3. `POST /orders` has no protection against a client resend.**
- **Case:** the user double-clicks, or the mobile client retries after a dropped response.
- **Result:** two orders and two charges; the expected result is one.
- **Fix:** accept a client-supplied idempotency key and store it with a unique constraint.

**4. The test plan covers only the happy path.**
The bugs above all live in failure paths. Add integration tests with a fake payment provider for:
- a declined charge
- a timeout followed by success
- a timeout on every attempt
- a crash between charge and commit
- a duplicate `POST`

## Revised flow

```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[Call charge API with order id as idempotency key, retry on timeout]
    F -- success --> G[Mark PAID, commit, send confirmation email]
    F -- declined --> H[Mark FAILED, commit, return error]
    F -- still unknown --> I[Leave PENDING, reconciliation job resolves it]
```

## Decide, but need not block tomorrow

- **Overselling:** the stock check reads a cache up to 5 minutes old and nothing reserves the item, so two orders can both take the last unit. At 200 orders a day, accepting this is reasonable, but the doc should say what happens then (refund, backorder) and who is told.
- **Lost confirmation email:** if the send fails after commit, nothing retries it. A retry or a "confirmation sent" flag on the order is enough.
- **Backups:** nightly backups mean a disk failure can lose up to a day of paid orders. Continuous WAL archiving on Postgres narrows that to minutes.

## What is already right

- The module boundaries, with no module reading another's tables.
- Thin routes with the flow in `OrderCheckout`.
- The order id on every log line.
- Accepting the cache over an 800 ms warehouse call.
- A single Postgres instance, which is the right size for 2,000 orders a day.

VERDICT: READY WITH CHANGES
