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
+63
View File
@@ -113,6 +113,45 @@ type Config struct {
// day (UTC), in sats. Rewards above it wait and are paid on a later
// day. 0 means no cap.
ReferralDailyCap int64 `toml:"referral_daily_cap" env:"REFERRAL_DAILY_CAP"`
// StarsEnabled offers Telegram Stars as a second way to pay for passes
// and credits, next to Lightning. It needs no setup beyond the bot.
StarsEnabled bool `toml:"stars_enabled" env:"STARS_ENABLED"`
// StarsPrices is the price in Stars of each product sold for Stars:
// pass_day, pass_week, pass_month and credits_<sats> for a credit pack
// (credits_1000 = 70). These are set on their own, never converted from
// sats. A product without a price is not offered for Stars. In the
// environment: STARS_PRICES="pass_day=50,credits_1000=70".
StarsPrices map[string]int64 `toml:"stars_prices"`
}
// StarsPrice returns the price in Stars of a product, or false when it
// is not sold for Stars. amount names the credit pack.
func (c Config) StarsPrice(product string, amount int64) (int64, bool) {
key := product
if product == "credits" {
key = "credits_" + strconv.FormatInt(amount, 10)
}
n, ok := c.StarsPrices[key]
return n, ok && n > 0
}
// ParseStarsPrices reads STARS_PRICES: "pass_day=50,credits_1000=70".
func ParseStarsPrices(raw string) (map[string]int64, error) {
out := map[string]int64{}
for _, part := range strings.Split(raw, ",") {
part = strings.TrimSpace(part)
if part == "" {
continue
}
k, v, ok := strings.Cut(part, "=")
n, err := strconv.ParseInt(strings.TrimSpace(v), 10, 64)
if !ok || err != nil {
return nil, fmt.Errorf("STARS_PRICES: bad entry %q, want product=stars", part)
}
out[strings.TrimSpace(k)] = n
}
return out, nil
}
// Telegram holds the Bot API settings.
@@ -387,6 +426,10 @@ func Default() Config {
CaptionAttribution: true,
ReferralRewardSats: -1,
ReferralDailyCap: 2000,
StarsPrices: map[string]int64{
"pass_day": 50, "pass_week": 200, "pass_month": 600,
"credits_1000": 70, "credits_5000": 330, "credits_20000": 1300,
},
}
}
@@ -424,6 +467,13 @@ func Load(path string) (Config, error) {
if err := applyTierEnv("PAID", &cfg.Limits.Paid, os.LookupEnv); err != nil {
return cfg, err
}
if raw, ok := os.LookupEnv("STARS_PRICES"); ok && raw != "" {
m, err := ParseStarsPrices(raw)
if err != nil {
return cfg, err
}
cfg.StarsPrices = m
}
if raw, ok := os.LookupEnv("SITE_TAGS"); ok && raw != "" {
m, err := ParseSiteTags(raw)
if err != nil {
@@ -467,6 +517,19 @@ func (c Config) Validate() error {
if c.QueueNoticeThresholdSeconds < 0 {
errs = append(errs, "QUEUE_NOTICE_THRESHOLD_SECONDS must not be negative")
}
for k, n := range c.StarsPrices {
known := k == "pass_day" || k == "pass_week" || k == "pass_month"
if amount, ok := strings.CutPrefix(k, "credits_"); ok {
v, err := strconv.ParseInt(amount, 10, 64)
known = err == nil && v > 0
}
if !known {
errs = append(errs, fmt.Sprintf("stars_prices: unknown product %q (pass_day, pass_week, pass_month, credits_<sats>)", k))
}
if n < 1 || n > 100000 {
errs = append(errs, fmt.Sprintf("stars_prices: %s must be between 1 and 100000 Stars", k))
}
}
if c.ReferralDailyCap < 0 {
errs = append(errs, "REFERRAL_DAILY_CAP must not be negative")
}
+32
View File
@@ -393,3 +393,35 @@ func TestReferralRewardNames(t *testing.T) {
t.Fatal("a negative cap was accepted")
}
}
func TestStarsPrices(t *testing.T) {
cfg := Default()
if n, ok := cfg.StarsPrice("pass_week", 0); !ok || n != 200 {
t.Fatalf("default pass_week = %d %v", n, ok)
}
if n, ok := cfg.StarsPrice("credits", 5000); !ok || n != 330 {
t.Fatalf("default 5000 credits = %d %v", n, ok)
}
if _, ok := cfg.StarsPrice("credits", 1234); ok {
t.Fatal("a pack without a Stars price is sold for Stars")
}
if _, ok := cfg.StarsPrice("one_off", 0); ok {
t.Fatal("one-off payments are not sold for Stars")
}
m, err := ParseStarsPrices(" pass_day=25, credits_1000=60 ")
if err != nil || m["pass_day"] != 25 || m["credits_1000"] != 60 || len(m) != 2 {
t.Fatalf("ParseStarsPrices = %v, %v", m, err)
}
if _, err := ParseStarsPrices("pass_day=lots"); err == nil {
t.Fatal("a bad price was accepted")
}
cfg.Telegram.Token = "x"
cfg.StarsPrices = map[string]int64{"pass_year": 5}
if err := cfg.Validate(); err == nil || !strings.Contains(err.Error(), "pass_year") {
t.Fatalf("unknown product: %v", err)
}
cfg.StarsPrices = map[string]int64{"pass_day": 0}
if err := cfg.Validate(); err == nil {
t.Fatal("a price of 0 Stars was accepted")
}
}