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
@@ -353,6 +353,52 @@ func (s *Server) PushWidgetQuery(chatID, userID int64, messageID int, data strin
|
||||
return id
|
||||
}
|
||||
|
||||
// PushPreCheckout queues the query Telegram sends before it takes a
|
||||
// payment, and returns its ID.
|
||||
func (s *Server) PushPreCheckout(userID int64, payload, currency string, total int) string {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
id := fmt.Sprintf("pcq%d", s.updateID+1)
|
||||
s.pushLocked(map[string]any{"pre_checkout_query": map[string]any{
|
||||
"id": id,
|
||||
"from": map[string]any{"id": userID, "is_bot": false, "first_name": "User", "language_code": s.userLang(userID)},
|
||||
"currency": currency,
|
||||
"total_amount": total,
|
||||
"invoice_payload": payload,
|
||||
}})
|
||||
return id
|
||||
}
|
||||
|
||||
// PushSuccessfulPayment queues the service message of a completed payment
|
||||
// in the payer's private chat.
|
||||
func (s *Server) PushSuccessfulPayment(userID int64, payload, currency string, total int, chargeID string) {
|
||||
s.pushPaymentLocked(userID, "successful_payment", map[string]any{
|
||||
"currency": currency, "total_amount": total, "invoice_payload": payload,
|
||||
"telegram_payment_charge_id": chargeID, "provider_payment_charge_id": "",
|
||||
})
|
||||
}
|
||||
|
||||
// PushRefundedPayment queues the service message of a refunded payment.
|
||||
func (s *Server) PushRefundedPayment(userID int64, payload string, total int, chargeID string) {
|
||||
s.pushPaymentLocked(userID, "refunded_payment", map[string]any{
|
||||
"currency": "XTR", "total_amount": total, "invoice_payload": payload,
|
||||
"telegram_payment_charge_id": chargeID,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) pushPaymentLocked(userID int64, field string, body map[string]any) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.msgID++
|
||||
s.pushLocked(map[string]any{"message": map[string]any{
|
||||
"message_id": s.msgID,
|
||||
"date": time.Now().Unix(),
|
||||
"chat": chatObject(userID),
|
||||
"from": map[string]any{"id": userID, "is_bot": false, "first_name": "User", "language_code": s.userLang(userID)},
|
||||
field: body,
|
||||
}})
|
||||
}
|
||||
|
||||
// PushRaw queues an arbitrary update body, for example my_chat_member.
|
||||
func (s *Server) PushRaw(update map[string]any) {
|
||||
s.mu.Lock()
|
||||
@@ -479,7 +525,10 @@ func (s *Server) resultLocked(c *Call) any {
|
||||
}
|
||||
uid, _ := strconv.ParseInt(c.Params["user_id"], 10, 64)
|
||||
return map[string]any{"status": status, "user": map[string]any{"id": uid, "is_bot": false, "first_name": "User"}}
|
||||
case "sendMessage", "sendPhoto", "sendVideo", "sendAudio", "sendDocument", "sendAnimation", "sendWidget":
|
||||
case "createInvoiceLink":
|
||||
s.fileSeq++
|
||||
return fmt.Sprintf("https://t.me/$fake_invoice_%d", s.fileSeq)
|
||||
case "sendMessage", "sendPhoto", "sendVideo", "sendAudio", "sendDocument", "sendAnimation", "sendWidget", "sendInvoice":
|
||||
s.msgID++
|
||||
c.MessageID = s.msgID
|
||||
msg := s.messageLocked(c)
|
||||
@@ -540,6 +589,17 @@ func (s *Server) messageLocked(c *Call) map[string]any {
|
||||
msg["animation"] = a
|
||||
case "sendWidget":
|
||||
msg["widget"] = map[string]any{"html": c.Params["html"], "chat_data": c.Params["chat_data"], "public_data": c.Params["public_data"]}
|
||||
case "sendInvoice":
|
||||
var prices []struct {
|
||||
Amount int `json:"amount"`
|
||||
}
|
||||
_ = json.Unmarshal([]byte(c.Params["prices"]), &prices)
|
||||
total := 0
|
||||
for _, p := range prices {
|
||||
total += p.Amount
|
||||
}
|
||||
msg["invoice"] = map[string]any{"title": c.Params["title"], "description": c.Params["description"],
|
||||
"start_parameter": "", "currency": c.Params["currency"], "total_amount": total}
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user