Phase 5 billing: account-based annual sub + tag SKUs + SMS pool + tag lifecycle

- Schema: plans, orders subscription cols + plan_id + period_started_at,
  billing_events log, users.stripe_id + sms_credits, products.price_aud
  (db/schema.sql + Laravel migration, idempotent)
- Laravel/Cashier: Billable User, checkout (annual sub + one-off SKUs with
  Managed Payments tax_code), webhook controller (signature-verified,
  idempotent, BILLING_ENABLED kill-switch), account tag transitions,
  nightly reconcile, Stripe portal link, PlanResource + billing dashboard
- Go frontend: account-level gating (paid sub required), SMS pool
  (included 50/yr + credits, drawn after included), plan caps replace
  constants, 60s plan cache (credits fresh), 25-tag cap (plan max_tags)
- BillingSeeder: personal plan + 3 SKUs + dev paid orders
- Verified test-mode e2e: subscribe/paid/active/alerts, pool exhaust +
  credits resume, lapsed/suspended, cancelled/closed, recover/active,
  webhook idempotency, 25-cap, one-off SKUs, replacement, kill-switch,
  invalid signature 400
This commit is contained in:
2026-08-28 13:19:32 +10:00
parent 43ce4514c4
commit aabaa750f4
43 changed files with 2679 additions and 341 deletions

View File

@@ -100,3 +100,45 @@ ALTER TABLE tags ADD CONSTRAINT tags_status_check CHECK (status IN ('unset','act
-- Product photos (admin).
ALTER TABLE products ADD COLUMN IF NOT EXISTS photo_url TEXT;
-- Product price (AUD) for SKU checkout (one-off tag/credit products).
ALTER TABLE products ADD COLUMN IF NOT EXISTS price_aud NUMERIC(10,2) NOT NULL DEFAULT 0;
-- Phase 5: Billing (account-based annual subscription + tag SKUs + SMS metering).
-- Idempotent; canonical until Laravel owns migrations.
CREATE TABLE IF NOT EXISTS plans (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
plan_type TEXT NOT NULL DEFAULT 'personal'
CHECK (plan_type IN ('personal', 'business')),
name TEXT NOT NULL,
price_aud NUMERIC(10,2) NOT NULL DEFAULT 10.00,
billing_interval TEXT NOT NULL DEFAULT 'year'
CHECK (billing_interval IN ('month', 'year')),
sms_included INTEGER NOT NULL DEFAULT 50,
max_tags INTEGER NOT NULL DEFAULT 25,
alerts_per_day INTEGER NOT NULL DEFAULT 5,
alerts_per_hour INTEGER NOT NULL DEFAULT 10,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
-- Order subscription columns (Cashier) + plan linkage + billing period marker.
ALTER TABLE orders ADD COLUMN IF NOT EXISTS stripe_id TEXT;
ALTER TABLE orders ADD COLUMN IF NOT EXISTS pm_type TEXT;
ALTER TABLE orders ADD COLUMN IF NOT EXISTS pm_last_four TEXT;
ALTER TABLE orders ADD COLUMN IF NOT EXISTS trial_ends_at TIMESTAMPTZ;
ALTER TABLE orders ADD COLUMN IF NOT EXISTS plan_id BIGINT REFERENCES plans(id);
ALTER TABLE orders ADD COLUMN IF NOT EXISTS period_started_at TIMESTAMPTZ;
-- User billing columns: Stripe customer id + purchased SMS credits.
ALTER TABLE users ADD COLUMN IF NOT EXISTS stripe_id TEXT;
ALTER TABLE users ADD COLUMN IF NOT EXISTS sms_credits INTEGER NOT NULL DEFAULT 0;
-- Webhook audit log (idempotency + support).
CREATE TABLE IF NOT EXISTS billing_events (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
stripe_event_id TEXT NOT NULL UNIQUE,
event_type TEXT NOT NULL,
order_id BIGINT REFERENCES orders(id),
payload JSONB,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);