**Not ready as written: steps 3–6 of the flow can charge a customer twice, or charge them and leave no order record.** The code structure section holds up, and the fix is a rewrite of that one section rather than a new architecture, so starting tomorrow is still realistic if you rewrite the flow today.

## Blocking: fix before building

**1. Retrying the charge on timeout can double-charge.**
A timeout means "we don't know", not "it failed". For example, the provider completes the charge at 5.2 s, we gave up at 5.0 s and retry, and the customer is charged twice for one order.
- **Fix:** send an idempotency key (the order id) on every charge attempt, so the provider treats retries as the same charge.
- **To check:** confirm that your provider supports idempotency keys; the design does not name the provider.

**2. Rolling back on failure erases the only evidence that we took money.**
The `PENDING` row lives inside an open transaction, so it is never visible to anything else and disappears on rollback. For example, the charge succeeds, then the process crashes or the commit fails. The customer is charged, there is no order row, and nothing in our database lets us find or refund it.

The same happens when all three retries time out but one of them actually went through. The expected result is an order row that survives, in a state that says "payment outcome unknown".
- **Fix:** commit the `PENDING` order first, call the provider outside any transaction, then record the result in a second short transaction.
- **Fix:** add explicit `FAILED` and "unknown" outcomes, plus a small reconciliation job that asks the provider about orders stuck in `PENDING`.

This also stops a database connection being held open for up to 15 s while the provider is slow.

**3. `POST /orders` itself is not idempotent.**
The worst-case response time is over 15 s (3 × 5 s timeouts). For example, the client or load balancer gives up at 10 s, the user taps "Pay" again, and we create two orders and two charges.
- **Fix:** have the client send an idempotency key with a unique constraint on it, and return the existing order on a repeat.

**4. Stock is checked but never reserved.**
For example, with one unit left, two customers check out within the same 5-minute cache window. Both pass the check, both pay, and one order cannot be fulfilled. The stale cache is not the main cause; this happens even with a fresh cache, because nothing decrements on order.

At 200 orders a day you can reasonably accept this, but the design has to say which option you chose:
- **Reserve:** decrement a local stock count in the same transaction as the order insert.
- **Accept overselling:** define what happens to the losing order (automatic refund plus an apology email, or a backorder).

**5. The test plan covers only the path where nothing goes wrong.**
One happy-path integration test leaves every case above untested. Add integration tests, against a fake payment provider, for:
- a decline
- a timeout followed by success
- timeouts on all attempts
- a crash between charge and commit
- a duplicate `POST`
- two orders for the last unit

## Proposed flow

```mermaid
flowchart TD
    A[POST /orders with idempotency key] --> B{Key seen before?}
    B -- yes --> C[Return existing order]
    B -- no --> D[Check and reserve stock]
    D --> E[Insert order as PENDING and commit]
    E --> F[Charge provider, order id as idempotency key, retry on timeout]
    F -- charged --> G[Mark PAID and queue confirmation email, commit]
    F -- declined --> H[Mark FAILED, release stock, return error]
    F -- still unknown --> I[Leave PENDING, tell client payment is being confirmed]
    I --> J[Reconciliation job asks provider, then marks PAID or FAILED]
```

## Not blocking, but decide and write down

- **Confirmation email:** it is sent after commit with no retry, so a mail outage silently loses it. Queue it in the same transaction as `PAID` (an outbox table and a small sender job) so it can be retried.
- **Failure types:** the design covers timeouts only. Say that a decline is never retried and that a provider 5xx is retried with the same idempotency key.
- **Backups:** nightly backups mean a disk failure can lose up to a day of paid orders. Turning on continuous WAL archiving (point-in-time recovery) is cheap. At minimum, note that the provider's records are the fallback source.

## What is right and should stay

- One service, one Postgres instance, and no queues or sagas is the correct size for 2,000 orders a day and a team of four.
- Module boundaries with no cross-module table reads, thin routes, an `OrderCheckout` service class and order-id logging are all sound.
- Using the cache instead of the 800 ms warehouse call is fine once item 4 is settled.

VERDICT: NOT READY
