Phase 2: money

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>
This commit is contained in:
Michilis
2026-11-18 08:09:35 +00:00
co-authored by Claude Fable 5.2
parent 3b361c17a1
commit 0d50f453a0
42 changed files with 7040 additions and 140 deletions
+114
View File
@@ -47,6 +47,9 @@ type User struct {
Banned bool
CreatedAt int64
LastSeenAt int64
ReferredBy int64 // user who invited this one, 0 for none
// IsNew is set by UpsertUser when the row was just created.
IsNew bool
}
// Request is one link sent by a user.
@@ -129,4 +132,115 @@ type Delivery struct {
TgMessageID int64
CreatedAt int64
UpdatedAt int64
// Source is the entitlement that paid: see package entitle.
Source string
// WalletID is the wallet that was charged, "" for free and passes.
WalletID string
// ChargeSats is what was debited (or prepaid) for this delivery.
ChargeSats int64
// QuotaDay is the usage day (YYYYMMDD) the download was counted
// against, 0 when it was not counted.
QuotaDay int
// Released is true once charge and quota were given back.
Released bool
}
// Wallet owner types.
const (
OwnerUser = "user"
OwnerGroup = "group"
)
// Ledger entry kinds. Amounts are always positive; the kind gives the sign.
const (
LedgerCredit = "credit"
LedgerDebit = "debit"
LedgerRefund = "refund"
)
// Purchase statuses.
const (
PurchasePending = "pending"
PurchasePaid = "paid"
PurchaseExpired = "expired"
PurchaseCancelled = "cancelled"
)
// Wallet holds credits for a user or a group.
type Wallet struct {
ID string
OwnerType string
OwnerID int64
Balance int64 // sats, cached sum of the ledger
CreatedAt int64
}
// LedgerEntry is one append-only movement on a wallet.
type LedgerEntry struct {
ID string
WalletID string
Kind string
Amount int64
BalanceAfter int64
Reason string
Ref string
// DedupeKey makes an entry idempotent: a second insert with the same
// key is ignored.
DedupeKey string
ActorID int64
CreatedAt int64
}
// Pass is a time window of paid limits on a wallet.
type Pass struct {
ID string
WalletID string
Kind string
StartsAt int64
ExpiresAt int64
Source string
CreatedAt int64
}
// Purchase is one invoice and what it buys.
type Purchase struct {
ID string
UserID int64 // the payer
WalletID string
Product string
AmountSats int64
Status string
PaymentHash string
Bolt11 string
ChatID int64
MessageID int64
MessageIsPhoto bool
Lang string
// RequestID and Profile describe a download to start once paid.
RequestID string
Profile string
// DeliveryID is the waiting delivery a skip-the-queue payment is for.
DeliveryID string
Fulfilled bool
CreatedAt int64
ExpiresAt int64
PaidAt int64
}
// Usage is what a user consumed on one UTC day.
type Usage struct {
Downloads int
Bytes int64
}
// GiftCode is a redeemable code.
type GiftCode struct {
Code string
Kind string // "credits" or a pass kind
Amount int64 // sats for credits, ignored for passes
MaxUses int
Uses int
ExpiresAt int64
CreatedBy int64
CreatedAt int64
}