**The design is not safe to build as written, but the fixes are small and standard: the code structure can stand, and steps 3 to 6 of the flow need to be rewritten before anyone writes payment code.** There are two ways it takes a customer's money incorrectly, and both come from how the charge call sits inside the database transaction.

## Must fix before building

**1. A timeout retry can charge the customer twice.**
A timeout means "we don't know", not "it failed". For example, the provider completes a charge in 5.1 s, the service gives up at 5 s and retries, and the provider charges again. The customer pays twice for one order when they should pay once.
- **Fix:** send an idempotency key (the order id) with every charge attempt, so the provider treats retries as the same charge.
- **To check:** confirm your provider supports idempotency keys; nearly all do.

**2. A customer can be charged and end up with no order.**
The `PENDING` row lives inside an uncommitted transaction, so a rollback erases all trace of it. There are two concrete cases:
- All attempts time out, but one actually succeeded at the provider. Step 6 rolls back and returns an error, so the money is taken and no order row exists.
- The charge succeeds and the service crashes before the commit, with the same result.

**Fix:** commit `PENDING` before calling the provider, then update the status in a second short transaction. An order whose outcome is unknown stays `PENDING`, and a small reconciliation job asks the provider what happened and settles it.

**3. The transaction stays open across the charge call.**
Each checkout pins a database connection for up to 20 s (four attempts at 5 s each) in the worst case. A slow spell at the provider will exhaust the connection pool and take down every endpoint, not just checkout. The fix for item 2 removes this too.

**4. Nothing stops a duplicate `POST /orders`.**
A double-click or a mobile client retry creates two orders and two charges. Accept a client-generated idempotency key and put a unique constraint on it.

**5. Stock is checked but never reserved or decremented.**
Two customers can buy the last unit within the same 5-minute cache window and both pass the check. The 5-minute cache is a reasonable call at this volume, but it needs one of two additions:
- a local decrement on the order, in the same transaction as the `PENDING` insert; or
- a written decision on what happens to an oversold order (refund, backorder, who gets told).

The second is a business decision and the design is silent on it.

**6. The test plan covers only the case that already works.**
One happy-path integration test leaves every item above untested. Add integration tests, against a fake provider, for at least:
- timeout, then success on retry: charged once;
- all attempts time out: order stays `PENDING`, then is reconciled;
- provider decline: order is `FAILED`, no charge;
- duplicate `POST`: one order;
- crash between charge and status update: reconciled to `PAID`.

## 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, reserve it, insert order as PENDING, commit]
    D --> E[Call charge API, order id as idempotency key, retry on timeout]
    E -- charged --> F[Mark PAID, commit]
    E -- declined --> G[Mark FAILED, release stock, return error]
    E -- still unknown --> H[Leave PENDING, tell client payment is being confirmed]
    H --> I[Reconciliation job asks provider, marks PAID or FAILED]
    F --> J[Send confirmation email]
    I -- PAID --> J
```

## Should fix, but not blocking

- **Email:** step 5 doesn't say what happens if the send fails. Send it after the commit and never let a failed send fail the order. A `confirmation_sent` flag and a retry are enough; you don't need a queue.
- **Backups:** nightly backups mean losing up to a day of paid orders if the instance dies. Turning on point-in-time recovery (WAL archiving) is cheap. A replica is not needed at this scale.
- **Order states:** write down the allowed statuses and transitions (`PENDING`, `PAID`, `FAILED`) so the reconciliation job and the checkout path agree.

## What is right

- The module boundaries, thin routes, the single `OrderCheckout` service and order-id logging are right.
- A single Postgres instance and a synchronous flow suit 2,000 orders a day; don't add a queue or split services.

Update the design with items 1 to 6 first, about an hour of writing, and you can start building tomorrow.

VERDICT: READY WITH CHANGES
