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:
co-authored by
Claude Fable 5.2
parent
3b361c17a1
commit
0d50f453a0
@@ -0,0 +1,87 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"crypto/subtle"
|
||||
"html"
|
||||
"net/http"
|
||||
|
||||
"github.com/noderunners/dlbot/internal/billing"
|
||||
"github.com/noderunners/dlbot/internal/db"
|
||||
"github.com/noderunners/dlbot/internal/i18n"
|
||||
)
|
||||
|
||||
// Payments serves the two payment routes of the core:
|
||||
//
|
||||
// POST /lnbits/webhook/{secret} payment notifications from LNbits
|
||||
// GET /pay/{purchase} "open in wallet" page for an invoice
|
||||
type Payments struct {
|
||||
Billing *billing.Billing
|
||||
Store *db.Store
|
||||
I18n *i18n.Bundle
|
||||
// Secret is the random path segment that LNbits was given. An empty
|
||||
// secret disables the webhook route.
|
||||
Secret string
|
||||
}
|
||||
|
||||
// Register adds the routes to the server.
|
||||
func (s *Server) RegisterPayments(p Payments) {
|
||||
if p.Secret != "" {
|
||||
s.mux.HandleFunc("POST /lnbits/webhook/{secret}", p.webhook)
|
||||
}
|
||||
s.mux.HandleFunc("GET /pay/{purchase}", p.payPage)
|
||||
}
|
||||
|
||||
// webhook accepts a notification. LNbits does not sign its webhooks, so
|
||||
// the secret in the path only keeps strangers out; the payment itself is
|
||||
// confirmed with LNbits by the billing service before anything is
|
||||
// credited. The answer is always 200 for a well-formed request: LNbits
|
||||
// does not retry, and polling covers anything that fails here.
|
||||
func (p Payments) webhook(w http.ResponseWriter, r *http.Request) {
|
||||
if subtle.ConstantTimeCompare([]byte(r.PathValue("secret")), []byte(p.Secret)) != 1 {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
hash, ok := billing.ParseWebhook(r.Body)
|
||||
if !ok {
|
||||
http.Error(w, "bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
// Errors are logged by the billing service and retried by its poller.
|
||||
_ = p.Billing.HandleWebhook(r.Context(), hash)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
// payPage forwards a browser to the lightning: link of an invoice.
|
||||
// Telegram URL buttons only accept http and https, which is why this small
|
||||
// hop exists. The purchase ID in the path is unguessable, and the page
|
||||
// shows nothing but the invoice, which is meant to be shared with a wallet.
|
||||
func (p Payments) payPage(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.Header().Set("Referrer-Policy", "no-referrer")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
pur, err := p.Store.GetPurchase(r.Context(), r.PathValue("purchase"))
|
||||
lang := i18n.Default
|
||||
if err == nil {
|
||||
lang = pur.Lang
|
||||
}
|
||||
t := func(id string) string { return html.EscapeString(p.I18n.T(lang, id, nil)) }
|
||||
page := func(code int, body string) {
|
||||
w.WriteHeader(code)
|
||||
_, _ = w.Write([]byte(`<!doctype html><html lang="` + html.EscapeString(lang) + `"><head><meta charset="utf-8">` +
|
||||
`<meta name="viewport" content="width=device-width, initial-scale=1"><title>` + t("pay.title") +
|
||||
`</title><style>body{font-family:system-ui,sans-serif;margin:0;min-height:100vh;display:grid;` +
|
||||
`place-items:center;background:#111;color:#eee;text-align:center}main{padding:24px;max-width:480px}` +
|
||||
`a.btn{display:inline-block;margin:16px 0;padding:14px 22px;border-radius:12px;background:#f7931a;` +
|
||||
`color:#111;font-weight:600;text-decoration:none}code{display:block;word-break:break-all;` +
|
||||
`font-size:12px;opacity:.7;margin-top:16px}</style>` + body + `</html>`))
|
||||
}
|
||||
if err != nil || pur.Status != db.PurchasePending {
|
||||
page(http.StatusGone, `</head><body><main><p>`+t("pay.gone")+`</p></main></body>`)
|
||||
return
|
||||
}
|
||||
link := "lightning:" + pur.Bolt11
|
||||
esc := html.EscapeString(link)
|
||||
page(http.StatusOK, `<meta http-equiv="refresh" content="0;url=`+esc+`"></head><body><main><p>`+t("pay.hint")+
|
||||
`</p><a class="btn" href="`+esc+`">`+t("pay.open")+`</a><code>`+html.EscapeString(pur.Bolt11)+`</code></main></body>`)
|
||||
}
|
||||
Reference in New Issue
Block a user