Phase 4: Telegram Stars payments

- Stars as a second payment method for passes and credit packs, next to
  Lightning (STARS_ENABLED, prices in [stars_prices] / STARS_PRICES, set
  on their own). Invoices in XTR with no provider token and one price;
  pre-checkout checks payer, state, currency and amount;
  successful_payment settles through the same transaction as Lightning,
  so entitlement is identical. Idempotent by telegram_payment_charge_id
  (unique index).
- Migration 00009: payment_method on purchases and ledger entries, plus
  stars_amount, tg_charge_id and refunded_at on purchases. Stars
  purchases never reach LNbits.
- Chat: buy screen, /credits, /account and the over-limit offer show
  Lightning first and a Pay with Stars button.
- Mini App: Lightning and Stars switch on Buy; Stars use
  createInvoiceLink and WebApp.openInvoice, confirmed by the server-side
  payment event.
- Owner /refund <purchase or charge ID> calls refundStarPayment and takes
  back the credits or pass; refunded_payment messages do the same once.
  /stats shows revenue by payment method.
- The sats referral reward counts Lightning purchases only.
- The bot now asks Telegram for pre_checkout_query updates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-18 21:20:03 +00:00
co-authored by Claude Opus 5.5
parent f7ceadc462
commit 2b5625e9d4
37 changed files with 1915 additions and 114 deletions
+32 -6
View File
@@ -491,13 +491,26 @@ func (m *MiniApp) products(w http.ResponseWriter, _ *http.Request, s *session) {
if up := m.Cfg.MaxUploadBytes(); up > 0 && up < size {
size = up
}
stars := func(product string, amount int64) int64 {
n, _ := m.Billing.StarsPrice(product, amount)
return n
}
starsPacks := []map[string]int64{}
for _, a := range p.CreditPacks {
if n := stars(billing.ProductCredits, a); n > 0 {
starsPacks = append(starsPacks, map[string]int64{"amount": a, "stars": n})
}
}
m.ok(w, map[string]any{
"enabled": m.Billing.Enabled(),
"enabled": m.Billing.Selling(),
"lightning": m.Billing.Enabled(),
"stars": m.Billing.StarsEnabled(),
"passes": []map[string]any{
{"product": billing.ProductPassDay, "price": p.PassDay},
{"product": billing.ProductPassWeek, "price": p.PassWeek},
{"product": billing.ProductPassMonth, "price": p.PassMonth},
{"product": billing.ProductPassDay, "price": p.PassDay, "stars": stars(billing.ProductPassDay, 0)},
{"product": billing.ProductPassWeek, "price": p.PassWeek, "stars": stars(billing.ProductPassWeek, 0)},
{"product": billing.ProductPassMonth, "price": p.PassMonth, "stars": stars(billing.ProductPassMonth, 0)},
},
"stars_packs": starsPacks,
"credit_packs": p.CreditPacks,
"credit_per_download": p.CreditPerDownload,
"paid": tierJSON{MaxSize: size, MaxDuration: int64(paid.MaxDuration.D().Seconds()), ActiveJobs: paid.ActiveJobs, DailyDownloads: paid.DailyDownloads, DailyBytes: paid.DailyMB << 20},
@@ -510,9 +523,9 @@ func (m *MiniApp) products(w http.ResponseWriter, _ *http.Request, s *session) {
func (m *MiniApp) purchaseJSON(p *db.Purchase, withInvoice bool) map[string]any {
out := map[string]any{
"id": p.ID, "product": p.Product, "amount": p.AmountSats, "status": p.Status,
"expires_at": p.ExpiresAt, "paid_at": p.PaidAt,
"expires_at": p.ExpiresAt, "paid_at": p.PaidAt, "method": p.PaymentMethod, "stars": p.StarsAmount,
}
if withInvoice && p.Status == db.PurchasePending {
if withInvoice && p.Status == db.PurchasePending && p.PaymentMethod != db.PayStars {
out["bolt11"] = p.Bolt11
if png, err := qrcode.Encode(strings.ToUpper(p.Bolt11), qrcode.Medium, 512); err == nil {
out["qr"] = "data:image/png;base64," + base64.StdEncoding.EncodeToString(png)
@@ -526,6 +539,8 @@ func (m *MiniApp) purchase(w http.ResponseWriter, r *http.Request, s *session) {
Target string `json:"target"` // "me" or a group chat ID
Product string `json:"product"`
Amount int64 `json:"amount"`
// Method is "lightning" (the default) or "stars".
Method string `json:"method"`
}
if err := decode(r, &in); err != nil {
m.fail(w, http.StatusBadRequest, "bad_request", "")
@@ -548,6 +563,17 @@ func (m *MiniApp) purchase(w http.ResponseWriter, r *http.Request, s *session) {
}
groupID = id
}
if in.Method == db.PayStars {
p, link, err := m.Bot.AppStarsPurchase(r.Context(), s.user, s.lang, groupID, in.Product, in.Amount)
if err != nil {
m.explain(w, s, err)
return
}
out := m.purchaseJSON(p, true)
out["invoice_link"] = link
m.ok(w, out)
return
}
p, err := m.Bot.AppPurchase(r.Context(), s.user, s.lang, groupID, in.Product, in.Amount, "")
if err != nil {
m.explain(w, s, err)