The design is not safe to build as written, because steps 3–6 can charge a customer twice or charge them and leave no order record. The fixes are small and confined to the payment steps, so you can start tomorrow if you make them first.

## Must fix before building the payment path

**1. Retrying on timeout can charge the customer more than once (step 4).**
A timeout means "no answer", not "the charge failed". For example, the provider takes 6 s to process a charge and succeeds; the service gave up at 5 s and retries, and the customer is charged twice for one order.
- **Fix:** send an idempotency key (the order id) with every charge attempt so the provider treats retries as the same charge. Confirm your provider supports this; most do.

**2. Rolling back on failure deletes the only record of an order that may have been paid (steps 3 and 6).**
The `PENDING` row lives inside a transaction that is still open during the payment call. For example, all attempts time out but one actually went through at the provider; the service rolls back and the customer sees an error. They have been charged, and there is no row to find or refund against. The same happens if the process crashes between the charge and the commit.
- **Fix:** commit the `PENDING` order first, call the provider with no transaction open, then update the order to `PAID` or `FAILED` in a second short transaction.
- **Also add:** a small scheduled job that asks the provider about orders stuck in `PENDING` and settles them.
- **Side benefit:** a database connection is no longer held for up to 15–20 s while the provider is slow.

**3. Nothing stops two orders from buying the same last item (step 2).**
The flow checks stock but never reserves or decrements it, and the cache will not reflect the service's own sales for up to 5 minutes. For example, with one unit left, two customers who order within the same 5 minutes both pass the check and both pay.
- **Fix:** accepting the cache is reasonable at your volume, but the design needs to say what happens on an oversell. Either decrement a local stock count in the same transaction as the order insert, or accept overselling and define the refund or backorder path.

**4. A repeated `POST /orders` creates a second order (step 1).**
A double-click, or a client retry after a slow response, creates and charges a duplicate order. A slow response is likely given the possible 15–20 s wait.
- **Fix:** have the client send an idempotency key and return the existing order when the key repeats.

**5. The test plan covers only the path where nothing goes wrong.**
Every failure above is in a failure path. Add integration tests, using a fake payment provider, for:
- timeout then success
- card declined
- all attempts timing out
- a crash after the charge
- a duplicate request

## Revised flow

```mermaid
sequenceDiagram
    Client->>Service: POST /orders (cart, idempotency key)
    Service->>Postgres: Check stock, insert order PENDING, commit
    Service->>Payment provider: Charge (order id as idempotency key, retry on timeout)
    Payment provider-->>Service: Success, declined, or no answer
    Service->>Postgres: Mark PAID or FAILED, commit
    Service-->>Client: Result
    Service->>Client: Confirmation email (after PAID)
    Note over Service,Payment provider: Scheduled job settles orders left PENDING
```

## Smaller points, not blocking

- **Email failure:** state that a failed confirmation email must not fail the order. Send it after the commit and retry it separately.
- **Backups:** nightly backups mean up to a day of paid orders could be lost. Turning on continuous archiving (point-in-time recovery) is cheap and worth doing before launch.
- **Retry count:** "retries, up to 3 times" is ambiguous between 3 and 4 total attempts. Pick one and state the worst-case wait the client sees.

## What is right and should stay

A single service on a single Postgres instance with a synchronous flow is the right size for 2,000 orders a day and a team of four. The module boundaries, thin routes, the `OrderCheckout` service class, and order-id logging are all sound. Do not add queues or split services.

VERDICT: READY WITH CHANGES
