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

Files touched

Review this PR

Database practice