The design is sound in structure and scale, but the payment step can charge a customer twice or charge them without leaving any order record. Fix those two things in the flow (both are small) and you can start building tomorrow.

## What to change before building

**1. A retry after a timeout can charge the customer twice.**
A timeout means you don't know whether the charge went through; it does not mean it failed.
- **Case:** the provider charges the card in 6 s, and the service gave up at 5 s and retries.
- **Result:** the customer is charged twice for one order.
- **Expected:** one charge.
- **Fix:** send an idempotency key (the order id) with every charge call, so the provider treats retries as the same charge.

**2. A rollback after a successful charge erases the order but not the charge.**
The `PENDING` insert and the `PAID` update sit in one transaction that stays open across the payment call.
- **Case:** the charge succeeds, then the service crashes or the commit fails.
- **Result:** the customer has paid, and the database has no row for the order, so there is nothing to refund against or reconcile.
- **Expected:** an order row that shows payment was attempted.
- **Fix:** commit the `PENDING` order first, call the provider with no transaction open, then update to `PAID` or `FAILED` in a second short transaction.

This also stops each checkout holding a database connection for up to 20 s (four attempts at 5 s each) while the provider is slow.

```mermaid
flowchart TD
    A[POST /orders] --> B[Check stock in cache]
    B --> C[Insert order PENDING and commit]
    C --> D[Charge with order id as idempotency key]
    D -->|success| E[Mark PAID, commit, send email]
    D -->|declined| F[Mark FAILED, return error]
    D -->|still unknown after retries| G[Leave PENDING, reconcile later]
```

The "leave PENDING" branch needs a small job that asks the provider what happened to old `PENDING` orders and settles them. It can ship a few days after the main flow, but it should be in the design.

**3. The test plan covers only the happy path.**
The bugs above live in the failure paths. Add integration tests with a fake payment provider for:
- a timeout followed by success, asserting one charge;
- a declined card;
- a crash after the charge.

## Decide, but not blocking

- **Overselling:** step 2 checks stock but does not reserve it, and the cache can be 5 minutes old, so two orders can both get the last item. At 200 orders a day, accepting that is reasonable. Write down what happens when it occurs (refund and apologise, or backorder).
- **Double-clicks:** a client that resubmits `POST /orders` creates two orders. A client-supplied request id would prevent it.

## What is already right

- Postgres on a single instance fits 2,000 orders a day, and one service suits a team of four.
- The module boundaries, thin routes and the `OrderCheckout` class are good choices.
- Putting the order id on every log line is right.
- Accepting the cache over an 800 ms warehouse call is a sensible tradeoff.
- Sending the confirmation email after the commit is the right order.

VERDICT: READY WITH CHANGES
