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:
co-authored by
Claude Opus 5.5
parent
f7ceadc462
commit
2b5625e9d4
+38
-1
@@ -226,6 +226,42 @@ func (b *Bot) AppPurchase(ctx context.Context, user *db.User, lang string, group
|
||||
})
|
||||
}
|
||||
|
||||
// AppStarsPurchase creates a Stars purchase from the Mini App and an
|
||||
// invoice link for WebApp.openInvoice. Payments made inside the Mini App
|
||||
// are the ones Telegram's affiliate program counts.
|
||||
func (b *Bot) AppStarsPurchase(ctx context.Context, user *db.User, lang string, groupID int64, product string, amount int64) (*db.Purchase, string, error) {
|
||||
if !billing.ValidProduct(product) {
|
||||
return nil, "", billing.ErrBadProduct
|
||||
}
|
||||
if !b.rate.allow("ap:"+itoa(user.ID), 10, time.Minute) {
|
||||
return nil, "", ErrRateLimited
|
||||
}
|
||||
ownerType, ownerID := db.OwnerUser, user.ID
|
||||
if groupID != 0 {
|
||||
g, err := b.store.GetGroup(ctx, groupID)
|
||||
if err != nil || !g.Present() {
|
||||
return nil, "", db.ErrNotFound
|
||||
}
|
||||
ownerType, ownerID = db.OwnerGroup, groupID
|
||||
}
|
||||
w, err := b.bill.Wallet(ctx, ownerType, ownerID)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
p, err := b.bill.CreateStarsPurchase(ctx, billing.NewPurchase{
|
||||
UserID: user.ID, WalletID: w.ID, Product: product, Amount: amount, Lang: lang,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
link, err := b.StarsInvoiceLink(ctx, p)
|
||||
if err != nil {
|
||||
_, _ = b.bill.Cancel(context.WithoutCancel(ctx), p.ID, user.ID)
|
||||
return nil, "", err
|
||||
}
|
||||
return p, link, nil
|
||||
}
|
||||
|
||||
// Explain turns an error from the operations above into a stable code and
|
||||
// a message in the user's language.
|
||||
func (b *Bot) Explain(lang string, err error) (code, message string, upgradable bool) {
|
||||
@@ -248,7 +284,8 @@ func (b *Bot) Explain(lang string, err error) (code, message string, upgradable
|
||||
return "payments_off", b.tr.T(lang, "buy.unavailable", nil), false
|
||||
case errors.Is(err, billing.ErrLightningDown):
|
||||
return "payments_down", b.tr.T(lang, "buy.backend_down_short", nil), false
|
||||
case errors.Is(err, billing.ErrBadAmount), errors.Is(err, billing.ErrBadProduct):
|
||||
case errors.Is(err, billing.ErrBadAmount), errors.Is(err, billing.ErrBadProduct),
|
||||
errors.Is(err, billing.ErrNotForStars), errors.Is(err, billing.ErrStarsOff):
|
||||
return "bad_amount", b.tr.T(lang, "buy.bad_amount", nil), false
|
||||
}
|
||||
b.log.Error("mini app operation failed", "err", err)
|
||||
|
||||
Reference in New Issue
Block a user