The design is not safe to build exactly as written, but it is close: the module layout and scope are right, and three changes to the payment steps (4–6) fix the cases where a customer can be charged twice or charged with no order on record. All three are small enough to settle today and still start tomorrow.

## What is already good
- **Sized for the load.** One service, one Postgres and no queues is the correct call for 2,000 orders a day and a team of four.
- **Clean module boundaries.** "No module reads another module's tables" plus thin routes and an `OrderCheckout` service will make the fixes below easy to slot in.
- **Order id on every log line.** This is what you will need when debugging payment problems.

## Must change before building the payment path

**1. A retry after a timeout can charge the customer twice.**
A timeout means "no answer," not "the charge failed."
- **Case:** the provider completes a $50 charge in 5.2 s. The service gave up at 5 s and retries, and the retry also succeeds.
- **Result:** the customer is charged $100.
- **Expected:** $50.
- **Fix:** send an idempotency key (the order id) with every charge attempt, so the provider treats retries as the same charge.

**2. The rollback erases the only record of an order that may have been paid.**
The `PENDING` row lives inside the open transaction, so nobody else can see it and the rollback deletes it.
- **Case:** all attempts time out, but the first one actually went through at the provider. The service rolls back and returns an error.
- **Result:** the customer is charged, there is no order row, and nothing is left to reconcile against. The same happens if the service crashes between the charge and the commit.
- **Expected:** an order that stays visible as `PENDING` until its payment outcome is known.
- **Fix:** commit `PENDING` first, call the provider outside any transaction, then record `PAID` or `FAILED` in a second short transaction. Add a small job that asks the provider about orders stuck in `PENDING`.

This also stops a database connection being held open for up to 20 seconds during retries.

**3. Nothing stops two people buying the last unit.**
The stock check reads a cache up to 5 minutes old, and the flow never reserves or decrements stock.
- **Case:** one unit is left, and two customers check out a minute apart.
- **Result:** both orders pass the check and both are charged.
- **Expected:** the second is rejected, or you have a stated policy for oversold orders.
- **Fix:** accepting the cache is reasonable. Either decrement a local stock count in the same transaction as the `PENDING` insert, or write down the oversell policy (refund and apologise) as a deliberate choice.

Revised flow:

```mermaid
sequenceDiagram
    participant C as Client
    participant S as OrderCheckout
    participant DB as Postgres
    participant P as Payment provider
    C->>S: POST /orders (cart)
    S->>DB: check stock, reserve, insert PENDING, commit
    S->>P: charge (idempotency key = order id), retry on timeout
    alt charge succeeded
        S->>DB: mark PAID, commit
        S->>C: success, then send email
    else charge declined
        S->>DB: mark FAILED, release stock, commit
        S->>C: error
    else outcome unknown
        S->>C: "payment processing"
        Note over S,P: reconciliation job resolves PENDING later
    end
```

## Should add, but does not block starting
- **Duplicate submissions.** A double-click or client retry on `POST /orders` creates two orders. Accept a client-supplied idempotency key on the endpoint.
- **Failure-path tests.** One happy-path integration test leaves the risky paths uncovered. Add integration tests for timeout then success, decline, timeout on every attempt, and duplicate submission.
- **Confirmation email.** If the send fails after commit, it is lost. Record "email sent" on the order and retry.
- **Backups.** Nightly backups mean a disk failure can lose up to a day of paid orders. Turn on continuous WAL archiving (point-in-time recovery) before real money flows.

Tomorrow you can start on the modules, routes, the order model and the inventory cache unchanged. Build steps 4–6 to the revised flow rather than the one in the document.

VERDICT: READY WITH CHANGES
