**design.md is close, but fix four gaps in the doc before you build. The current text leaves room for double stock restores, deadlocks, untrusted prices and orders stuck in `PENDING`.** The overall shape is sound and doesn't need rework. You reserve stock with a conditional update, call the payment provider outside any transaction, send the order id as the provider's idempotency key, and settle unknown results with a reconciliation job. That holds up well at 2,000 orders a day. Each fix below is a paragraph or two in the doc.

## Fix before building

**1. Two writers can settle the same order, and the doc doesn't stop both from acting.** The request path (steps 4–5) and the reconciliation job (step 6) can both settle an order. With two instances, the job may also run twice at once.
- **Case:** a `PENDING` order for 1 × SKU-A is declined. Both instances' jobs see it and each runs "mark `FAILED` and restore stock."
- **Wrong result:** stock goes up by 2.
- **Expected:** stock goes up by 1.
- **Fix:** make every status change conditional, e.g. `UPDATE orders SET status='FAILED' WHERE id=:id AND status='PENDING'`. Restore stock only if that update changed a row. Also say explicitly that reconciliation settling as `FAILED` restores stock. Step 6 currently omits this.

```mermaid
stateDiagram-v2
    [*] --> PENDING: POST /orders (stock reserved)
    PENDING --> PAID: charge succeeded (request or reconciler)
    PENDING --> FAILED: declined / no charge (request or reconciler), restores stock once
    PAID --> [*]
    FAILED --> [*]
```
Every arrow only applies `WHERE status='PENDING'`. Whichever writer gets there second does nothing.

**2. Carts with several items can deadlock.**
- **Case:** order A has {SKU-X, SKU-Y} and order B has {SKU-Y, SKU-X}, at the same moment. A locks X and B locks Y, then each waits for the other.
- **Result:** Postgres aborts one of them, and the user sees an error even though stock was available.
- **Fix:** update the stock rows in sorted SKU order. Retry the transaction once if Postgres still reports a deadlock.

**3. The doc doesn't say where `unit_price` and `total` come from.** If they come from the client's cart, a client can send `unit_price: 0.01` and get charged that amount. State that prices come from the server's catalog, and name which module owns it. If the client's price is wrong, decide whether to reject the order or re-price it.

**4. The reconciliation job has no rule for "the provider has no charge for this order."** This happens when an instance crashes after step 2 commits but before step 3 reaches the provider. The order stays `PENDING` forever and the 30-minute alert fires every time. Pick one rule:
- retry the charge with the same idempotency key, or
- mark the order `FAILED` and restore its stock after N minutes.

Also check how long your provider keeps idempotency keys. Many keep them about 24 hours, so a retry after that window could charge the customer a second time.

## Smaller items (clarify, but they shouldn't hold you up)

- **Module boundary vs. shared transaction.** Step 2 (reserve stock + insert order) and step 5 (mark `FAILED` + restore stock) each span the orders and inventory modules in one transaction. That conflicts with "no module reads another module's tables." Say how this works. The usual answer: the inventory module offers `reserve()`/`release()` functions that run inside a transaction the caller passes in. Otherwise `OrderCheckout` ends up touching the `stock` table directly.
- **Which errors are retried.** Retry only timeouts and 5xx responses, never declines. As written, "up to 3 retries" could be read as retrying a decline.
- **How a client learns the outcome after a `202`.** No `GET /orders/{id}` exists. Either add one or document that re-sending the `POST` with the same key is the way to check.
- **Idempotency key scope and mismatches.** Make the unique constraint `(customer_id, idempotency_key)` rather than global. Decide what happens when the same key arrives with a different cart; a `422` is typical.
- **Lost confirmation emails.** In step 4, if the instance crashes after the `PAID` commit but before the email is queued, the email is never sent. Either write the email job in the same transaction (an outbox table) or accept the risk in writing.
- **Schema.** Add primary keys, foreign keys, and `CHECK (available >= 0)` on `stock`. `order_items` has no primary key.

## Tests to add to the plan

Add these to the current integration list:
- two concurrent orders for the last unit
- two multi-SKU orders in opposite order (the deadlock case)
- a multi-item cart where one SKU is short: everything rolls back and no stock changes
- decline is not retried
- reconciliation settles `PAID`
- reconciliation settles `FAILED` and restores stock
- reconciliation finds no charge
- two reconciliation runs on the same order restore stock only once
- duplicate key with a different cart

Write these tests first. Items 1 and 2 above are the bugs they'll catch.

VERDICT: READY WITH CHANGES
