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
+75
View File
@@ -83,6 +83,81 @@ func TestApplyEnvRejectsBadValues(t *testing.T) {
}
}
func TestMoneyDefaultsAndValidation(t *testing.T) {
cfg := Default()
cfg.Telegram.Token = "x"
p := cfg.Prices
if p.PassDay != 700 || p.PassWeek != 3000 || p.PassMonth != 9000 {
t.Fatalf("pass prices changed: %+v", p)
}
if cfg.Limits.Free.DailyDownloads != 5 || cfg.Limits.Paid.MaxSizeMB != 2000 ||
cfg.Limits.Paid.MaxDuration.D() != 3*time.Hour || cfg.Limits.Paid.ActiveJobs != 3 {
t.Fatalf("tier defaults changed: %+v", cfg.Limits)
}
if cfg.LNbits.Enabled() {
t.Fatal("payments must be off until LNbits is configured")
}
if err := cfg.Validate(); err != nil {
t.Fatalf("a config without payments must validate: %v", err)
}
// One of the two LNbits settings alone is a mistake.
cfg.LNbits.URL = "https://lnbits.example.com"
if err := cfg.Validate(); err == nil {
t.Fatal("LNBITS_URL without a key must be rejected")
}
cfg.LNbits.InvoiceKey = "key"
if err := cfg.Validate(); err != nil || !cfg.LNbits.Enabled() {
t.Fatalf("complete LNbits settings: %v", err)
}
cfg.Prices.PassDay = 0
if err := cfg.Validate(); err == nil {
t.Fatal("a zero pass price must be rejected when payments are on")
}
cfg.Prices.PassDay = 700
cfg.LNbits.InvoiceExpiry = Duration(10 * time.Second)
if err := cfg.Validate(); err == nil {
t.Fatal("an invoice expiry under a minute must be rejected")
}
// One-off pricing: base plus one step per started 100 MB.
prices := map[int64]int64{
0: 200, // unknown size is priced as 200 MB
1: 150,
100 << 20: 150,
101 << 20: 200,
1 << 30: 100 + 11*50,
}
for size, want := range prices {
if got := p.OneOffPrice(size); got != want {
t.Errorf("OneOffPrice(%d) = %d, want %d", size, got, want)
}
}
if p.PassPrice("week") != 3000 || p.PassPrice("decade") != 0 {
t.Fatal("PassPrice is wrong")
}
}
func TestMoneyEnv(t *testing.T) {
cfg := Default()
env := map[string]string{
"PRICE_PASS_DAY": "1000", "CREDIT_PACKS": "100, 200", "LNBITS_URL": "https://ln.example.com/",
"RATE_USER_PER_MINUTE": "3", "PUBLIC_URL": "https://bot.example.com/",
}
if err := applyEnv(reflect.ValueOf(&cfg).Elem(), lookup(env)); err != nil {
t.Fatal(err)
}
if cfg.Prices.PassDay != 1000 || !reflect.DeepEqual(cfg.Prices.CreditPacks, []int64{100, 200}) ||
cfg.Rate.UserPerMinute != 3 || cfg.LNbits.URL != "https://ln.example.com/" {
t.Fatalf("env not applied: %+v %+v", cfg.Prices, cfg.Rate)
}
if err := applyTierEnv("PAID", &cfg.Limits.Paid, lookup(map[string]string{
"PAID_DAILY_DOWNLOADS": "7", "PAID_DAILY_MB": "9",
})); err != nil || cfg.Limits.Paid.DailyDownloads != 7 || cfg.Limits.Paid.DailyMB != 9 {
t.Fatalf("paid tier env: %+v, %v", cfg.Limits.Paid, err)
}
}
func TestValidate(t *testing.T) {
bad := []func(*Config){
func(c *Config) { c.Telegram.Mode = "carrier-pigeon" },