**The design is mostly sound and you can start tomorrow, but update the doc first.** Six fixes need to go in before you code the affected parts. None of them needs a redesign; each is a sentence or two in the doc plus a test.

## What's already good
- Stock is reserved with a conditional `UPDATE ... WHERE available >= :qty`. This is the right way to stop overselling under concurrent orders.
- The payment call happens outside any transaction and uses the order id as the provider's idempotency key, so retries don't double-charge.
- When the payment result is unknown, the order stays `PENDING` and a reconciliation job settles it later. This is the hard case most designs skip.
- The scale (2,000 orders a day) is small enough for this approach.

## Fix before building

**1. Stock can be restored twice.** Settling an order isn't guarded. Here's the failing case: both instances run the reconciliation job, both see order X as `PENDING`, both get "declined" from the provider, and both run "mark `FAILED` + restore stock". Order X had 2 units, so stock goes up by 4 when it should go up by 2. A late request handler racing the job can do the same thing.
*Fix:* every status change must be `UPDATE orders SET status = ... WHERE id = :id AND status = 'PENDING'`. Restore stock only if that update changed exactly one row. Also say whether the job runs on one instance only or on both with `FOR UPDATE SKIP LOCKED`.

**2. Orders reconciled as `FAILED` don't get their stock back.** Step 5 restores stock on a decline. Step 6 only says the job "settles them as PAID or FAILED". Both paths should call the same settle function.

**3. A charge that never reached the provider stays `PENDING` forever.** If every attempt timed out before the request reached the provider, the provider has no record of it. The job has no rule for "not found", so the order sits there, the 30-minute alert fires, and nobody knows what to do with it.
*Fix:* add a rule, e.g. "not found and older than N minutes → `FAILED`, restore stock". Before you build, also confirm that your provider lets you look up a charge by your idempotency key or order id. Many providers don't (Stripe, for example, can't be queried by idempotency key, and its keys expire after 24 hours). If yours doesn't, put the order id in the charge's metadata and store the provider's charge id on the order.

**4. The confirmation email can be lost.** The design says "commit, then queue the email". If the process crashes between those two steps, the order is `PAID` and no email is ever sent. Orders reconciled as `PAID` also never get an email.
*Fix:* write an outbox row (a pending-email record) in the same transaction as the `PAID` update, and have a worker send from the outbox. Add the outbox table to the data model.

**5. The design contradicts its own module rule.** Step 2 updates `stock` (inventory) and inserts into `orders` (orders) in one transaction. The code-structure section says no module reads another module's tables. Decide how a transaction spans modules. The usual answer is that each module exposes functions that take a shared transaction (unit of work), so `OrderCheckout` owns the transaction and each module still touches only its own tables. Write the decision down.

**6. Two concurrent orders can deadlock.** Order A reserves `[x, y]` while order B reserves `[y, x]`. Each locks its first row and waits on the other's. *Fix:* reserve SKUs in sorted order.

## Smaller gaps (fine to fill in as you go)
- **No way to check a `202` order.** Nothing tells the client how to learn the outcome. Add `GET /orders/{id}`, or state that re-sending the same `POST` returns the current status.
- **Idempotency key details:**
  - Is the key unique globally or per customer?
  - What happens when the same key arrives with a different cart? The usual answer is `422`.
  - Two identical requests arriving at once will hit the unique constraint. Catch that error and return the existing order instead of a `500`.
- **Retry scope:** retry on timeouts and provider `5xx` only, never on a decline. The worst case is about 20 s of timeouts plus backoff, so check that the load balancer's timeout allows it.
- **Prices:** state that `unit_price` comes from your catalog, not from the client's cart.
- **Data model:**
  - Add primary keys: `stock.sku`, and `(order_id, sku)` on `order_items`.
  - Add `CHECK (available >= 0)`.
  - Add a column for the provider's charge id.

## Tests to add to the plan
- A decline restores stock.
- The reconciliation job settles an order as `PAID`, as `FAILED` (with stock restored), and handles "not found".
- Two settlers race on the same order and stock is restored only once.
- Two duplicate `POST`s arrive at the same time.
- Two buyers race for the last unit of stock.
- An email failure doesn't change the order, and the outbox resends it.
- Multi-item orders reserved in opposite SKU order don't deadlock.

## Order lifecycle with the fixes
```mermaid
stateDiagram-v2
    [*] --> PENDING: stock reserved (SKUs in sorted order)
    PENDING --> PAID: charge succeeded (+ outbox email row, same transaction)
    PENDING --> FAILED: declined, or "not found" after N minutes (+ restore stock)
    PENDING --> PENDING: result unknown, returns 202, reconciliation job retries
    note right of PENDING: every change out of PENDING is a guarded UPDATE ... WHERE status = 'PENDING'
```

To start tomorrow, add fixes 1–6 and the missing tests to `design.md`. Check how your payment provider supports charge lookup (fix 3) before building the reconciliation job. Steps 1–2 of the flow can be built right away.

VERDICT: READY WITH CHANGES
