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
+53 -15
View File
@@ -77,19 +77,30 @@ func (b *Bot) cmdBuy(ctx context.Context, m *telego.Message, act actor) {
}
func (b *Bot) showBuy(ctx context.Context, chat int64, act actor) {
if !b.bill.Enabled() {
if !b.bill.Selling() {
_, _ = b.send(ctx, chat, 0, b.tr.HTML(act.lang, "buy.unavailable", nil), nil)
return
}
text, kb := b.buyView(act.lang)
_, _ = b.send(ctx, chat, 0, text, kb)
}
// buyView is the buy screen: Lightning prices first, and a button to the
// Stars prices when Stars are on. Without Lightning it is the Stars prices.
func (b *Bot) buyView(lang string) (string, *telego.InlineKeyboardMarkup) {
paid := b.cfg.Limits.Paid
rows := b.passButtons(act.lang, func(product string) string { return cbData(cbBuy, product) })
rows = append(rows, b.amountButtons(act.lang, billing.ProductCredits, b.cfg.Prices.CreditPacks))
text := b.tr.HTML(act.lang, "buy.intro", i18n.Args{
"MaxSize": b.tr.Size(act.lang, min64(paid.MaxSizeMB<<20, b.cfg.MaxUploadBytes())),
"MaxDuration": b.tr.Span(act.lang, paid.MaxDuration.D()),
"PerDownload": b.tr.Sats(act.lang, b.cfg.Prices.CreditPerDownload),
text := b.tr.HTML(lang, "buy.intro", i18n.Args{
"MaxSize": b.tr.Size(lang, min64(paid.MaxSizeMB<<20, b.cfg.MaxUploadBytes())),
"MaxDuration": b.tr.Span(lang, paid.MaxDuration.D()),
"PerDownload": b.tr.Sats(lang, b.cfg.Prices.CreditPerDownload),
})
_, _ = b.send(ctx, chat, 0, text, keyboard(rows...))
if !b.bill.Enabled() {
stars, kb := b.starsMenu(lang)
return text + "\n\n" + stars, kb
}
rows := b.passButtons(lang, func(product string) string { return cbData(cbBuy, product) })
rows = append(rows, b.amountButtons(lang, billing.ProductCredits, b.cfg.Prices.CreditPacks), b.starsMenuButton(lang))
return text, keyboard(rows...)
}
func min64(a, b int64) int64 {
@@ -113,8 +124,12 @@ func (b *Bot) cmdCredits(ctx context.Context, m *telego.Message, act actor) {
"PerDownload": b.tr.Sats(act.lang, b.cfg.Prices.CreditPerDownload),
})
var kb *telego.InlineKeyboardMarkup
if b.bill.Enabled() {
kb = keyboard(b.amountButtons(act.lang, billing.ProductCredits, b.cfg.Prices.CreditPacks))
if b.bill.Selling() {
var packs []telego.InlineKeyboardButton
if b.bill.Enabled() {
packs = b.amountButtons(act.lang, billing.ProductCredits, b.cfg.Prices.CreditPacks)
}
kb = keyboard(packs, b.starsMenuButton(act.lang))
}
_, _ = b.send(ctx, m.Chat.ID, 0, text, kb)
}
@@ -173,8 +188,12 @@ func (b *Bot) cmdAccount(ctx context.Context, m *telego.Message, act actor) {
"Used": b.tr.Size(lang, usage.Bytes), "Max": b.capSize(lang, dec.Limits.DailyBytes),
}))
var kb *telego.InlineKeyboardMarkup
if b.bill.Enabled() {
kb = keyboard(b.passButtons(lang, func(product string) string { return cbData(cbBuy, product) })...)
if b.bill.Selling() {
var rows [][]telego.InlineKeyboardButton
if b.bill.Enabled() {
rows = b.passButtons(lang, func(product string) string { return cbData(cbBuy, product) })
}
kb = keyboard(append(rows, b.starsMenuButton(lang))...)
}
_, _ = b.send(ctx, m.Chat.ID, 0, sb.String(), kb)
}
@@ -656,7 +675,14 @@ func (b *Bot) handlePayEvent(ctx context.Context, ev billing.Event) {
}
b.showPurchaseState(ctx, p)
if ev.Type == billing.EventPaid {
if p.MessageID == 0 {
switch {
case p.MessageID == 0 && p.PaymentMethod == db.PayStars:
// Telegram's invoice message cannot be edited into "paid", and
// the payment arrived as a message: confirm it right away.
if msg, err := b.send(ctx, p.UserID, 0, b.paidText(ctx, p), nil); err == nil {
_ = b.store.SetPurchaseMessage(context.WithoutCancel(ctx), p.ID, p.UserID, int64(msg.MessageID), false)
}
case p.MessageID == 0:
b.goBG("confirm-payment", func() { b.confirmWithoutInvoice(ctx, p.ID) })
}
b.fulfil(ctx, p)
@@ -686,9 +712,13 @@ func (b *Bot) confirmWithoutInvoice(ctx context.Context, purchaseID string) {
// paidText is what the invoice message becomes once it is settled.
func (b *Bot) paidText(ctx context.Context, p *db.Purchase) string {
lang := p.Lang
amount := b.tr.Sats(lang, p.AmountSats)
if p.PaymentMethod == db.PayStars {
amount = b.starsAmount(lang, p.StarsAmount)
}
text := b.tr.HTML(lang, "invoice.paid", i18n.Args{
"Title": b.productTitle(lang, p.Product),
"Amount": b.tr.Sats(lang, p.AmountSats),
"Amount": amount,
})
w, err := b.store.GetWallet(ctx, p.WalletID)
if err != nil {
@@ -860,7 +890,15 @@ func (b *Bot) offerUpgrade(ctx context.Context, chat, replyTo int64, lang string
rows = b.passButtons(lang, func(product string) string {
return cbData(cbOfferBuy, product, req.ID, string(profile))
})
if le.Code == service.LimitTooLarge || le.Code == service.LimitTooLong {
if !b.bill.Enabled() {
rows = nil // Stars only: the passes come with the Stars button
}
if b.bill.StarsEnabled() {
rows = append(rows, []telego.InlineKeyboardButton{
button(b.tr.T(lang, "btn.pay_stars", nil), cbData(cbStarsOffer, req.ID, string(profile))),
})
}
if b.bill.Enabled() && (le.Code == service.LimitTooLarge || le.Code == service.LimitTooLong) {
price := b.cfg.Prices.OneOffPrice(le.Size)
rows = append(rows, []telego.InlineKeyboardButton{
button(b.priced(lang, b.tr.T(lang, "btn.this_file", nil), price),