**design.md is ready to build from after a handful of small additions.** The core is sound: stock is reserved with a guarded update, the payment call happens outside any transaction, the provider idempotency key is stable across retries, and there is a reconciliation job for unknown outcomes. The gaps are all in what happens when two code paths try to settle the same order, and in the reconciliation path. Each fix is a sentence or two in the doc plus a test, so you can start tomorrow.

## Must fix before coding the settle logic

**1. Settling an order isn't guarded, so stock can be restored twice.**
Two instances each run the reconciliation job every 5 minutes, and the live request can still be settling the same order. Nothing stops both from acting.
- *Example:* an order for 2 units of SKU `A` is `PENDING`. Both instances' jobs pick it up, both learn the charge was declined, and both run "mark `FAILED` + restore stock". `available` goes up by 4 when it should go up by 2, so you sell stock you don't have.
- *Fix:* make every status change conditional, e.g. `UPDATE orders SET status = 'FAILED' WHERE id = :id AND status = 'PENDING'`. Restore stock (or send the email) only if that update changed a row. Optionally have the job claim rows with `FOR UPDATE SKIP LOCKED`.

**2. When reconciliation sets an order to `FAILED`, it has to restore stock.**
Step 5 restores stock on a decline. Step 6 only says "settles them as `PAID` or `FAILED`". As written, a reconciled failure keeps its stock reserved forever. State that both paths call the same settle function.

**3. Reconciliation needs a rule for "provider has no record of this charge".**
- *Example:* an instance crashes after step 2 commits but before step 3 sends the charge. The provider never saw the order, so the job can't get either `PAID` or `FAILED` back.
- *Fix:* decide what happens and write it down. For example, after 2 minutes, "not found" means mark `FAILED` and restore stock. Or retry the charge with the same idempotency key.

**4. The confirmation email must also go out when reconciliation marks an order `PAID`.**
Step 4 only queues it on the live path. Separately, a crash between "commit `PAID`" and "queue email" loses the email. If that matters to you, write an outbox row in the same transaction. If it doesn't, say in the doc that losing the email is accepted.

## Worth stating, not blocking

- **The module rule conflicts with step 2.** Step 2 updates `stock` (inventory module) and inserts into `orders` (orders module) in one transaction. That's fine if `OrderCheckout` opens the transaction and passes it to both modules. Write that down so nobody "fixes" it by splitting the transaction.
- **Duplicate key with a different cart.** Same `Idempotency-Key` with a different body should return an error (commonly `422`), not the old order. Also note that a concurrent duplicate hits the unique constraint at insert, so the handler must catch that error, roll back, and return the existing order.
- **How the client learns the result after `202`.** Name the endpoint, e.g. `GET /orders/{id}`.
- **Who sets prices.** `unit_price` and `total` should come from the server, never from the client's cart.
- **Request time.** Up to 4 attempts at 5 s each plus backoff is about 25 s or more. Check the load balancer's timeout is longer than that, or the client gets a gateway error while the charge is still going through.

## Tests to add to the list

- Reconciliation: settles to `PAID` (and sends the email), settles to `FAILED` (and restores stock), and handles provider "not found".
- Two settlers on one order: stock is restored exactly once.
- Decline restores stock (step 5 isn't covered yet).
- Concurrent duplicate `POST /orders` with the same key, and same key with a different cart.

Order lifecycle with the guard in place (each arrow fires at most once):

```mermaid
stateDiagram-v2
    [*] --> PENDING: POST /orders (stock reserved)
    PENDING --> PAID: charge succeeded (live request or reconciliation)
    PENDING --> FAILED: declined or provider has no record (live request or reconciliation), stock restored
    PAID --> [*]: confirmation email queued
    FAILED --> [*]
```

VERDICT: READY WITH CHANGES
