diff --git a/crates/db/migrations/0027_stripe_checkouts.sql b/crates/db/migrations/0027_stripe_checkouts.sql index bd5e4b0..f7d2114 100644 --- a/crates/db/migrations/0027_stripe_checkouts.sql +++ b/crates/db/migrations/0027_stripe_checkouts.sql @@ -1,74 +1,16 @@ -- Stripe Checkout sessions started from the package cards. The package is -- granted once, when the payment is confirmed (webhook, or the return page -- asking Stripe), and the session id keeps that from happening twice. --- Card details never reach us: only what Stripe reports about the payment and --- the buyer's billing details for the invoice. CREATE TABLE stripe_checkouts ( -- Stripe's Checkout Session id (cs_test_… / cs_live_…). - id text PRIMARY KEY, - -- Kept when the account is deleted: payments and invoices must be kept - -- for the accounting law's 8 years. - user_id uuid REFERENCES users ON DELETE SET NULL, - package text NOT NULL, - price_huf integer NOT NULL CHECK (price_huf > 0), - live boolean NOT NULL, - created_at timestamptz NOT NULL DEFAULT now(), - -- Filled when the payment is confirmed. - completed_at timestamptz, - order_id uuid REFERENCES package_orders ON DELETE SET NULL, - payment_intent text, - -- What Stripe charged, in forints. - amount_huf integer, - -- Stripe's customer_details as it came: name, e-mail, address, and for a - -- company its name and tax ids. - customer jsonb + id text PRIMARY KEY, + user_id uuid NOT NULL REFERENCES users ON DELETE CASCADE, + package text NOT NULL, + price_huf integer NOT NULL CHECK (price_huf > 0), + live boolean NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + completed_at timestamptz, + order_id uuid REFERENCES package_orders ON DELETE SET NULL ); CREATE INDEX stripe_checkouts_user_idx ON stripe_checkouts (user_id, created_at DESC); - --- One invoice per paid checkout, issued in Billingo by the worker. Every --- invoice detail is kept here (the buyer and the item as invoiced, Billingo's --- whole answer); the PDF itself is fetched from Billingo when downloaded. -CREATE TABLE invoices ( - id uuid PRIMARY KEY, - checkout_id text NOT NULL UNIQUE REFERENCES stripe_checkouts ON DELETE RESTRICT, - user_id uuid REFERENCES users ON DELETE SET NULL, - -- pending → issued; failed after the last retry (an admin looks at it). - status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'issued', 'failed')), - attempts integer NOT NULL DEFAULT 0, - next_attempt_at timestamptz NOT NULL DEFAULT now(), - last_error text, - -- The buyer as invoiced. - buyer_name text NOT NULL, - buyer_email text, - buyer_country text NOT NULL, - buyer_post_code text NOT NULL, - buyer_city text NOT NULL, - buyer_address text NOT NULL, - -- Billingo's partner tax type: HAS_TAX_NUMBER, NO_TAX_NUMBER or FOREIGN. - buyer_tax_type text NOT NULL CHECK (buyer_tax_type IN ('HAS_TAX_NUMBER', 'NO_TAX_NUMBER', 'FOREIGN')), - buyer_tax_number text, - -- The single item. vat_rate is Billingo's VAT value: AAM (alanyi - -- adómentes: net = gross, no VAT) or a rate like 27%. - item_name text NOT NULL, - gross_huf integer NOT NULL CHECK (gross_huf > 0), - net_huf integer NOT NULL, - vat_huf integer NOT NULL, - vat_rate text NOT NULL DEFAULT 'AAM', - payment_method text NOT NULL DEFAULT 'online_bankcard', - -- From Billingo. - billingo_partner_id bigint, - billingo_document_id bigint UNIQUE, - invoice_number text, - invoice_date date, - fulfillment_date date, - due_date date, - document jsonb, - -- When Billingo e-mailed it to the buyer. - sent_at timestamptz, - created_at timestamptz NOT NULL DEFAULT now(), - issued_at timestamptz -); - -CREATE INDEX invoices_user_idx ON invoices (user_id, created_at DESC); -CREATE INDEX invoices_due_idx ON invoices (next_attempt_at) WHERE status = 'pending'; diff --git a/crates/db/migrations/0028_payment_details_invoices.sql b/crates/db/migrations/0028_payment_details_invoices.sql new file mode 100644 index 0000000..f064f32 --- /dev/null +++ b/crates/db/migrations/0028_payment_details_invoices.sql @@ -0,0 +1,64 @@ +-- What a confirmed payment keeps for the invoice, and the invoices issued in +-- Billingo by the worker. Card details never reach us: only what Stripe +-- reports about the payment and the buyer's billing details. + +-- Payments outlive a deleted account: the accounting law asks for 8 years. +ALTER TABLE stripe_checkouts ALTER COLUMN user_id DROP NOT NULL; +ALTER TABLE stripe_checkouts DROP CONSTRAINT stripe_checkouts_user_id_fkey; +ALTER TABLE stripe_checkouts ADD CONSTRAINT stripe_checkouts_user_id_fkey + FOREIGN KEY (user_id) REFERENCES users ON DELETE SET NULL; + +ALTER TABLE stripe_checkouts + ADD COLUMN payment_intent text, + -- What Stripe charged, in forints. + ADD COLUMN amount_huf integer, + -- Stripe's customer_details as it came: name, e-mail, address, and for a + -- company its name and tax ids. + ADD COLUMN customer jsonb; + +-- One invoice per paid checkout. Every invoice detail is kept here (the buyer +-- and the item as invoiced, Billingo's whole answer); the PDF itself is +-- fetched from Billingo when downloaded. +CREATE TABLE invoices ( + id uuid PRIMARY KEY, + checkout_id text NOT NULL UNIQUE REFERENCES stripe_checkouts ON DELETE RESTRICT, + user_id uuid REFERENCES users ON DELETE SET NULL, + -- pending → issued; failed after the last retry (an admin looks at it). + status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'issued', 'failed')), + attempts integer NOT NULL DEFAULT 0, + next_attempt_at timestamptz NOT NULL DEFAULT now(), + last_error text, + -- The buyer as invoiced. + buyer_name text NOT NULL, + buyer_email text, + buyer_country text NOT NULL, + buyer_post_code text NOT NULL, + buyer_city text NOT NULL, + buyer_address text NOT NULL, + -- Billingo's partner tax type: HAS_TAX_NUMBER, NO_TAX_NUMBER or FOREIGN. + buyer_tax_type text NOT NULL CHECK (buyer_tax_type IN ('HAS_TAX_NUMBER', 'NO_TAX_NUMBER', 'FOREIGN')), + buyer_tax_number text, + -- The single item. vat_rate is Billingo's VAT value: AAM (alanyi + -- adómentes: net = gross, no VAT) or a rate like 27%. + item_name text NOT NULL, + gross_huf integer NOT NULL CHECK (gross_huf > 0), + net_huf integer NOT NULL, + vat_huf integer NOT NULL, + vat_rate text NOT NULL DEFAULT 'AAM', + payment_method text NOT NULL DEFAULT 'online_bankcard', + -- From Billingo. + billingo_partner_id bigint, + billingo_document_id bigint UNIQUE, + invoice_number text, + invoice_date date, + fulfillment_date date, + due_date date, + document jsonb, + -- When Billingo e-mailed it to the buyer. + sent_at timestamptz, + created_at timestamptz NOT NULL DEFAULT now(), + issued_at timestamptz +); + +CREATE INDEX invoices_user_idx ON invoices (user_id, created_at DESC); +CREATE INDEX invoices_due_idx ON invoices (next_attempt_at) WHERE status = 'pending';