The design isn't ready to build from yet. The module layout and code-structure plan are sound. The problem is steps 3–6, the core checkout sequence: as written, it can charge a customer twice, or take their money and keep no order record. Both are fixable, and the fixes change the order statuses and the shape of `OrderCheckout`. So update the doc before you write code, or tomorrow's code will build in the bug.

## Must fix before building

**1. Retrying the charge can charge the customer twice (step 4).** A timeout doesn't mean the charge failed. It only means you didn't hear back in time.
- **Case:** the provider charges £50 but its reply takes 6 s. Your 5 s timeout fires and you retry, and the provider charges another £50.
- **Wrong result:** £100 taken. **Expected:** £50 taken.
- **Fix:** send an idempotency key with every charge call (the order id works), so the provider treats a retry as the same charge. Check that your provider supports this. If it doesn't, don't retry automatically.

**2. Calling the payment provider inside the database transaction can lose the order after the money is taken (steps 3–6).**
- **Case:** the charge succeeds, then the commit fails or the process crashes before committing.
- **Wrong result:** the transaction rolls back, so the order row disappears even though the customer has paid. **Expected:** an order record that shows they paid.
- The same structure also means every payment failure deletes the `PENDING` row, so you lose the record of the attempt. And with retries, one request can hold a database connection open for 20 s or more.
- **Fix:** commit the order as `PENDING` first, charge outside any transaction, then update the order in a second short transaction. Add a scheduled job that finds orders stuck in `PENDING` and checks them against the provider.

**3. Running out of retries doesn't mean the payment was declined (step 6).** After three timeouts you don't know whether the customer was charged. Telling them "error" invites them to pay again. Add a separate status for an unknown payment outcome, show the customer a "we're confirming your payment" message, and let the scheduled job from fix 2 settle it.

**4. Duplicate submissions create duplicate orders (step 1).** A double-click, or a client that gives up during a 20 s retry loop and resends, creates a second order and a second charge. The idempotency key in fix 1 doesn't help here, because each new order has its own id. Have the client send its own idempotency key on `POST /orders`, and return the existing order if that key has been seen before.

**5. Stock is checked but never reserved (step 2).** Nothing in the flow reduces stock.
- **Case:** one unit is left. Two customers check out within the same 5-minute cache window.
- **Wrong result:** both orders are paid. **Expected:** one order is paid and the other is told the item is out of stock.
- At 2,000 orders a day, accepting some overselling may be reasonable, but it's a product decision. Write down whether you'll oversell and refund, or reserve stock with the warehouse.

## Corrected flow

```mermaid
flowchart TD
    A[POST /orders with client idempotency key] --> B{Key seen before?}
    B -- yes --> R[Return existing order]
    B -- no --> C[Check stock in inventory cache]
    C --> D[Insert order as PENDING and commit]
    D --> E[Charge provider using order id as idempotency key, retry on timeout]
    E -- success --> F[Mark PAID, commit, queue confirmation email]
    E -- declined --> G[Mark FAILED, return error]
    E -- still timing out --> H[Mark PAYMENT_UNKNOWN, tell customer we're confirming]
    H --> J[Scheduled job checks provider and settles PENDING or PAYMENT_UNKNOWN orders]
```

## Should fix (smaller items)
- **Email:** say what happens if sending the confirmation email fails. It shouldn't fail an order that is already paid. Queue the email after the commit instead of sending it inside the request.
- **Tests:** one integration test for the happy path isn't enough for a payment flow. Use a fake payment provider and add tests for:
  - a timeout followed by success (only one charge)
  - a declined payment
  - a crash after the charge but before the update
  - a duplicate `POST /orders`
  - the scheduled job settling a stuck order
- **Backups:** nightly backups mean up to 24 hours of paid orders could be lost. The provider still has the payment records, so you could rebuild them, but write down that this is an accepted risk.

## What's good
Separate modules that don't read each other's tables, thin routes, logging with the order id on every line, and growth estimates sized to the real load. Keep all of that as it is.

Rewriting steps 3–6 and adding the stock decision is about an hour of design work, so tomorrow's start date is still realistic if you update the doc first.

VERDICT: NOT READY
