The core of the design holds up, so you can start tomorrow. Fix three things in the doc first, because each one decides how a piece of code gets written. Everything else is a small addition you can make as you go.

## What's already right
- Stock is reserved with a conditional update (`available >= :qty`), so two buyers can't both get the last unit.
- The payment call happens outside any transaction, and the order id is sent as the provider's idempotency key. Retries can't double-charge, and no database lock is held while waiting on the network.
- An unknown payment result leaves the order `PENDING`. A reconciliation job settles it later, and an alert fires after 30 minutes, so orders don't silently get stuck.
- The email is sent after the commit and can never fail the order.

## Fix before building

**1. Stock can be restored twice for one order.** Steps 5 and 6 can both settle the same order, and neither step checks the order's current status before acting.
- Case: the charge for order A (2 units of SKU X) times out on the first try, and a later retry is declined. At the same moment the reconciliation job picks up A, because it has been `PENDING` for more than 2 minutes, and also sees the decline.
- Wrong result: both mark A `FAILED` and both add the 2 units back, so `available` ends up 2 higher than it should be.
- Expected: exactly one of them settles A and restores the stock.
- Fix: every status change must only apply if the order is still pending: `UPDATE orders SET status = :new WHERE id = :id AND status = 'PENDING'`. Restore stock (or queue the email) only if that update changed a row.

```mermaid
stateDiagram-v2
    [*] --> PENDING: stock reserved, order inserted
    PENDING --> PAID: charge succeeded (only if still PENDING)
    PENDING --> FAILED: declined, or provider has no charge (only if still PENDING) + restore stock
    PAID --> [*]
    FAILED --> [*]
```

**2. The doc doesn't say where prices come from.** Step 1 says the client sends "the cart". If `unit_price` or `total` is taken from that request, a client can send a price of 0.01 and be charged 0.01. State that prices are read on the server side (from a catalog or price table, which isn't in the data model yet). Also say which module owns prices.

**3. The transaction in step 2 crosses module boundaries.** It writes `stock`, which the inventory module owns, and `orders`, which the orders module owns, in one transaction. That's correct, but the code-structure section doesn't say how two modules share one transaction. Decide now. For example: `OrderCheckout` opens a unit of work and passes it to `inventory.reserve(...)` and `orders.create(...)`, so neither module touches the other's tables. If you don't decide, someone will reach straight into `stock` from the orders code on day one.

## Add as you build (small, but write them down)
- **Concurrent orders can deadlock.** Order 1 holds {X, Y} and order 2 holds {Y, X}. Each locks its first SKU and waits for the other, and Postgres aborts one with a deadlock error, not a 409. Fix: update SKUs in sorted order and merge duplicate lines into one first.
- **Clients have no way to see the result after a 202.** Add `GET /orders/{id}`.
- **An idempotency key reused with a different cart.** Today it silently returns the first order. Return 422 instead. Also say what status code a repeated key gets while its order is still `PENDING`.
- **The reconciliation job needs a case for "provider has no charge for this id".** This happens if the service crashed before calling the provider. Settle it as `FAILED` and restore the stock.
- **The email can be lost.** If the service crashes between "commit `PAID`" and "queue email", no email is sent. Write the email job in the same transaction as the status change (an outbox table). Also confirm the reconciliation job sends the email when it settles an order as `PAID`, which the doc doesn't say.
- **Retry only timeouts and 5xx errors**, never declines or other 4xx responses.
- **Schema:**
  - `stock.sku` should be the primary key.
  - `order_items` needs a primary key (`order_id, sku`).
  - Add an index on `orders(status, created_at)` for the reconciliation job.
  - Add a check constraint `available >= 0`.
- **Missing tests:**
  - Two concurrent requests for the last unit.
  - Two concurrent requests with the same idempotency key.
  - The handler and the reconciliation job settling the same order at once (the test for fix 1).
  - Reconciliation finding a paid charge, a declined charge, and no charge.
  - A client sending its own prices.
  - Email failure.
  - Write the test for fix 1 first.

Nothing here needs a redesign. Capacity isn't a concern: 2,000 orders a day is small for two instances and one Postgres database.

VERDICT: READY WITH CHANGES
