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
+30 -13
View File
@@ -196,26 +196,26 @@ func (b *Billing) State(ctx context.Context, ownerType string, ownerID int64) (e
// Credit adds sats to a wallet. The dedupe key makes it idempotent.
func (b *Billing) Credit(ctx context.Context, walletID string, sats int64, reason, ref, dedupe string, actor int64) (bool, error) {
return b.move(ctx, db.LedgerCredit, walletID, sats, reason, ref, dedupe, actor)
return b.move(ctx, db.LedgerCredit, walletID, sats, reason, ref, dedupe, actor, "")
}
// Debit takes sats from a wallet, or fails with db.ErrInsufficient.
func (b *Billing) Debit(ctx context.Context, walletID string, sats int64, reason, ref, dedupe string, actor int64) (bool, error) {
return b.move(ctx, db.LedgerDebit, walletID, sats, reason, ref, dedupe, actor)
return b.move(ctx, db.LedgerDebit, walletID, sats, reason, ref, dedupe, actor, "")
}
// Refund gives sats back to a wallet.
func (b *Billing) Refund(ctx context.Context, walletID string, sats int64, reason, ref, dedupe string, actor int64) (bool, error) {
return b.move(ctx, db.LedgerRefund, walletID, sats, reason, ref, dedupe, actor)
return b.move(ctx, db.LedgerRefund, walletID, sats, reason, ref, dedupe, actor, "")
}
func (b *Billing) move(ctx context.Context, kind, walletID string, sats int64, reason, ref, dedupe string, actor int64) (bool, error) {
func (b *Billing) move(ctx context.Context, kind, walletID string, sats int64, reason, ref, dedupe string, actor int64, method string) (bool, error) {
var applied bool
err := b.store.InTx(ctx, func(tx *db.Store) error {
var err error
applied, err = tx.AddLedger(ctx, &db.LedgerEntry{
WalletID: walletID, Kind: kind, Amount: sats, Reason: reason, Ref: ref,
DedupeKey: dedupe, ActorID: actor, CreatedAt: b.nowMilli(),
DedupeKey: dedupe, ActorID: actor, CreatedAt: b.nowMilli(), PaymentMethod: method,
})
return err
})
@@ -390,7 +390,9 @@ func (b *Billing) Check(ctx context.Context, purchaseID string, userID int64) (*
if p.UserID != userID {
return nil, ErrNotOwner
}
if p.Status != db.PurchasePaid {
if p.Status != db.PurchasePaid && p.PaymentMethod != db.PayStars {
// A Stars payment arrives as a Telegram update; there is nothing
// to ask LNbits about.
if _, err := b.checkOne(ctx, p); err != nil {
return nil, err
}
@@ -400,7 +402,7 @@ func (b *Billing) Check(ctx context.Context, purchaseID string, userID int64) (*
// checkOne confirms a purchase with LNbits and settles or expires it.
func (b *Billing) checkOne(ctx context.Context, p *db.Purchase) (settled bool, err error) {
if p.Status == db.PurchasePaid {
if p.Status == db.PurchasePaid || p.PaymentMethod == db.PayStars {
return false, nil
}
if b.ln == nil {
@@ -442,6 +444,13 @@ func (b *Billing) checkOne(ctx context.Context, p *db.Purchase) (settled bool, e
// transaction. It is idempotent: the second and later calls return false
// and change nothing. Callers must have confirmed the payment.
func (b *Billing) Settle(ctx context.Context, purchaseID string) (bool, error) {
settled, _, err := b.settle(ctx, purchaseID, nil)
return settled, err
}
// settle is Settle with a step of its own inside the transaction, run once
// the purchase is marked paid: a Stars payment records its charge ID there.
func (b *Billing) settle(ctx context.Context, purchaseID string, inTx func(tx *db.Store) error) (bool, *db.Purchase, error) {
var (
settled bool
p *db.Purchase
@@ -452,6 +461,11 @@ func (b *Billing) Settle(ctx context.Context, purchaseID string) (bool, error) {
if err != nil || !won {
return err
}
if inTx != nil {
if err := inTx(tx); err != nil {
return err
}
}
if p, err = tx.GetPurchase(ctx, purchaseID); err != nil {
return err
}
@@ -460,6 +474,7 @@ func (b *Billing) Settle(ctx context.Context, purchaseID string) (bool, error) {
if _, err := tx.AddLedger(ctx, &db.LedgerEntry{
WalletID: p.WalletID, Kind: db.LedgerCredit, Amount: p.AmountSats,
Reason: "purchase", Ref: p.ID, DedupeKey: "purchase:" + p.ID, ActorID: p.UserID, CreatedAt: now,
PaymentMethod: p.PaymentMethod,
}); err != nil {
return err
}
@@ -483,18 +498,18 @@ func (b *Billing) Settle(ctx context.Context, purchaseID string) (bool, error) {
return nil
})
if errors.Is(err, db.ErrDuplicate) {
return false, nil // another connection settled it first
return false, nil, nil // another connection settled it first
}
if err != nil || !settled {
return false, err
return false, nil, err
}
b.mu.Lock()
b.stats.InvoicesSettled++
b.mu.Unlock()
b.log.Info("invoice settled", "purchase_id", p.ID, "product", p.Product, "sats", p.AmountSats,
"user_id", p.UserID, "wallet_id", p.WalletID)
"stars", p.StarsAmount, "method", p.PaymentMethod, "user_id", p.UserID, "wallet_id", p.WalletID)
b.emit(EventPaid, p)
return true, nil
return true, p, nil
}
// rewardReferrer pays the inviter of a user who settled their first
@@ -544,7 +559,9 @@ func (b *Billing) rewardReferrer(ctx context.Context, tx *db.Store, p *db.Purcha
// countsForReferral reports whether a purchase was paid in a way our
// referral program rewards: Lightning.
func countsForReferral(p *db.Purchase) bool { return true }
func countsForReferral(p *db.Purchase) bool {
return p.PaymentMethod == db.PayLightning || p.PaymentMethod == ""
}
// underCap reports whether an inviter may be paid amount more today.
func (b *Billing) underCap(ctx context.Context, tx *db.Store, referrer int64, day int, amount int64) (bool, error) {
@@ -642,7 +659,7 @@ func (b *Billing) ConvertToCredits(ctx context.Context, p *db.Purchase) (bool, e
if err != nil {
return false, err
}
return b.Credit(ctx, w.ID, p.AmountSats, "purchase_fallback", p.ID, "purchase-fallback:"+p.ID, p.UserID)
return b.move(ctx, db.LedgerCredit, w.ID, p.AmountSats, "purchase_fallback", p.ID, "purchase-fallback:"+p.ID, p.UserID, p.PaymentMethod)
}
// --- reconciliation --------------------------------------------------------