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
+24
View File
@@ -0,0 +1,24 @@
-- +goose Up
-- Telegram Stars as a second payment method. Every purchase and every
-- ledger entry says how it was paid; everything before this was Lightning.
ALTER TABLE purchases ADD COLUMN payment_method TEXT NOT NULL DEFAULT 'lightning';
-- What a Stars purchase cost in Stars. amount_sats keeps the sats value of
-- what it bought (the credit pack, or the pass price) for the wallet.
ALTER TABLE purchases ADD COLUMN stars_amount BIGINT NOT NULL DEFAULT 0;
-- Telegram's identifier of a Stars payment. Unique, so the same payment
-- event can never settle twice.
ALTER TABLE purchases ADD COLUMN tg_charge_id TEXT NOT NULL DEFAULT '';
ALTER TABLE purchases ADD COLUMN refunded_at BIGINT NOT NULL DEFAULT 0;
CREATE UNIQUE INDEX purchases_charge_idx ON purchases (tg_charge_id) WHERE tg_charge_id <> '';
CREATE INDEX purchases_method_idx ON purchases (payment_method, status);
ALTER TABLE ledger ADD COLUMN payment_method TEXT NOT NULL DEFAULT 'lightning';
-- +goose Down
ALTER TABLE ledger DROP COLUMN payment_method;
DROP INDEX purchases_method_idx;
DROP INDEX purchases_charge_idx;
ALTER TABLE purchases DROP COLUMN refunded_at;
ALTER TABLE purchases DROP COLUMN tg_charge_id;
ALTER TABLE purchases DROP COLUMN stars_amount;
ALTER TABLE purchases DROP COLUMN payment_method;