**This design is close to ready. Before you build, change one thing: the step that moves an order to `FAILED` and puts its stock back must only work on an order that is still `PENDING`.** That one-line rule closes a real hole where stock gets returned twice. Everything else below is small enough to handle while you build.

## What's already solid
- **Duplicate orders:** the unique `Idempotency-Key` stops them. Sending the order id to the payment provider as its idempotency key stops double charges when you retry. Together they cover the riskiest failures in checkout.
- **Overselling:** the conditional `UPDATE ... WHERE available >= :qty` inside one transaction is simple and correct.
- **No database transaction is open during the payment call,** so a slow provider can't tie up database connections.
- **Unknown payment results:** answering `202`, running reconciliation, and alerting after 30 minutes means an order whose payment result never came back can't go unnoticed.
- **The test list covers the failures that matter,** including a timeout on every attempt and buying the last unit.

## The one change to make first
Two places can settle a `PENDING` order: the request itself and the reconciliation job. The job also runs on both instances. Nothing in the design stops two of them from settling the same order.

**Concrete case:** order X holds 2 units of SKU A, and the provider declines it. Both instances run reconciliation at the same moment and both see X as `PENDING`. Each one marks it `FAILED` and adds 2 back. Expected: stock goes up by 2. Actual: it goes up by 4, and you sell 2 units you don't have.

**Fix (fits your current approach):** make every status change conditional, and only restore stock if the change actually happened, in the same transaction:

```sql
UPDATE orders SET status = 'FAILED', updated_at = now()
WHERE id = :id AND status = 'PENDING';
-- restore stock only if this updated 1 row
```

Use the same `AND status = 'PENDING'` guard for `PAID`. Add a test where the order is settled twice at the same time and stock comes back only once.

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

## Small things to cover while building
1. **Reconciliation when the provider has no record of the charge.** This happens if an instance crashes after step 2 but before calling the provider. State that "no charge found" means `FAILED` plus stock restored. Also state that reconciliation queues the confirmation email when it settles an order as `PAID`.
2. **Two identical `POST /orders` at the same moment.** The second one fails on the unique constraint. Catch that error and return the existing order instead of a `500`.
3. **Deadlocks on carts with several items.** Two carts that update the same SKUs in different orders can deadlock. Sort the SKUs before running the stock updates.
4. **How the client finds out the result after a `202`.** Add a `GET /orders/{id}` so the client can check the status later.

None of these requires redesigning anything. Each one adds to what you already have.

VERDICT: READY WITH CHANGES
