Lightning payments through LNbits, wallets, passes, credits, the entitlement resolver and priority lanes. - internal/entitle: pure resolver (one-off, own pass, group pass, group credits, own credits, free) with a table-driven suite and an invariant check over every input combination - internal/billing: wallets, append-only ledger with dedupe keys, stacking passes, purchases, gift codes, referral reward - LNbits client verified against v0.12.12 and v1.7.1 source - Settlement by one conditional UPDATE: idempotent under any mix of webhooks, polls and the check button. Webhooks are unsigned, so every payment is confirmed with LNbits before it is credited - Polling fallback, reconciliation of all unsettled invoices at startup, late payments still honored, no expiry while LNbits is unreachable - Charge, quota and job are written in one transaction; failed, cancelled and undeliverable downloads are refunded exactly once - Paid lane first with one slot of the fleet reserved for free users - Daily download quota and daily volume cap, per-user and per-group rate limits - Bot: /buy /account /limits /credits /redeem /referral /donate, QR invoice message that flips to paid, over-limit offer (passes or this file only), deep links from groups, skip the queue - Fake LNbits server for tests; end-to-end money flows including a payment made while the core was down Cashu redemption is behind an interface and not implemented. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
115 lines
4.1 KiB
SQL
115 lines
4.1 KiB
SQL
-- +goose Up
|
|
-- Money. The ledger is append-only: rows are inserted, never updated or
|
|
-- deleted. wallets.balance is a cached sum that is updated in the same
|
|
-- transaction as each ledger row and can be checked against the ledger.
|
|
|
|
CREATE TABLE wallets (
|
|
id TEXT PRIMARY KEY,
|
|
owner_type TEXT NOT NULL,
|
|
owner_id BIGINT NOT NULL,
|
|
balance BIGINT NOT NULL DEFAULT 0,
|
|
created_at BIGINT NOT NULL,
|
|
UNIQUE (owner_type, owner_id)
|
|
);
|
|
|
|
CREATE TABLE ledger (
|
|
id TEXT PRIMARY KEY,
|
|
wallet_id TEXT NOT NULL REFERENCES wallets (id),
|
|
kind TEXT NOT NULL,
|
|
amount BIGINT NOT NULL,
|
|
balance_after BIGINT NOT NULL,
|
|
reason TEXT NOT NULL,
|
|
ref TEXT NOT NULL DEFAULT '',
|
|
dedupe_key TEXT NOT NULL UNIQUE,
|
|
actor_id BIGINT NOT NULL DEFAULT 0,
|
|
created_at BIGINT NOT NULL
|
|
);
|
|
CREATE INDEX ledger_wallet_idx ON ledger (wallet_id, created_at);
|
|
|
|
CREATE TABLE passes (
|
|
id TEXT PRIMARY KEY,
|
|
wallet_id TEXT NOT NULL REFERENCES wallets (id),
|
|
kind TEXT NOT NULL,
|
|
starts_at BIGINT NOT NULL,
|
|
expires_at BIGINT NOT NULL,
|
|
source TEXT NOT NULL DEFAULT '',
|
|
created_at BIGINT NOT NULL
|
|
);
|
|
CREATE INDEX passes_wallet_idx ON passes (wallet_id, expires_at);
|
|
|
|
-- A purchase is one invoice. payment_hash maps the Lightning payment back
|
|
-- to the purchase and through it to the wallet that gets credited.
|
|
CREATE TABLE purchases (
|
|
id TEXT PRIMARY KEY,
|
|
user_id BIGINT NOT NULL,
|
|
wallet_id TEXT NOT NULL REFERENCES wallets (id),
|
|
product TEXT NOT NULL,
|
|
amount_sats BIGINT NOT NULL,
|
|
status TEXT NOT NULL,
|
|
payment_hash TEXT NOT NULL UNIQUE,
|
|
bolt11 TEXT NOT NULL,
|
|
chat_id BIGINT NOT NULL DEFAULT 0,
|
|
message_id BIGINT NOT NULL DEFAULT 0,
|
|
message_is_photo BOOLEAN NOT NULL DEFAULT FALSE,
|
|
lang TEXT NOT NULL DEFAULT 'en',
|
|
request_id TEXT NOT NULL DEFAULT '',
|
|
profile TEXT NOT NULL DEFAULT '',
|
|
delivery_id TEXT NOT NULL DEFAULT '',
|
|
fulfilled BOOLEAN NOT NULL DEFAULT FALSE,
|
|
created_at BIGINT NOT NULL,
|
|
expires_at BIGINT NOT NULL,
|
|
paid_at BIGINT NOT NULL DEFAULT 0
|
|
);
|
|
CREATE INDEX purchases_status_idx ON purchases (status, created_at);
|
|
CREATE INDEX purchases_user_idx ON purchases (user_id, status);
|
|
|
|
CREATE TABLE usage_daily (
|
|
user_id BIGINT NOT NULL,
|
|
day INTEGER NOT NULL,
|
|
downloads INTEGER NOT NULL DEFAULT 0,
|
|
bytes BIGINT NOT NULL DEFAULT 0,
|
|
PRIMARY KEY (user_id, day)
|
|
);
|
|
|
|
CREATE TABLE gift_codes (
|
|
code TEXT PRIMARY KEY,
|
|
kind TEXT NOT NULL,
|
|
amount BIGINT NOT NULL,
|
|
max_uses INTEGER NOT NULL DEFAULT 1,
|
|
uses INTEGER NOT NULL DEFAULT 0,
|
|
expires_at BIGINT NOT NULL DEFAULT 0,
|
|
created_by BIGINT NOT NULL DEFAULT 0,
|
|
created_at BIGINT NOT NULL
|
|
);
|
|
|
|
CREATE TABLE gift_redemptions (
|
|
code TEXT NOT NULL REFERENCES gift_codes (code),
|
|
user_id BIGINT NOT NULL,
|
|
created_at BIGINT NOT NULL,
|
|
PRIMARY KEY (code, user_id)
|
|
);
|
|
|
|
ALTER TABLE users ADD COLUMN referred_by BIGINT NOT NULL DEFAULT 0;
|
|
|
|
-- What paid for a delivery, so a failed download can be refunded exactly.
|
|
ALTER TABLE deliveries ADD COLUMN source TEXT NOT NULL DEFAULT 'free';
|
|
ALTER TABLE deliveries ADD COLUMN wallet_id TEXT NOT NULL DEFAULT '';
|
|
ALTER TABLE deliveries ADD COLUMN charge_sats BIGINT NOT NULL DEFAULT 0;
|
|
ALTER TABLE deliveries ADD COLUMN quota_day INTEGER NOT NULL DEFAULT 0;
|
|
ALTER TABLE deliveries ADD COLUMN released BOOLEAN NOT NULL DEFAULT FALSE;
|
|
|
|
-- +goose Down
|
|
ALTER TABLE deliveries DROP COLUMN released;
|
|
ALTER TABLE deliveries DROP COLUMN quota_day;
|
|
ALTER TABLE deliveries DROP COLUMN charge_sats;
|
|
ALTER TABLE deliveries DROP COLUMN wallet_id;
|
|
ALTER TABLE deliveries DROP COLUMN source;
|
|
ALTER TABLE users DROP COLUMN referred_by;
|
|
DROP TABLE gift_redemptions;
|
|
DROP TABLE gift_codes;
|
|
DROP TABLE usage_daily;
|
|
DROP TABLE purchases;
|
|
DROP TABLE passes;
|
|
DROP TABLE ledger;
|
|
DROP TABLE wallets;
|