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
+20 -1
View File
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"html"
"strconv"
"strings"
"time"
@@ -36,6 +37,16 @@ type linkRequest struct {
// startLink runs the link flow in the background so a slow metadata lookup
// never blocks other updates of the chat.
func (b *Bot) startLink(ctx context.Context, lr linkRequest) {
userOK := b.rate.allow("u:"+strconv.FormatInt(lr.act.user.ID, 10), b.cfg.Rate.UserPerMinute, time.Minute)
groupOK := isPrivate(lr.chatType) ||
b.rate.allow("g:"+strconv.FormatInt(lr.chatID, 10), b.cfg.Rate.GroupPerMinute, time.Minute)
if !userOK || !groupOK {
// One notice per minute is enough; more would be its own flood.
if b.rate.allow("n:"+strconv.FormatInt(lr.chatID, 10)+":"+strconv.FormatInt(lr.act.user.ID, 10), 1, time.Minute) {
_, _ = b.send(ctx, lr.chatID, lr.sourceID, b.tr.HTML(lr.act.lang, "notice.rate_limited", nil), nil)
}
return
}
b.goBG("link", func() { b.linkFlow(ctx, lr) })
}
@@ -104,7 +115,7 @@ func (b *Bot) linkFlow(ctx context.Context, lr linkRequest) {
ReplyMarkup: kb,
})
if err == nil {
if err := b.store.SetRequestCard(ctx, req.ID, int64(msg.MessageID), true); err != nil {
if err := b.store.SetRequestCard(context.WithoutCancel(ctx), req.ID, int64(msg.MessageID), true); err != nil {
log.Error("save card", "err", err)
}
b.deleteMessage(ctx, lr.chatID, phID)
@@ -200,6 +211,14 @@ func (b *Bot) cbDownload(ctx context.Context, q *telego.CallbackQuery, act actor
if _, rerr := b.store.TransitionRequest(ctx, req.ID, db.RequestChosen, db.RequestReady); rerr != nil {
b.log.Error("release request", "request_id", req.ID, "err", rerr)
}
var le *service.LimitError
if errors.As(err, &le) && le.Upgradable {
// Over the limit, but a pass or a one-off payment would cover
// it: offer those instead of a dead end.
b.answer(ctx, q, "", false)
b.offerUpgrade(ctx, req.ChatID, cardID, act.lang, req, profile, le, isPrivate(req.ChatType))
return
}
b.answer(ctx, q, b.startErrorText(act.lang, err), true)
return
}