**Don't build the flow as written: steps 3–6 can charge a customer more than once, or charge them and keep no record of the order.** The fixes are well known and fit in about an hour of doc edits, so you can still start tomorrow. The code structure section is sound and the tests section needs one addition.

## Must fix before writing checkout code

**1. Retrying a timed-out charge can charge the customer twice (step 4).**
A timeout means "no answer", not "no charge". For example, the provider charges the card in 6 s, the service gives up at 5 s and retries, and the customer is charged twice for one order.
- **Fix:** send the provider's idempotency key (use the order id) on every attempt, so a retry returns the original result instead of charging again.
- **Check today:** confirm your provider supports idempotency keys; most do.

**2. The rollback erases the only record of an order that may have been paid (steps 3 and 6).**
The `PENDING` row lives inside a transaction that stays open during the payment call. If the charge succeeds and then the commit fails, the process crashes, or all three retries time out, the rollback deletes the row. The customer is charged, there is no order, and there is nothing to find them by.
- **Fix:** commit the `PENDING` row before calling the provider, then record the outcome in a second short transaction.
- **Add a status for "payment outcome unknown"** and a small job that asks the provider what happened and settles those orders.
- **Never roll back to nothing:** a declined payment becomes `FAILED`.

**3. `POST /orders` has no protection against the client sending it twice (step 1).**
The request can take 15 s or more with retries, which is longer than most clients and load balancers wait. For example, the app times out at 10 s, the user taps "Pay" again, and you get two orders and two charges.
- **Fix:** the client sends a unique key per checkout attempt, with a unique constraint on it in the orders table. A repeat returns the existing order.

**4. Nothing says what happens when you sell stock you don't have (step 2).**
The cache can be 5 minutes stale, and nothing reserves stock. Two buyers of the last unit both pass the check and both get charged, even with a fresh cache.
- **Decision for you:** either accept overselling and write down the handling (who is told, automatic refund or backorder), or reserve stock locally when the order is created.
- **My recommendation:** at 200 orders a day, accept it and document the refund path, unless you sell many low-stock items.

## Corrected flow

```mermaid
sequenceDiagram
    participant C as Client
    participant S as order-service
    participant DB as Postgres
    participant P as Payment provider
    C->>S: POST /orders (cart + checkout key)
    S->>DB: Insert order PENDING, commit
    S->>P: Charge (idempotency key = order id), retry on timeout with same key
    alt Charged
        S->>DB: Mark PAID, commit
        S->>C: Success, then confirmation email
    else Declined
        S->>DB: Mark FAILED, commit
        S->>C: Error
    else Still no answer
        S->>DB: Mark payment unknown, commit
        S->>C: "We're confirming your payment"
        Note over S,P: Background job asks provider and settles the order
    end
```

## Should fix, but doesn't block starting

- **Tests:** one happy-path integration test leaves the costly cases uncovered. Add integration tests for timeout then success, decline, all retries timing out, a duplicate `POST`, and a crash between charge and commit.
- **Email:** if the send fails after commit, the customer gets no confirmation. Say whether it is retried; a queued send is enough.
- **Backups:** nightly backups on a single instance mean a disk failure can lose up to a day of paid orders. Turn on continuous archiving (point-in-time recovery) before launch.

Scale is not a concern: 2,000 orders a day is under two a minute, so the single Postgres instance is fine.

VERDICT: READY WITH CHANGES
