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
+28 -1
View File
@@ -52,6 +52,12 @@ func (b *Bot) ownerCommand(ctx context.Context, m *telego.Message, act actor, cm
case "credit":
b.cmdCredit(ctx, m, act, false)
case "refund":
// /refund <purchase or charge ID> returns a Stars payment;
// /refund <user> <sats> gives sats back, as before.
if f := strings.Fields(commandArgs(m)); len(f) == 1 && !isDigits(f[0]) {
b.refundStars(ctx, m, act, f[0])
return true
}
b.cmdCredit(ctx, m, act, true)
case "blocklist":
b.cmdBlocklist(ctx, m, act)
@@ -97,7 +103,7 @@ func (b *Bot) cmdStats(ctx context.Context, m *telego.Message, act actor) {
}
}
maint, _ := b.store.GetKV(ctx, "maintenance")
b.ownerReply(ctx, m, act, "owner.stats", i18n.Args{
text := b.tr.HTML(lang, "owner.stats", i18n.Args{
"Users": b.tr.Number(lang, users),
"Done": b.tr.Number(lang, jobs[db.JobDone]),
"Failed": b.tr.Number(lang, jobs[db.JobFailed]),
@@ -109,6 +115,27 @@ func (b *Bot) cmdStats(ctx context.Context, m *telego.Message, act actor) {
"Created": bst.InvoicesCreated, "Settled": bst.InvoicesSettled,
"Lightning": ln, "Books": books, "Maint": b.onOff(lang, maint == "1"),
})
// Revenue by payment method, all time.
if revenue, err := b.store.RevenueByMethod(ctx, 0); err == nil && len(revenue) > 0 {
text += "\n\n" + b.tr.HTML(lang, "owner.revenue_title", nil)
for _, r := range revenue {
key, amount := "owner.revenue_lightning", b.tr.Sats(lang, r.Sats)
if r.Method == db.PayStars {
key, amount = "owner.revenue_stars", b.starsAmount(lang, r.Stars)
}
text += "\n" + b.tr.HTML(lang, key, i18n.Args{"Amount": amount, "Count": r.Purchases, "Refunded": r.Refunded})
}
}
_, _ = b.send(ctx, m.Chat.ID, 0, text, nil)
}
func isDigits(s string) bool {
for _, r := range s {
if r < '0' || r > '9' {
return false
}
}
return s != ""
}
// cmdQueueAll is /queue all for owners: every queued and running job.