Products · Payment V1

Payment

A one-time payment with Stripe Checkout in test mode.

Version 1 · Stripe Checkout, test modeDesigned, not builtModule 7 of 15

DataSchemaSections

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 USD
  • orders_status_check: only the four statuses
  • orders_paid_check: paid_at is set exactly when status is paid
  • orders_checkout_check: checkout_url only while pending
  • orders_open_reference_idx: unique (app, reference) where status in (pending, paid), so the same thing is never on two open orders or paid twice
  • orders_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

payment.orders

idowner_idappreferencedescriptionamountcurrencystatusstripe_session_idcheckout_urlcheckout_expires_atpaid_atcreated_at
0193b120-5a6b-7c7d-8e8f-9a0b1c2d3e4f0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3edocflowdocflow:0193b0c4-91aa-7b33-8c44-5d6e7f8a9b0cInvoice extraction: acme-invoice-0042.pdf500USDpaidcs_test_a1B2c3D4e5F6g7H8i9J0exampleSession(empty)2026-11-17 10:16:10+002026-11-17 09:17:02+002026-11-17 09:16:10+00
0193b130-1122-7334-8556-6778899aabbc0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3edocflowdocflow:0193b129-0000-7000-8000-000000000001Invoice extraction: march.pdf500USDexpiredcs_test_expiredSessionExample(empty)2026-11-16 11:00:00+00(empty)2026-11-16 10:00:00+00

payment.stripe_events

event_idtypeorder_idcreated_at
evt_test_1Q2w3E4r5T6ycheckout.session.completed0193b120-5a6b-7c7d-8e8f-9a0b1c2d3e4f2026-11-17 09:17:03+00

payment.inbound_events

event_idtypecreated_at
0193b2aa-6f01-7c2d-9e3f-4a5b6c7d8e9fidentity.user.deleted2026-11-17 10:00:01+00

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';