Files
sorsfonalak/crates/db/migrations/0027_stripe_checkouts.sql
T
fecoandClaude Opus 5.5 2a0830fda0
CI / test (push) Successful in 1m29s
CI / build (push) Successful in 9m7s
CI / deploy (push) Skipped
Invoices: issued in Billingo by the worker, downloadable on Számláim
A paid checkout queues an invoice row with every invoice detail (buyer,
tax type and number, item, net, VAT, gross). The worker's new 'invoices'
task creates the Billingo partner and the paid card invoice and e-mails
it (API v3), saving each step so a retry never issues a second invoice,
and keeps Billingo's whole answer; the PDF is fetched from Billingo when
the user downloads it from the new Számláim page (in the menu once there
is an invoice). Payments and invoices outlive a deleted account.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-20 07:52:34 +00:00

74 lines
3.5 KiB
SQL

-- 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, gross price with 27% VAT.
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 '27%',
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';