I don't think you should build this flow as written. The payment steps (3 to 6) can charge a customer twice, or charge them and leave no record of the order. Both are fixable with design changes you probably know already, so this is a few hours of rework on the doc, not a restart. The module layout (separate modules, thin routes, the `OrderCheckout` class, logging) is fine and you could start on it tomorrow.

## Blocking: the payment flow loses or duplicates money

**1. Retrying the charge after a timeout can charge the customer twice.**
A 5 s timeout doesn't mean the charge failed. Often the provider charged the card and the response just arrived late.
- Example: the first charge call times out, but the provider did charge $80. The service retries, and the retry also succeeds. The customer pays $160 for one order instead of $80.
- Fix: send an idempotency key with every charge attempt. Use the order id, so all retries count as one charge. Check that your provider supports this; most do. If a retry is still uncertain, look up the charge status before calling it a failure.

**2. Because the charge runs inside the open database transaction, a customer can be charged with no order saved.**
The `PENDING` row only exists inside the uncommitted transaction. Nothing outside can see it, and a rollback erases it.
- Example: the charge succeeds, then the commit fails (a lost connection, a deploy restarting the process, or a crash between steps 4 and 5). The customer is charged and your database has no order. Nothing exists that anyone could use to reconcile or refund it.
- Example: step 6 rolls back on "failure". If that failure was really an uncertain timeout, you have deleted the only record of a charge that may have gone through.
- The open transaction also holds a database connection during up to 4 × 5 s of payment calls. That's harmless at 200 orders a day but it's the wrong pattern to build on.
- Fix: commit `PENDING` in its own short transaction first. Call the provider with no transaction open. Then mark the order `PAID` or `FAILED`, or `PAYMENT_UNKNOWN` if the outcome is uncertain. Add a small job that reconciles stale `PENDING` and `PAYMENT_UNKNOWN` orders against the provider.

**3. If the client sends `POST /orders` twice, it gets two orders and two charges.**
Retries of up to about 20 s are exactly when clients give up and retry, and when users double-click. Have the client send an idempotency key with `POST /orders`, and store it with a unique constraint.

```mermaid
sequenceDiagram
    participant C as Client
    participant S as OrderCheckout
    participant DB as Postgres
    participant P as Payment provider
    C->>S: POST /orders (idempotency key)
    S->>DB: insert PENDING, commit
    S->>P: charge (key = order id), retry safely
    alt success
        S->>DB: mark PAID
        S-->>C: 201
    else declined
        S->>DB: mark FAILED
        S-->>C: error
    else still unknown
        S->>DB: mark PAYMENT_UNKNOWN
        S-->>C: 202 (processing)
    end
    Note over DB,P: Reconcile job resolves stale PENDING / UNKNOWN
```

## Decide before building, but not blocking

**4. Overselling: stock is never reserved or decremented.** With a cache up to 5 minutes old and no reservation, two customers can both buy the last unit. At 2,000 orders a day this will happen if any items have low stock. Using the cache may be fine, but write down the decision: what happens to an order that can't be fulfilled (auto-refund, backorder, or manual handling)? Who owns that?

**5. Email failure isn't specified.** If sending the email throws after the commit, does the client see an error for an order that succeeded? Send the email after responding, ideally from a queue or outbox table, and make sure it can never fail the order.

**6. Nightly backups mean up to 24 hours of paid orders can be lost.** You'd have taken customers' money with no record of it. Turning on WAL archiving or point-in-time recovery (or using managed Postgres with it built in) is cheap insurance.

**7. The test plan covers only the case that's least likely to break.** One integration test for the happy path skips the cases above. Add integration tests for these:
- a charge timeout followed by a successful retry, which must charge only once
- a commit failure after a successful charge
- a duplicate `POST /orders`
- a payment decline
- an email failure

Under your own TDD standard, these tests are what make the redesigned flow safe to build.

## Not a problem

The scale is low. You don't need queues for the main flow, sharding, or more services. The module boundaries and the "no module reads another's tables" rule are good.

**What to do next:** rewrite steps 3 to 6 along the lines of the diagram, add idempotency keys at both layers, write down the overselling decision, and expand the test plan. After that I'd expect the doc to be ready.

VERDICT: NOT READY
