**It's close, but not ready to build as written.** The overall shape is sound: stock is reserved in a database transaction, the payment call happens outside any transaction, the provider gets an idempotency key, and a reconciliation job settles orders whose payment result is unknown. Most designs I see get that wrong, and this one doesn't. There are still three gaps that will cause wrong stock counts or wrong charges, and each takes a sentence or two to fix. Fix them tonight and you can start tomorrow.

## Must fix before coding

**1. Two processes can settle the same order, and stock gets restored twice.** Both instances run the reconciliation job every 5 minutes, and the design never says that moving out of `PENDING` is guarded.
- Concrete case: order O reserved the last unit of SKU X, so `available = 0`. The server crashed before charging. At 12:05 both instances' jobs pick up O, and the provider reports no charge for either. Both mark O `FAILED` and both add the unit back, so `available = 2`. The right answer is 1.
- The same race exists between a slow request still running step 4 or 5 and the job. On the `PAID` side, the customer gets two confirmation emails.
- Fix: every status change is `UPDATE orders SET status = :new WHERE id = :id AND status = 'PENDING'`. Restore stock or queue the email only if that update changed exactly one row. Write this rule into the design.

```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), restore stock
    PAID --> [*]
    FAILED --> [*]
```

**2. Where do prices come from?** The data model has no product or price table, and step 1 says the client sends "the cart". If `unit_price` comes from the client, anyone can buy anything for $0.01. Say which service or table owns prices, and that the server computes `total` itself.

**3. A negative quantity adds stock.** Nothing validates `quantity`.
- Concrete case: a cart line `{sku: X, quantity: -5}` against `available = 0`. The check `0 >= -5` passes, so `available` becomes 5 and the order total goes negative.
- Fix: validate `quantity > 0` at the API, and add `CHECK (quantity > 0)` on `order_items` and `CHECK (available >= 0)` on `stock`. While you're there, state the primary keys: `stock(sku)` and `order_items(order_id, sku)`.

## Should fix (small, cheap)

4. **Deadlocks between concurrent reservations.** Cart A reserves `[sku1, sku2]` while cart B reserves `[sku2, sku1]`. Postgres aborts one of them, and that customer gets a 500. Fix: run the stock updates in SKU order.
5. **Reconciliation needs a rule for "the provider has no charge for this order".** That's what happens if the server crashes between step 2 and step 3. Say that this case means `FAILED` plus restoring stock, and that it goes through the same guarded path as a decline. The 2-minute age threshold is only safe if the request's whole retry budget is well under 2 minutes. Four attempts at 5 s each plus backoff comes to roughly 25 to 30 s, which is fine, but write the bound down so nobody raises the timeout later and breaks it.
6. **Concurrent duplicate `POST /orders`.** The second request fails on the unique constraint. Its transaction rolls back (so its reservation goes away too), and the service should then catch that error and return the existing order. Also decide two things:
   - Is the key unique per customer, i.e. `UNIQUE (customer_id, idempotency_key)`?
   - What happens when the key repeats but the cart is different? I'd return 422, not the old order.
7. **The confirmation email can be lost.** "Commit, then queue" loses it if the process dies in between. Either queue it inside the same transaction (a table the job sends from), or write down that you accept the risk. At 2,000 orders a day, accepting it is reasonable, but say so.
8. **Module boundaries versus a shared transaction.** Steps 2 and 5 change `stock` and `orders` in one transaction, but the modules may not touch each other's tables. Say that the inventory module exposes `reserve` and `release` functions that take the caller's transaction. Otherwise someone will either break the boundary or split the transaction.

## Tests to add to the plan

- Reconciliation: settles as `PAID`, settles as `FAILED` with stock restored, provider reports no charge, and two job runs on the same order at once (stock restored exactly once).
- A crash after step 2 commits but before the charge.
- Two requests with the same `Idempotency-Key` at the same moment.
- A repeated key with a different cart.
- Zero or negative quantity is rejected.
- Two carts with the same SKUs in opposite order, run concurrently (no deadlock).
- Test the guarded status update directly: a second transition out of `PENDING` changes nothing.

## Fine as is

Scale isn't a concern here: 2,000 orders a day is light for two instances and one Postgres. Sending the order id as the provider's idempotency key, the 30-minute alert on stuck `PENDING` orders, and thin routes over an `OrderCheckout` service are all good choices.

VERDICT: READY WITH CHANGES
