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
+65
View File
@@ -1070,3 +1070,68 @@ the affiliate program. What was found and planned is in
link (a `copy_text` button) and QR code (a PNG sent in the chat). The
Mini App Me page shows the same numbers, copy and share, and the QR
code as an image in the profile response.
### Phase 4: Telegram Stars payments
- **Where the prices live.** "A separate config table (stars_prices)" is
read as a configuration table: `[stars_prices]` in the TOML file, or
`STARS_PRICES="pass_day=50,credits_1000=70"`. It is not a database
table. Keys are `pass_day`, `pass_week`, `pass_month` and
`credits_<sats>`, one per credit pack. A product without a price is
not sold for Stars; nothing is converted from sats. The defaults
(50, 200, 600; 70, 330, 1300) are placeholders for the owner to set.
`STARS_ENABLED` is off by default, so existing setups keep working
unchanged.
- **What Stars buy.** Stars buy passes and credit packs, for the user's
own wallet. Stars purchases from the Mini App can also be for a group.
Skipping the queue, one-off payments for a single file, donations and
group top-ups from the chat stay Lightning only. One-off prices depend
on the file size, and a fixed Stars price per size step was not asked
for.
- **How it is paid.**
- The invoice uses currency XTR, an empty provider token and exactly
one price, with payload `dlp:<purchase id>`.
- The pre-checkout answer checks that the payload belongs to this payer
and is still pending, and that currency and amount match.
- `successful_payment` settles through the same transaction as
Lightning, so a pass or credits are granted exactly the same way and
the follow-up (the download behind an over-limit offer) runs the
same way.
- The `telegram_payment_charge_id` is stored under a unique index, so
a repeated event settles nothing twice.
- A Stars purchase gets a placeholder payment hash (`stars:<id>`) and
is never sent to LNbits, by the poll or by "Check payment".
- **The books.** `purchases.payment_method` and `ledger.payment_method`
(migration 00009) say lightning or stars; older rows are lightning.
`purchases.amount_sats` of a Stars purchase is the sats value of what
it bought: the credit pack, or the pass's Lightning price.
`stars_amount` is what was paid in Stars. /stats shows revenue per
method: sats for Lightning, Stars for Stars, refunds counted apart.
- **Refunds.**
- `/refund <purchase id or charge id>` calls `refundStarPayment` and
then takes back what the purchase granted: credits as far as they
are left, or the pass. Passes stacked after a refunded pass move up
by its length.
- The old `/refund <user> <sats>` form still gives sats back.
- A `refunded_payment` message from Telegram (for example after the
owner's refund) undoes the purchase too. That only happens once.
- A payment that cannot be matched to an order is logged with its
charge ID, so the owner can refund it.
- **A failed download after a Stars payment** follows the Lightning
policy unchanged. The scheduler retries a job once. What a download
charged is given back when it finally fails. A pass that was bought
stays, and a pass bought for a download that can no longer start stays
too. No extension of the pass was added, since Lightning has none.
- **In the chat.** The buy screen, /credits and /account show the
Lightning buttons first and a "Pay with Stars" button that switches the
message to Stars prices and back. The over-limit offer adds a Stars
button that shows the Stars passes in a message of their own. Once
paid, a new message confirms the payment, because Telegram's invoice
message cannot be edited into "paid".
- **In the Mini App**, Buy has a Lightning and Stars switch. A Stars
purchase returns an invoice link (`createInvoiceLink`), which the app
opens with `WebApp.openInvoice`. The status the app gets back only
closes or keeps the view. The app learns about the payment by asking
the core, which settles it from Telegram's `successful_payment` update.
- `pre_checkout_query` was added to the update types the bot asks
Telegram for.
+104 -19
View File
@@ -145,14 +145,6 @@ Downloading…
editMessageText
```
<b>Test Video</b>
Sending it to Telegram…
[Cancel]
```
editMessageText
```
<b>Test Video</b>
Sending it to Telegram…
@@ -362,7 +354,7 @@ editMessageText
Share your link. When somebody you invited pays for a first purchase in sats (Lightning), you get 200 sats in credits.
https://t.me/nr_dl_test_bot?start=rf_CW5AYJM3
https://t.me/nr_dl_test_bot?start=rf_YY6OJU6T
Invited: 0 · Bought something: 0
Earned: 0 sats · Waiting: 0 sats
@@ -553,6 +545,7 @@ Downloads: 4 of 5
Volume: 4 KB of 250 MB
[Day pass · 700 sats] [Week pass · 3,000 sats]
[Month pass · 9,000 sats]
[⭐ Pay with Stars]
```
sendMessage
@@ -574,7 +567,7 @@ sendMessage
Share your link. When somebody you invited pays for a first purchase in sats (Lightning), you get 200 sats in credits.
https://t.me/nr_dl_test_bot?start=rf_CW5AYJM3
https://t.me/nr_dl_test_bot?start=rf_YY6OJU6T
Invited: 0 · Bought something: 0
Earned: 0 sats · Waiting: 0 sats
@@ -597,6 +590,7 @@ You pay in sats over Lightning.
[Day pass · 700 sats] [Week pass · 3,000 sats]
[Month pass · 9,000 sats]
[1,000 sats] [5,000 sats] [20,000 sats]
[⭐ Pay with Stars]
```
sendMessage
@@ -606,6 +600,7 @@ sendMessage
One download costs 50 sats. Top up:
[1,000 sats] [5,000 sats] [20,000 sats]
[⭐ Pay with Stars]
```
sendMessage
@@ -862,6 +857,7 @@ You pay in sats over Lightning.
[Day pass · 700 sats] [Week pass · 3,000 sats]
[Month pass · 9,000 sats]
[1,000 sats] [5,000 sats] [20,000 sats]
[⭐ Pay with Stars]
```
sendPhoto
@@ -869,13 +865,40 @@ sendPhoto
```
<b>Day pass</b> · 700 sats
<code>lnbc7000n1fake932bb5fc424d32f8967f5a103ec1aa9b323d60cf969ac292033fe465b6058e60</code>
<code>lnbc7000n1fakee38a3d34236055a47c726ecb0a1c30d15a717a59fccef6acb0cff8dcb7dc200f</code>
Scan the code or tap the invoice to copy it. Valid for 10 minutes.
[Open in wallet]
[Check payment] [Cancel]
```
editMessageText
```
<b>Pay with Telegram Stars</b>
The same passes and credits, paid from your Stars balance in Telegram.
[Day pass · ⭐ 50] [Week pass · ⭐ 200]
[Month pass · ⭐ 600]
[1,000 sats · ⭐ 70] [5,000 sats · ⭐ 330] [20,000 sats · ⭐ 1,300]
[⚡ Pay with Lightning]
```
editMessageText
```
<b>Passes and credits</b>
A <b>pass</b> lifts your limits to 1.95 GB and 3 hours per download and puts you in the priority queue for as long as it runs.
<b>Credits</b> pay per download, 50 sats each, with the same limits and priority. They never expire.
You pay in sats over Lightning.
[Day pass · 700 sats] [Week pass · 3,000 sats]
[Month pass · 9,000 sats]
[1,000 sats] [5,000 sats] [20,000 sats]
[⭐ Pay with Stars]
```
## es
sendMessage
@@ -1234,7 +1257,7 @@ editMessageText
Comparte tu enlace. Cuando alguien que invitaste pague una primera compra en sats (Lightning), recibes 200 sats en créditos.
https://t.me/nr_dl_test_bot?start=rf_MXZGYTEK
https://t.me/nr_dl_test_bot?start=rf_Q3U2NIQH
Invitados: 0 · Compraron algo: 0
Ganado: 0 sats · En espera: 0 sats
@@ -1425,6 +1448,7 @@ Descargas: 4 de 5
Volumen: 4 KB de 250 MB
[Pase de un día · 700 sats] [Pase semanal · 3.000 sats]
[Pase mensual · 9.000 sats]
[⭐ Pagar con Estrellas]
```
sendMessage
@@ -1446,7 +1470,7 @@ sendMessage
Comparte tu enlace. Cuando alguien que invitaste pague una primera compra en sats (Lightning), recibes 200 sats en créditos.
https://t.me/nr_dl_test_bot?start=rf_MXZGYTEK
https://t.me/nr_dl_test_bot?start=rf_Q3U2NIQH
Invitados: 0 · Compraron algo: 0
Ganado: 0 sats · En espera: 0 sats
@@ -1469,6 +1493,7 @@ Pagas en sats por Lightning.
[Pase de un día · 700 sats] [Pase semanal · 3.000 sats]
[Pase mensual · 9.000 sats]
[1.000 sats] [5.000 sats] [20.000 sats]
[⭐ Pagar con Estrellas]
```
sendMessage
@@ -1478,6 +1503,7 @@ sendMessage
Una descarga cuesta 50 sats. Carga saldo:
[1.000 sats] [5.000 sats] [20.000 sats]
[⭐ Pagar con Estrellas]
```
sendMessage
@@ -1734,6 +1760,7 @@ Pagas en sats por Lightning.
[Pase de un día · 700 sats] [Pase semanal · 3.000 sats]
[Pase mensual · 9.000 sats]
[1.000 sats] [5.000 sats] [20.000 sats]
[⭐ Pagar con Estrellas]
```
sendPhoto
@@ -1741,13 +1768,40 @@ sendPhoto
```
<b>Pase de un día</b> · 700 sats
<code>lnbc7000n1fake5bb91054d0d2b0cbcc9f5394928a595cf2ccba08d4a09432f7db5063c88ffd85</code>
<code>lnbc7000n1fake7bf3f553278730f9f02e2e8e66bb1524a1efc083ab3bfe31c83c00e15d64e033</code>
Escanea el código o toca la factura para copiarla. Vale por 10 minutos.
[Abrir en la billetera]
[Verificar el pago] [Cancelar]
```
editMessageText
```
<b>Pagar con Estrellas de Telegram</b>
Los mismos pases y créditos, pagados con tu saldo de Estrellas en Telegram.
[Pase de un día · ⭐ 50] [Pase semanal · ⭐ 200]
[Pase mensual · ⭐ 600]
[1.000 sats · ⭐ 70] [5.000 sats · ⭐ 330] [20.000 sats · ⭐ 1.300]
[⚡ Pagar con Lightning]
```
editMessageText
```
<b>Pases y créditos</b>
Un <b>pase</b> sube tus límites a 1,95 GB y 3 horas por descarga y te pone en la fila prioritaria mientras dure.
Los <b>créditos</b> pagan por descarga, 50 sats cada una, con los mismos límites y la misma prioridad. No vencen nunca.
Pagas en sats por Lightning.
[Pase de un día · 700 sats] [Pase semanal · 3.000 sats]
[Pase mensual · 9.000 sats]
[1.000 sats] [5.000 sats] [20.000 sats]
[⭐ Pagar con Estrellas]
```
## nl
sendMessage
@@ -1827,7 +1881,7 @@ editMessageText
```
<b>Test Video</b>
Downloaden…
Naar Telegram sturen…
[Annuleren]
```
@@ -1835,7 +1889,7 @@ editMessageText
```
<b>Test Video</b>
Naar Telegram sturen…
Downloaden…
[Annuleren]
```
@@ -2106,7 +2160,7 @@ editMessageText
Deel je link. Als iemand die je uitnodigde een eerste aankoop in sats (Lightning) betaalt, krijg jij 200 sats aan tegoed.
https://t.me/nr_dl_test_bot?start=rf_4VIFLSDC
https://t.me/nr_dl_test_bot?start=rf_TXUCNI2E
Uitgenodigd: 0 · Iets gekocht: 0
Verdiend: 0 sats · Wacht nog: 0 sats
@@ -2297,6 +2351,7 @@ Downloads: 4 van 5
Volume: 4 KB van 250 MB
[Dagpas · 700 sats] [Weekpas · 3.000 sats]
[Maandpas · 9.000 sats]
[⭐ Betalen met Stars]
```
sendMessage
@@ -2318,7 +2373,7 @@ sendMessage
Deel je link. Als iemand die je uitnodigde een eerste aankoop in sats (Lightning) betaalt, krijg jij 200 sats aan tegoed.
https://t.me/nr_dl_test_bot?start=rf_4VIFLSDC
https://t.me/nr_dl_test_bot?start=rf_TXUCNI2E
Uitgenodigd: 0 · Iets gekocht: 0
Verdiend: 0 sats · Wacht nog: 0 sats
@@ -2341,6 +2396,7 @@ Je betaalt in sats via Lightning.
[Dagpas · 700 sats] [Weekpas · 3.000 sats]
[Maandpas · 9.000 sats]
[1.000 sats] [5.000 sats] [20.000 sats]
[⭐ Betalen met Stars]
```
sendMessage
@@ -2350,6 +2406,7 @@ sendMessage
Eén download kost 50 sats. Opwaarderen:
[1.000 sats] [5.000 sats] [20.000 sats]
[⭐ Betalen met Stars]
```
sendMessage
@@ -2606,6 +2663,7 @@ Je betaalt in sats via Lightning.
[Dagpas · 700 sats] [Weekpas · 3.000 sats]
[Maandpas · 9.000 sats]
[1.000 sats] [5.000 sats] [20.000 sats]
[⭐ Betalen met Stars]
```
sendPhoto
@@ -2613,9 +2671,36 @@ sendPhoto
```
<b>Dagpas</b> · 700 sats
<code>lnbc7000n1fake2279497394e085369465914011a009430187243aabd22a7b3642440c8ec67831</code>
<code>lnbc7000n1fake5443af8ff471a30e71eaa1ee0db07d5d314f447f88f895369ea8892d717b5879</code>
Scan de code of tik op de factuur om hem te kopiëren. Geldig voor 10 minuten.
[Openen in wallet]
[Betaling controleren] [Annuleren]
```
editMessageText
```
<b>Betalen met Telegram Stars</b>
Dezelfde passen en hetzelfde tegoed, betaald met je Stars-saldo in Telegram.
[Dagpas · ⭐ 50] [Weekpas · ⭐ 200]
[Maandpas · ⭐ 600]
[1.000 sats · ⭐ 70] [5.000 sats · ⭐ 330] [20.000 sats · ⭐ 1.300]
[⚡ Betalen met Lightning]
```
editMessageText
```
<b>Passen en tegoed</b>
Een <b>pas</b> verhoogt je limieten naar 1,95 GB en 3 uur per download en zet je in de voorrangswachtrij zolang hij loopt.
<b>Tegoed</b> betaalt per download, 50 sats per stuk, met dezelfde limieten en voorrang. Het verloopt nooit.
Je betaalt in sats via Lightning.
[Dagpas · 700 sats] [Weekpas · 3.000 sats]
[Maandpas · 9.000 sats]
[1.000 sats] [5.000 sats] [20.000 sats]
[⭐ Betalen met Stars]
```