Schema Designed
The SQL for the Payment service's tables, in the payment schema. No other service can read them.
-- Payment service, version 1. Schema: payment.
-- IDs are UUIDv7 values generated by the service.
-- Checked with `npm run schema:check`: runs on PGlite, and every sample row passes every constraint.
create schema if not exists payment;
create table payment.orders (
id uuid primary key,
owner_id uuid,
app text not null,
reference text not null,
description text not null,
amount integer not null,
currency text not null,
status text not null default 'pending',
stripe_session_id text,
checkout_url text,
checkout_expires_at timestamptz,
paid_at timestamptz,
created_at timestamptz not null default now(),
constraint orders_stripe_session_id_key unique (stripe_session_id),
constraint orders_text_check check (
length(app) between 1 and 50
and length(reference) between 1 and 200
and length(description) between 1 and 200
),
constraint orders_amount_check check (amount between 50 and 100000 and currency = 'USD'),
constraint orders_status_check check (status in ('pending', 'paid', 'expired', 'failed')),
constraint orders_paid_check check ((status = 'paid') = (paid_at is not null)),
constraint orders_checkout_check check (checkout_url is null or status = 'pending'),
constraint orders_session_check check (left(stripe_session_id, 3) = 'cs_')
);
-- Never two open orders, and never two payments, for the same thing in the same app.
create unique index orders_open_reference_idx on payment.orders (app, reference)
where status in ('pending', 'paid');
create index orders_owner_id_idx on payment.orders (owner_id, created_at desc);
create table payment.stripe_events (
event_id text primary key,
type text not null,
order_id uuid,
created_at timestamptz not null default now(),
constraint stripe_events_id_check check (left(event_id, 4) = 'evt_' and length(type) between 1 and 100)
);
create table payment.inbound_events (
event_id uuid primary key,
type text not null,
created_at timestamptz not null default now()
);
-- The service's database login reaches only this schema. Orders are never deleted.
grant usage on schema payment to payment_service;
grant select, insert, update on payment.orders to payment_service;
grant select, insert on payment.stripe_events, payment.inbound_events to payment_service;
grant select, insert on payment.audit_events to payment_service;
Schema level: enforced by the database, whatever the code does
- Never two pending-or-paid orders for the same app and reference (partial unique index).
- A paid order always has its payment time; a checkout address exists only while pending.
- Amounts are whole minor units within range; currency is USD.
- Each Stripe event and platform event is recorded once (primary keys).
- Payment's database login reaches only the
payment schema and cannot delete orders.
Constraints and indexes by table
payment.orders
orders_pkey: primary key (id)orders_stripe_session_id_key: unique (stripe_session_id)orders_amount_check: amount 50 to 100,000; currency is USDorders_status_check: only the four statusesorders_paid_check: paid_at is set exactly when status is paidorders_checkout_check: checkout_url only while pendingorders_open_reference_idx: unique (app, reference) where status in (pending, paid), so the same thing is never on two open orders or paid twiceorders_owner_id_idx: index on (owner_id, created_at desc), for a user's orders
payment.stripe_events
stripe_events_pkey: primary key (event_id)stripe_events_id_check: event_id starts with evt_
payment.inbound_events
inbound_events_pkey: primary key (event_id)
Sample data
Common queries
Reuse an open order, or create one (`POST /orders`)
select * from payment.orders
where app = $1 and reference = $2 and status in ('pending', 'paid');
-- paid: 409 already_paid; pending with an open page: return it; none: insert a new order
Apply a Stripe event once (`POST /webhooks/stripe`)
insert into payment.stripe_events (event_id, type, order_id) values ($1, $2, $3)
on conflict (event_id) do nothing;
-- no row inserted: already processed, answer 200
update payment.orders set status = 'paid', paid_at = now(), checkout_url = null
where id = $3 and status = 'pending';
Schema on GitHub →