Skip to main content

max / makenotwork

1.7 KB · 31 lines History Blame Raw
1 -- Platform-funded credits (Fan+ renewal credit reimbursement).
2 --
3 -- A platform-wide credit (the $5 Fan+ renewal credit) reduces the amount a buyer
4 -- pays on a creator's Direct Charge. To honour the "0% platform fee, creators keep
5 -- everything" promise, MNW reimburses the creator the discounted amount via a
6 -- platform -> connected transfer. `platform_credit_cents` records how much MNW owes
7 -- the creator on a given transaction; the scheduler sweep settles it and stamps
8 -- `platform_credit_settled_at`.
9 --
10 -- Lifecycle (mirrors pending_refunds): a completed transaction with
11 -- platform_credit_cents > 0 and settled_at IS NULL is claimed (claimed_at set),
12 -- the transfer is created with a deterministic idempotency key, then settled_at is
13 -- stamped. A graceful transfer failure clears claimed_at for retry; a process death
14 -- between claim and settle leaves claimed_at set / settled_at NULL and is escalated
15 -- by the stale sweep (escalated_at) rather than blindly retried.
16
17 ALTER TABLE transactions
18 ADD COLUMN IF NOT EXISTS platform_credit_cents BIGINT NOT NULL DEFAULT 0,
19 ADD COLUMN IF NOT EXISTS platform_credit_claimed_at TIMESTAMPTZ,
20 ADD COLUMN IF NOT EXISTS platform_credit_settled_at TIMESTAMPTZ,
21 ADD COLUMN IF NOT EXISTS platform_credit_escalated_at TIMESTAMPTZ;
22
23 ALTER TABLE transactions
24 ADD CONSTRAINT platform_credit_cents_nonneg CHECK (platform_credit_cents >= 0);
25
26 -- Partial index for the settlement sweep: only rows that actually owe a credit and
27 -- have not yet settled.
28 CREATE INDEX IF NOT EXISTS idx_transactions_platform_credit_owed
29 ON transactions (completed_at)
30 WHERE platform_credit_cents > 0 AND platform_credit_settled_at IS NULL;
31