# Review of design.md

**No, not yet.** The payment step (steps 3–6) can charge a customer twice, and it can charge them with no order saved. Both problems are in the core money path, so fix them in the doc before you write code. The fix is a known pattern and about an hour of design work. The code structure section is sound and I'd keep it as is.

## Must fix before building

**1. Retrying on timeout can charge the same card up to four times.**
A timeout doesn't mean the charge failed. It means you don't know the result. Example: a customer checks out a $50 cart. The provider charges the card but answers after 6 s. Your 5 s timeout fires and you retry three times. Each retry is a new charge, so the customer pays up to $200 when they should pay $50.
*Fix:* send an idempotency key with every charge request, for example the order id; most payment providers support this. Every retry then refers to the same single charge.

**2. If the charge succeeds but the commit fails, nothing records that you took the money.**
The `PENDING` row is inserted inside the same open transaction (step 3), so it only becomes visible at commit. Example: the charge succeeds, then the process crashes or the commit fails. The rollback removes the `PENDING` row. The customer has paid, and you have no record of the order. The same thing happens after three timed-out retries: step 6 rolls back and returns an error, but one of those attempts may have charged the card.
That transaction also stays open across up to about 20 s of payment calls (4 attempts × 5 s). The whole time it holds a database connection.
*Fix:* commit `PENDING` before calling the provider. Charge with the idempotency key. Then update to `PAID` or `FAILED` in a second, short transaction. If the result is still unknown, leave the order `PENDING` and have a reconciliation job ask the provider later.

```mermaid
stateDiagram-v2
    [*] --> PENDING: insert and commit
    PENDING --> PAID: charge succeeded
    PENDING --> FAILED: charge declined
    PENDING --> PENDING: timeout, result unknown
    PENDING --> PAID: reconciliation finds the charge
    PENDING --> FAILED: reconciliation finds no charge
    PAID --> [*]: queue confirmation email
```

**3. `POST /orders` has no protection against duplicate submissions.**
Example: a user double-clicks Pay, or their phone retries on a flaky network. You get two orders and two charges, each with its own key, so fix 1 doesn't catch this.
*Fix:* have the client send an `Idempotency-Key` header. Store it with a unique constraint, and return the existing order when the same key arrives again.

**4. The design doesn't say what happens when the cached stock is wrong.**
The cache can be up to 5 minutes old, and nothing reserves or decrements stock. Example: one unit is left, and two customers check out within a minute. Both pass the stock check and both are charged. Using the cache is a reasonable tradeoff at this volume, but the doc should state the outcome. Do you refund? Backorder? Re-check with the warehouse after payment? Whoever builds this needs that decision.

## Should fix, but they don't block starting

- **Tests:** one happy-path integration test isn't enough for a payment flow. Add tests for a timeout followed by a successful retry with no second charge, a declined charge, a commit failure after a successful charge, a duplicate `POST`, and running out of stock. Write these first, since they define the fixes above.
- **Confirmation email:** don't send it inside the request. Queue it after the `PAID` commit, for example with an outbox table. That way a failure at the email provider can't fail checkout or get it retried.
- **Backups:** with nightly backups only, a database loss can wipe up to 24 hours of paid orders. Turn on point-in-time recovery (continuous WAL archiving). It's cheap.
- **Scale:** 2,000 orders a day is about one order every 40 seconds. A single Postgres instance easily handles that, so no change is needed.

## Next step

Rewrite steps 3–6 using the lifecycle above. Add the client idempotency key and a one-line decision on overselling. The plan is then ready to build from.

VERDICT: NOT READY
