-- 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: name, e-mail, address, and for a company -- its name and tax ids. customer jsonb, -- The invoice (a background job issues it): pending → issued, or failed -- after retries (an admin looks at it). invoice_status text CHECK (invoice_status IN ('pending', 'issued', 'failed')), invoice_attempts integer NOT NULL DEFAULT 0, invoice_error text, invoice_number text, invoiced_at timestamptz ); CREATE INDEX stripe_checkouts_user_idx ON stripe_checkouts (user_id, created_at DESC); CREATE INDEX stripe_checkouts_invoice_idx ON stripe_checkouts (completed_at) WHERE invoice_status = 'pending';