The design is close: the core of the flow is sound and you can start building tomorrow. Before you write the settlement code, make two small changes to the doc. Without them, an order can be settled twice (stock comes back twice, or a paid order gets marked `FAILED`), and an order that was never charged can sit in `PENDING` with no rule for what happens next.

## What's already solid
- The stock reservation is one atomic `UPDATE ... WHERE available >= :qty`. This can't oversell, and it doesn't need locks or a read-then-write step.
- The payment call happens outside the transaction, so a slow provider doesn't hold database locks.
- Using the order id as the provider's idempotency key means retries can't double-charge.
- The "unknown result" case is handled properly: the order stays `PENDING`, the client gets `202`, a reconciliation job settles it, and an alert fires after 30 minutes. Many designs skip this case.
- The module boundaries, the thin routes and the order id on every log line are all right for a team of four at 2,000 orders a day.

## Changes to make before building

**1. Only settle an order if it is still `PENDING` (blocking).**
Two code paths can settle the same order: the request path (steps 4–5) and the reconciliation job (step 6). The job also runs on both service instances. Here's a failing case:
- The first charge attempt times out at the provider but actually succeeds. The order stays `PENDING`.
- At 5 minutes, the reconciliation jobs on instance A and instance B both pick up the order. If the provider reports it as declined or not found, both mark it `FAILED` and both restore stock. The stock count is now 2 too high, when it should be restored once.
- In the same way, a late success on the request path can overwrite a `FAILED` set by the job, or the reverse.

Fix: every settlement should be `UPDATE orders SET status = :new WHERE id = :id AND status = 'PENDING'`. Restore stock and queue the email only if that update changed exactly one row. Then whichever path settles first wins, and the other does nothing.

```mermaid
stateDiagram-v2
    [*] --> PENDING: POST /orders (stock reserved)
    PENDING --> PAID: charge succeeded (only if still PENDING) → queue email
    PENDING --> FAILED: declined / never charged (only if still PENDING) → restore stock
    PAID --> [*]
    FAILED --> [*]
```

**2. Say what reconciliation does when the provider has no record of the charge (blocking).**
If an instance crashes after step 2 commits but before step 3 sends the charge, the order is `PENDING` and the provider has never seen it. Step 6 only covers `PAID` or `FAILED`. Pick one rule: either mark the order `FAILED` and restore stock, or send the charge again with the same order id. Also say that reconciliation queues the confirmation email when it settles an order as `PAID`.

**3. Settle where unit prices come from.**
`order_items.unit_price` and `orders.total` exist, but the design has no prices table and doesn't say where prices are looked up. If prices come from the client's cart, a customer can send a price of 0.01. Prices should be looked up on the server.

**4. Queue the email in the same transaction as `PAID`.**
"Commit, then queue" loses the email if the instance crashes between those two steps. Write a row to an outbox table (a pending-emails table) in the `PAID` transaction instead, and have a worker send from it. If the queue is already in Postgres, this is just a matter of where the insert goes.

**5. Add `GET /orders/{id}`.**
A client that gets `202` has no documented way to find out how the order ended. This endpoint lets it check.

## Smaller items (fine to handle while building)
- **Same key, different cart:** if the `Idempotency-Key` repeats but the cart is different, return `422` rather than the old order. When two identical requests arrive at once, the second fails on the unique constraint. Catch that error and return the existing order instead of a `500`.
- **Deadlocks:** reserve SKUs in sorted order, so two carts that share SKUs can't deadlock each other.
- **Index:** add one on `orders (status, created_at)` so the reconciliation query stays fast.

## Tests to add
Add these to the integration list:
- Reconciliation finds a charge that succeeded at the provider, and one the provider has never seen.
- Reconciliation and the request path settle the same order at the same time; stock is restored exactly once.
- Two jobs run at the same time and settle each order once.
- Two buyers compete for the last unit; one gets `201`, the other gets `409`.
- The same key is sent with a different cart.

Items 1 and 2 are each a sentence in the doc plus a `WHERE` clause in the code. Items 3–5 are decisions that are easier to make now than to retrofit.

VERDICT: READY WITH CHANGES
