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
@@ -0,0 +1,208 @@
|
||||
package billing_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/noderunners/dlbot/internal/billing"
|
||||
"github.com/noderunners/dlbot/internal/db"
|
||||
)
|
||||
|
||||
const bob = int64(43)
|
||||
|
||||
func starsRig(t *testing.T) *rig {
|
||||
t.Helper()
|
||||
r := newRig(t)
|
||||
r.cfg.StarsEnabled = true
|
||||
r.b = r.newBilling()
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *rig) buyStars(user int64, product string, amount int64) *db.Purchase {
|
||||
r.t.Helper()
|
||||
p, err := r.b.CreateStarsPurchase(context.Background(), billing.NewPurchase{
|
||||
UserID: user, WalletID: r.wallet(user).ID, Product: product, Amount: amount, Lang: "en",
|
||||
})
|
||||
if err != nil {
|
||||
r.t.Fatalf("CreateStarsPurchase: %v", err)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func TestStarsPricesAreTheirOwn(t *testing.T) {
|
||||
r := starsRig(t)
|
||||
ctx := context.Background()
|
||||
p := r.buyStars(alice, billing.ProductCredits, 5000)
|
||||
if p.PaymentMethod != db.PayStars || p.StarsAmount != 330 || p.AmountSats != 5000 || p.Bolt11 != "" {
|
||||
t.Fatalf("purchase = %+v", p)
|
||||
}
|
||||
// Only passes and credit packs with a configured price are sold.
|
||||
for _, c := range []struct {
|
||||
product string
|
||||
amount int64
|
||||
}{{billing.ProductOneOff, 1000}, {billing.ProductDonation, 500}, {billing.ProductCredits, 1234}, {billing.ProductSkipQueue, 0}} {
|
||||
_, err := r.b.CreateStarsPurchase(ctx, billing.NewPurchase{UserID: alice, WalletID: r.wallet(alice).ID, Product: c.product, Amount: c.amount})
|
||||
if !errors.Is(err, billing.ErrNotForStars) {
|
||||
t.Errorf("%s %d: %v, want ErrNotForStars", c.product, c.amount, err)
|
||||
}
|
||||
}
|
||||
// Off means off.
|
||||
r.cfg.StarsEnabled = false
|
||||
r.b = r.newBilling()
|
||||
if _, err := r.b.CreateStarsPurchase(ctx, billing.NewPurchase{UserID: alice, WalletID: r.wallet(alice).ID, Product: billing.ProductPassDay}); !errors.Is(err, billing.ErrStarsOff) {
|
||||
t.Fatalf("stars off: %v", err)
|
||||
}
|
||||
if creates, statuses := r.ln.Calls(); creates != 0 || statuses != 0 {
|
||||
t.Fatalf("a Stars purchase talked to LNbits: %d creates, %d statuses", creates, statuses)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStarsPreCheckout(t *testing.T) {
|
||||
r := starsRig(t)
|
||||
ctx := context.Background()
|
||||
p := r.buyStars(alice, billing.ProductPassDay, 0)
|
||||
payload := billing.StarsPayload(p)
|
||||
cases := []struct {
|
||||
name string
|
||||
payload string
|
||||
user int64
|
||||
currency string
|
||||
total int64
|
||||
want error
|
||||
}{
|
||||
{"matches", payload, alice, "XTR", 50, nil},
|
||||
{"other amount", payload, alice, "XTR", 49, billing.ErrStarsMismatch},
|
||||
{"other currency", payload, alice, "USD", 50, billing.ErrStarsMismatch},
|
||||
{"other payer", payload, bob, "XTR", 50, billing.ErrUnknownCharge},
|
||||
{"unknown payload", "dlp:nope", alice, "XTR", 50, billing.ErrUnknownCharge},
|
||||
{"foreign payload", "something else", alice, "XTR", 50, billing.ErrUnknownCharge},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if err := r.b.PreCheckStars(ctx, c.payload, c.user, c.currency, c.total); !errors.Is(err, c.want) {
|
||||
t.Errorf("%s: %v, want %v", c.name, err, c.want)
|
||||
}
|
||||
}
|
||||
// A cancelled invoice is not paid any more.
|
||||
if _, err := r.b.Cancel(ctx, p.ID, alice); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := r.b.PreCheckStars(ctx, payload, alice, "XTR", 50); !errors.Is(err, billing.ErrInvoiceClosed) {
|
||||
t.Fatalf("cancelled: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStarsPaymentGrantsTheSameAsLightningOnce(t *testing.T) {
|
||||
r := starsRig(t)
|
||||
ctx := context.Background()
|
||||
// The same pass, once over Lightning (bob) and once in Stars (alice).
|
||||
_ = r.ln.Pay(r.buy(bob, billing.ProductPassWeek, 0).PaymentHash)
|
||||
p := r.buyStars(alice, billing.ProductPassWeek, 0)
|
||||
settled, paid, err := r.b.SettleStars(ctx, billing.StarsPayload(p), "charge-1", alice, "XTR", 200)
|
||||
if err != nil || !settled || paid.Status != db.PurchasePaid || paid.ChargeID != "charge-1" {
|
||||
t.Fatalf("SettleStars = %v, %+v, %v", settled, paid, err)
|
||||
}
|
||||
stA, _ := r.b.State(ctx, db.OwnerUser, alice)
|
||||
stB, _ := r.b.State(ctx, db.OwnerUser, bob)
|
||||
if stA.PassUntil == 0 || stA.PassUntil != stB.PassUntil {
|
||||
t.Fatalf("pass until: stars %d, lightning %d", stA.PassUntil, stB.PassUntil)
|
||||
}
|
||||
// The same payment event again, and again with the poll: nothing new.
|
||||
for i := 0; i < 3; i++ {
|
||||
again, cur, err := r.b.SettleStars(ctx, billing.StarsPayload(p), "charge-1", alice, "XTR", 200)
|
||||
if err != nil || again || cur.ID != p.ID {
|
||||
t.Fatalf("repeat %d: %v %+v %v", i, again, cur, err)
|
||||
}
|
||||
}
|
||||
if n := r.count(billing.EventPaid); n != 2 {
|
||||
t.Fatalf("%d paid events, want 2 (one per purchase)", n)
|
||||
}
|
||||
if _, err := r.b.Reconcile(ctx, time.Hour); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if st, _ := r.b.State(ctx, db.OwnerUser, alice); st.PassUntil != stA.PassUntil {
|
||||
t.Fatal("the pass was extended twice")
|
||||
}
|
||||
|
||||
// Credits: the pack's sats arrive in the wallet, marked as paid in Stars.
|
||||
c := r.buyStars(alice, billing.ProductCredits, 1000)
|
||||
if _, _, err := r.b.SettleStars(ctx, billing.StarsPayload(c), "charge-2", alice, "XTR", 70); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := r.balance(alice); got != 1000 {
|
||||
t.Fatalf("balance = %d", got)
|
||||
}
|
||||
if l := r.ledger(alice); len(l) == 0 || l[0].PaymentMethod != db.PayStars {
|
||||
t.Fatalf("ledger = %+v", l)
|
||||
}
|
||||
// A payment that does not match its invoice is not settled.
|
||||
m := r.buyStars(alice, billing.ProductCredits, 5000)
|
||||
if _, _, err := r.b.SettleStars(ctx, billing.StarsPayload(m), "charge-3", alice, "XTR", 1); !errors.Is(err, billing.ErrStarsMismatch) {
|
||||
t.Fatalf("mismatch: %v", err)
|
||||
}
|
||||
r.booksBalance()
|
||||
}
|
||||
|
||||
func TestStarsRefundUndoesThePurchase(t *testing.T) {
|
||||
r := starsRig(t)
|
||||
ctx := context.Background()
|
||||
c := r.buyStars(alice, billing.ProductCredits, 1000)
|
||||
_, _, _ = r.b.SettleStars(ctx, billing.StarsPayload(c), "c1", alice, "XTR", 70)
|
||||
pass := r.buyStars(alice, billing.ProductPassDay, 0)
|
||||
_, _, _ = r.b.SettleStars(ctx, billing.StarsPayload(pass), "c2", alice, "XTR", 50)
|
||||
week := r.buyStars(alice, billing.ProductPassWeek, 0)
|
||||
_, _, _ = r.b.SettleStars(ctx, billing.StarsPayload(week), "c3", alice, "XTR", 200)
|
||||
before, _ := r.b.State(ctx, db.OwnerUser, alice)
|
||||
|
||||
// The day pass goes; the week pass stacked after it moves up a day.
|
||||
if ok, err := r.b.UndoStarsPurchase(ctx, pass.ID); err != nil || !ok {
|
||||
t.Fatalf("undo pass: %v %v", ok, err)
|
||||
}
|
||||
after, _ := r.b.State(ctx, db.OwnerUser, alice)
|
||||
if got := before.PassUntil - after.PassUntil; got != (24 * time.Hour).Milliseconds() {
|
||||
t.Fatalf("pass shortened by %d ms, want one day", got)
|
||||
}
|
||||
// Credits are taken back as far as they are left.
|
||||
if ok, _ := r.b.UndoStarsPurchase(ctx, c.ID); !ok || r.balance(alice) != 0 {
|
||||
t.Fatalf("balance after refund = %d", r.balance(alice))
|
||||
}
|
||||
// Once only, and never for a Lightning purchase.
|
||||
if ok, _ := r.b.UndoStarsPurchase(ctx, c.ID); ok {
|
||||
t.Fatal("refunded twice")
|
||||
}
|
||||
ln := r.buy(bob, billing.ProductCredits, 1000)
|
||||
_ = r.ln.Pay(ln.PaymentHash)
|
||||
if ok, _ := r.b.UndoStarsPurchase(ctx, ln.ID); ok || r.balance(bob) != 1000 {
|
||||
t.Fatal("a Lightning purchase was undone as a Stars refund")
|
||||
}
|
||||
if p := r.purchase(pass.ID); p.Status != db.PurchaseRefunded || p.RefundedAt == 0 {
|
||||
t.Fatalf("purchase = %+v", p)
|
||||
}
|
||||
r.booksBalance()
|
||||
}
|
||||
|
||||
func TestStarsPurchasesEarnNoSatsReferral(t *testing.T) {
|
||||
r := starsRig(t)
|
||||
ctx := context.Background()
|
||||
const inviter, invited = int64(700), int64(701)
|
||||
for _, id := range []int64{inviter, invited} {
|
||||
if _, err := r.store.UpsertUser(ctx, id, "", "U", "en"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := r.store.SetReferrer(ctx, invited, inviter); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Telegram's affiliate program covers Stars; our sats reward does not.
|
||||
p := r.buyStars(invited, billing.ProductPassDay, 0)
|
||||
_, _, _ = r.b.SettleStars(ctx, billing.StarsPayload(p), "c1", invited, "XTR", 50)
|
||||
if r.balance(inviter) != 0 {
|
||||
t.Fatal("a Stars purchase paid a sats referral reward")
|
||||
}
|
||||
// The first Lightning purchase after it does.
|
||||
_ = r.ln.Pay(r.buy(invited, billing.ProductCredits, 1000).PaymentHash)
|
||||
if r.balance(inviter) != 200 {
|
||||
t.Fatalf("inviter balance = %d, want 200", r.balance(inviter))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user