The design is close but not ready yet. The core checkout flow is sound. Before you write code, add two rules and make one decision. Each is a sentence or two in the doc. The test list also needs about five more cases.

## What's already solid
- Reserving stock with `available >= :qty` blocks overselling without needing locks.
- The payment call happens outside any database transaction, and the order id is the provider's idempotency key. A retry can't double-charge.
- Confirmation email can't fail an order, and the reconciliation job (the 5-minute sweep of `PENDING` orders) covers crashes after the charge.

## Must fix before building

**1. Stock can be restored twice, and then you oversell.** Nothing stops two processes from settling the same order. You run two instances, and the doc doesn't say the reconciliation job runs on only one of them.
- *Case:* SKU A has `available = 0` after order X reserved the last unit. The charge result is unknown, so X stays `PENDING`. Both instances run reconciliation, both get "declined" from the provider, and both mark X `FAILED` and restore stock. Now `available = 2` when it should be 1, and you sell a unit you don't have.
- *Fix:* every status change must be conditional, `UPDATE orders SET status = :new WHERE id = :id AND status = 'PENDING'`. Only restore stock if that update changed a row. Also make sure only one instance works on an order at a time, using a Postgres advisory lock or `SELECT … FOR UPDATE SKIP LOCKED`.

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

**2. Decide what reconciliation does when the provider has no record of the charge.**
- *Case:* an instance crashes after step 2 commits but before it calls the payment provider. The provider has no charge for that order. Under the current design the order stays `PENDING` forever, the stock stays reserved, and the only thing that happens is the 30-minute alert.
- *Fix:* say that "no charge found" means `FAILED` plus stock restore. That is only safe if the request path has a hard total time limit that's well under the 2-minute cutoff. Today that's roughly 4 × 5 s plus backoff, about 30 s. Write the limit down so a later change to the retry settings can't quietly break it.

**3. Scope the idempotency key, and reject a reused key that comes with a different cart.**
- *Case 1:* the key is unique across all customers. If customer B sends a key that customer A already used, B gets A's order back. That leaks A's data.
- *Case 2:* a client sends a new cart but reuses an old key. They silently get the old order back.
- *Fix:* make the constraint unique on `(customer_id, idempotency_key)`. Store a hash of the request body and return `422` when the key matches but the body doesn't.
- Also say what happens when two identical requests arrive at the same moment. The second one hits the unique constraint, which rolls back its stock reservation in the same transaction. That's correct. The handler must then catch the violation and return the existing order, not a `500`.

## Should fix (small, cheap now)
- **Deadlock between carts.** Cart [A, B] and cart [B, A] can deadlock. Postgres then aborts one of them with an error the client sees as a `500` instead of a `409`. Fix: update stock rows in SKU order.
- **Module boundary vs. shared transactions.** "No module reads another's tables" conflicts with steps 2 and 5, which change `stock` and `orders` in one transaction. Decide how they share it. For example, the inventory module exposes `reserve(tx, items)` and `release(tx, items)`, and `OrderCheckout` owns the transaction.
- **Price source.** Say that `unit_price` and `total` are calculated on the server from the catalog, never taken from what the client sends.
- **Email.** Orders that reconciliation marks `PAID` need the email too. And a crash between "commit `PAID`" and "queue email" loses the email. Fix: write an outbox row in the same transaction that marks the order paid.
- **What happens after a `202`.** Tell the client how to find out the result: a `GET /orders/{id}` endpoint, or "retry the same `POST` with the same key."
- **Retries.** Retry only on timeouts and 5xx errors, never after a decline.
- **Schema.** Add primary keys (`stock.sku`, `order_items(order_id, sku)`), foreign keys, a `CHECK (available >= 0)`, a `CHECK` on `status`, and an index on `orders(status, created_at)` for the reconciliation query.

## Tests to add
The current list only covers the request path. Add:
- Reconciliation finds the charge paid, declined, and not found.
- The double-settle race from fix 1: two settlers on one order, and stock is restored exactly once.
- Two concurrent `POST`s with the same key.
- The same key with a different cart, which should return `422`.
- A crash between reserving stock and charging, which reconciliation should release.
- Opposite-order carts don't deadlock.

VERDICT: READY WITH CHANGES
