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