**The design is mostly sound, and you can start building tomorrow.** About an hour of edits to design.md first will close the gaps below. Four are high severity. They all sit in the payment-failure and recovery paths, so you can build the happy path (steps 1–4) while you fix the doc.

**What's already right:** the duplicate-request handling (idempotency key), the stock update that can't oversell, the payment call made outside the database transaction, using the order id as the provider's idempotency key, the reconciliation job and the 30-minute alert.

## Order status lifecycle and where the gaps are

```mermaid
stateDiagram-v2
    [*] --> PENDING: POST /orders (stock reserved)
    PENDING --> PAID: charge succeeds (request or reconciliation)
    PENDING --> FAILED: charge declined (request or reconciliation)
    PENDING --> PENDING: result unknown, 202
```

The design never says who restores stock when the reconciliation job marks an order FAILED. It also doesn't cover the case where the provider has no record of the charge at all.

## Problems, by severity

**High**

1. **The reconciliation job doesn't restore stock.** Step 5 restores stock on a decline. Step 6 only says the job "settles them as PAID or FAILED".
   - Case: a charge times out, and the job later learns it was declined. The order becomes FAILED but the stock stays reserved.
   - Expected: the job runs the same "mark FAILED and restore stock" transaction as step 5. Make that one shared function.
2. **No plan for "the provider has no record of this charge".**
   - Case: an instance crashes after step 2 commits but before step 3 runs. The job asks the provider about the order and gets "not found". Nothing defines what happens next, so the order stays PENDING until the alert fires, with its stock held.
   - Fix: decide whether the job marks the order FAILED and restores stock, or retries the charge. Retrying is safe because the order id is the idempotency key.
3. **Stock can be restored twice.** Status updates are unconditional, and the job runs on both instances.
   - Case: both instances' reconciliation jobs pick up the same PENDING order with 2 units reserved, and both see "declined". Stock goes up by 4 instead of 2. The same thing can happen between a slow request in step 5 and the job.
   - Fix: make every status change conditional (`UPDATE orders SET status=… WHERE id=… AND status='PENDING'`) and only restore stock if a row actually changed. Also make sure only one instance runs the job at a time, for example with a Postgres advisory lock or `FOR UPDATE SKIP LOCKED`.
4. **The client has no way to learn the outcome after a `202`.** No `GET /orders/{id}` (or callback) is defined, so the client can't tell whether the order was paid. Add the endpoint to the flow and to the tests.

**Medium**

5. **The design doesn't say where prices come from.** "The client sends … the cart." If `unit_price` or `total` come from the request, a client can set its own price. State that the server looks up prices.
6. **The same idempotency key with a different cart silently returns the old order.**
   - Case: a client bug reuses a key for a new cart, gets back the old order, and believes the new one was placed.
   - Fix: store a hash of the request and return `422` when it doesn't match. Also scope the unique constraint to `(customer_id, idempotency_key)`.
7. **Two simultaneous requests with the same key aren't covered.** The second insert hits the unique constraint. The design should say: roll back (which also releases its stock), then load and return the existing order without charging again.
8. **The confirmation email can be lost.** "Commits, and then queues" means a crash between the two steps drops the email. Either write the queue entry in the same transaction as the PAID update (an "outbox" table), or accept and document the loss. Also say whether orders the job marks PAID get an email.
9. **Tests are missing for the recovery paths**, which are where the high-severity gaps are. Add:
   - reconciliation outcomes: unknown→PAID, unknown→FAILED with stock restored, and provider "not found"
   - a crash between commit and charge
   - concurrent duplicate requests
   - the job and a request settling the same order at once
   - a reused key with a different cart
   - a decline restoring stock

**Low**

10. **Possible deadlocks.** Two carts that update the same SKUs in opposite order (A,B and B,A) can deadlock. Sort SKUs before running the updates.
11. **Schema gaps.** `order_items` has no primary key. `stock.sku` isn't declared as the primary key. Add a `CHECK (available >= 0)` constraint as a backstop.
12. **The module rule and the shared transaction need one sentence of reconciling.** "No module reads another module's tables" fits only if step 2 calls an inventory-module method that joins a transaction the orders module passes in. Name that pattern so nobody writes cross-module SQL to make it work.

VERDICT: READY WITH CHANGES
