Pay an order with the wallet balance
Database · PostgreSQL · intermediate · modification
Customers can now pay a pending order with their wallet balance, the store credit they get from refunds and gift cards. `POST /api/orders/{order_id}/pay-with-wallet` calls the new `wallet.pay_order`, which does the whole payment in one transaction: it claims the order by flipping it from `pending` to `paid` (so the same order cannot be paid twice), checks that the balance covers the total, writes the new balance and records the payment in `wallet_payments`. If the balance is short it raises before anything is kept, the transaction rolls back and the order stays pending. Migration `0063_wallet_payments.sql` adds `wallet_payments`, one row per order paid with a wallet, and `app/orders.py` maps the two failure cases to 404 and 409. Includes API tests in `tests/test_pay_with_wallet.py`.
Shop API: FastAPI 0.115 on Python 3.12 with psycopg 3 (plain SQL, no ORM). `app/db.py` holds a `psycopg_pool.ConnectionPool` (at most 10 connections per pod; `pool.connection()` commits on a clean exit and rolls back on an exception), and the API runs as 6 pods against one PostgreSQL 16 primary at the default isolation level, READ COMMITTED. Tables from earlier migrations: `customers` (`id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY`, `email text NOT NULL UNIQUE`, `created_at timestamptz NOT NULL DEFAULT now()`); `orders` (`id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY`, `customer_id bigint NOT NULL REFERENCES customers (id)`, `status text NOT NULL` with `CHECK (status IN ('pending', 'paid', 'cancelled'))`, `total_cents bigint NOT NULL CHECK (total_cents > 0)`, `paid_at timestamptz` nullable, null until paid, `created_at timestamptz NOT NULL DEFAULT now()`); `wallets` (`id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY`, `customer_id bigint NOT NULL UNIQUE REFERENCES customers (id)`, `balance_cents bigint NOT NULL DEFAULT 0 CHECK (balance_cents >= 0)`). Every customer gets exactly one wallet row at signup and it is never deleted. Two existing writers change balances, each in its own short transaction: the refund worker runs `UPDATE wallets SET balance_cents = balance_cents + %s WHERE customer_id = %s`, and gift card redemption runs the same statement. Balances and order totals stay below 10^12 cents. `app.auth.current_customer_id` (existing) returns the signed-in customer's id or answers 401, and `app/main.py` already includes the `orders` router. Schema changes are plain SQL files in `migrations/`, applied in name order by the existing runner at the start of each deploy, each file in one transaction. The test suite (`tests/conftest.py`, existing) builds a fresh test database from the migrations, empties it between tests, and provides `create_customer(email, balance_cents=0)` (a customer with their wallet), `insert_order(customer_id, total_cents, status="pending")`, `fetch_one(sql, params)` and `client_for(customer_id)` (a test client signed in as that customer).
Requirements
- New endpoint `POST /api/orders/{order_id}/pay-with-wallet` pays one of the signed-in customer's pending orders from their wallet (store credit). On success the order becomes `paid` with `paid_at` set, the wallet balance goes down by exactly the order's `total_cents`, a `wallet_payments` row records the payment, and the response is 200 `{"order_id", "status": "paid", "wallet_balance_cents"}` with the new balance.
- If the balance is less than the order's total, the answer is 409 `insufficient wallet balance` and nothing changes: the order stays `pending` and the balance stays as it was. An order that is not one of the signed-in customer's `pending` orders (another customer's, already paid, cancelled, unknown) answers 404 `order not found`, so an order is paid at most once.
- The money rule: a wallet's balance is always its credits minus its payments. Every successful payment is debited, the balance never goes below zero, and credits that arrive in the meantime (refunds, gift cards) are kept. Requests for one customer can run at the same time (a customer with several pending orders can pay them from different tabs or devices, and the refund worker credits wallets at any moment), and the rule holds however they interleave; when each payment fits the balance alone but the two together exceed it, one succeeds and the other answers 409.
- Unchanged: `GET /api/orders/{order_id}` returns one of the customer's own orders or 404.
Files touched
- migrations/0063_wallet_payments.sql
- app/wallet.py
- app/orders.py
- tests/test_pay_with_wallet.py