**It isn't ready to build yet.** Steps 3–6, the core of the checkout flow, can charge a customer more than once, or take their money and keep no order. The fix changes where the database transaction starts and ends, so it needs to go into the design before anyone writes code. The rest of the design is sound and should stay as it is: separate modules with their own tables, thin routes, the `OrderCheckout` service class, and logging the order id on every line. A few hours of design work should be enough to fix it.

## Must fix before building

**1. Retrying the payment call can charge the customer twice.**
Step 4 retries the charge API after a 5-second timeout. A timeout doesn't mean the charge failed. It often means it succeeded and the response arrived late.
- *Case:* a customer checks out for $80. The first charge goes through at the provider, but the reply takes 6 s. The service gives up, retries, and the second charge succeeds.
- *Result:* the customer pays $160 for one order, or up to $320 if all three retries run.
- *Expected:* one $80 charge.
- *Fix:* send an idempotency key with every charge call, derived from the order id, so retries reuse it. Most payment providers support this. Check that yours does before you build.

**2. The database transaction stays open during the payment call, so a rollback can wipe out the record of a real charge.**
Step 3 opens the transaction, step 4 calls the provider, and step 6 rolls back on failure.
- *Case:* the charge succeeds, but all retries time out (or the process crashes before the commit). Step 6 rolls back.
- *Result:* the customer is charged and the `PENDING` order row is gone. Nothing in your system shows the charge happened.
- *Expected:* an order row exists that you can reconcile against the provider.
- *Also:* with retries, one checkout can hold a database connection for 20 seconds or more. A few slow payments at once will use up the connection pool on your single Postgres instance.
- *Fix:* commit the `PENDING` order first, call the provider outside any transaction, then update the order in a short second transaction. Add a background job that checks orders left in `PENDING` against the provider and moves them to `PAID` or `FAILED`.

**3. Nothing stops a client from submitting the same order twice.**
If the client retries `POST /orders` after a slow response (and a request can take 20 seconds), you get two orders and two charges.
- *Fix:* the client sends an idempotency key with `POST /orders`, and the server returns the existing order for a key it has already seen.

```mermaid
flowchart TD
    A[POST /orders with idempotency key] --> B[Check stock]
    B --> C[Transaction 1: insert order PENDING, commit]
    C --> D[Charge provider using order id as idempotency key]
    D -->|success| E[Transaction 2: mark PAID]
    D -->|declined| F[Transaction 2: mark FAILED]
    D -->|timeout or crash| G[Order stays PENDING]
    G --> H[Background job checks PENDING orders against provider]
    H --> E
    H --> F
    E --> I[Queue confirmation email]
```

## Decide before building

**4. Stock is never reserved, so the design will oversell.** Step 2 reads a cache that can be up to 5 minutes old, and no step in the flow reduces stock.
- *Case:* one unit of an item is left. Two customers check out within the same 5 minutes. Both pass the stock check, and both pay.
- *Result:* two paid orders for one unit.

Using the cache is a reasonable call, given the 800 ms warehouse call. But the design has to say what happens when an order oversells. Either reserve stock (decrement it locally or in the warehouse system) when the order is created, or accept overselling and write down how it gets handled: an automatic refund and an apology email. At 2,000 orders a day it will happen regularly.

**5. The test plan covers only the success path.** Your own standard is that failure cases count as much as the happy path. Add integration tests for:
- a payment timeout followed by a retry, asserting exactly one charge
- a crash after the charge but before the order is marked `PAID`, then recovery by the background job
- a duplicate `POST /orders` with the same key
- a payment decline
- an oversell, whichever policy you choose in point 4

## Worth stating in the design, but not blocking

- **Email (step 5):** if the email send fails after the commit, the order is paid and the customer is never told. Queue the email, or write it to a table in the same transaction that a worker then sends from (the "outbox" pattern), so it gets retried.
- **Backups:** with nightly backups only, a failure late in the day loses up to about 24 hours of paid orders. The payment provider will still have the charges, but you won't have the orders. Turn on continuous WAL archiving so you can restore to a point in time, or at least write down that you accept this risk.

VERDICT: NOT READY
