LIVE PORTAL Telegram v11.8 API Synchronized Free Animated Stickers
translate Translated
admin_panel_settings ADMIN CONTROLS Guide #18467 • Beginner Basics

Payments API: Credit Cards & Telegram Stars in Bot Workflows

[Telegram 94] Payments API: Credit Cards & Telegram Stars in Bot Workflows
school TELEGRAM MASTERCLASS • STEP 094 / 100

Step 094: Payments API: Credit Cards & Telegram Stars in Bot Workflows

Step 094 of 100 (94%)

Executive Overview: Monetizing Telegram automated ecosystems requires navigating two distinct transaction paradigms: native Telegram Stars (XTR) for Apple & Google IAP-compliant digital goods, and direct credit card gateways (Stripe, Smart Glocal) with 0% platform commission for physical e-commerce. In Telegram 194, we construct an end-to-end production payment pipeline: executing sendInvoice, enforcing the critical 10-second pre_checkout_query verification timeout, validating cryptographically sealed receipts via successful_payment, handling automated refunds, and routing digital revenue to TON via Fragment.

< 10s
Pre-Checkout SLA
0% Fee
Physical Goods Fiat
Telegram Stars
Apple & Google Compliant
Fragment TON
21-Day Holding & Cashout

1. The 3-Step Payment Authorization Handshake

The Telegram Payments API enforces a strict three-stage cryptographic protocol to guarantee transactional integrity and prevent inventory race conditions:

Stage 1: sendInvoice

Invoice Dispatch

Your bot calls sendInvoice with a title, description, price breakdown array (LabeledPrice), currency, and a custom payload string (e.g. your database internal order ID).

Stage 2: pre_checkout_query

10-Second Authorization Window

When the user taps "Pay", Telegram sends a pre_checkout_query update. Your bot has exactly 10 seconds to verify inventory and respond with answerPreCheckoutQuery(ok=True).

Stage 3: successful_payment

Receipt & Fulfillment

Once the user's payment clears, Telegram delivers a message containing the successful_payment object with a unique charge ID. Your backend unlocks access and fulfills the order.

2. Dual Monetization: Telegram Stars (XTR) vs. Physical Goods

Telegram strictly separates digital virtual goods from physical merchandise to maintain compliance with mobile app store guidelines:

Digital Goods: Telegram Stars (XTR)

Mandatory for Virtual Items & Subscriptions

  • Applies to e-books, online courses, digital software, gaming assets, and VIP channel subscriptions.
  • Set currency = "XTR" and leave provider_token = "" empty.
  • Users purchase Stars in-app via Apple Pay, Google Pay, or Fragment.
  • Developers cash out accumulated Stars to TON cryptocurrency on Fragment after 21 days.
Physical Goods: Merchant Gateways

Credit Cards (Stripe / Smart Glocal)

  • Applies exclusively to physical merchandise, apparel, hardware, and offline delivery services.
  • Obtain a merchant token from @BotFather (e.g. Stripe live token).
  • Supports flexible shipping queries (sendShippingQuery) to calculate dynamic courier rates.
  • 0% Telegram fee: Telegram takes no commission on physical e-commerce transactions.

3. Complete Python Implementation: Stars Invoicing & Pre-Checkout

Below is an asynchronous PTB v20 implementation demonstrating invoice generation, pre-checkout validation, and receipt processing:

ptb_stars_payment_flow.py Telegram Stars (XTR) Flow
from telegram import Update, LabeledPrice
from telegram.ext import ApplicationBuilder, CommandHandler, PreCheckoutQueryHandler, MessageHandler, filters, ContextTypes

async def buy_vip_pass(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    # 1. Dispatch Star Invoice
    prices = [LabeledPrice("VIP Monthly Pass", 150)]  # 150 Telegram Stars
    await context.bot.send_invoice(
        chat_id=update.effective_chat.id,
        title="TGWAY VIP Monthly Pass",
        description="Unlock full access to alpha signals and private community forums.",
        payload="ORDER_VIP_USER_84920",
        currency="XTR",  # Telegram Stars Currency Code
        prices=prices,
        provider_token="" # Empty string for Telegram Stars
    )

async def pre_checkout_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    # 2. Validate Order within 10-Second Window
    query = update.pre_checkout_query
    if query.invoice_payload.startswith("ORDER_VIP_"):
        await query.answer(ok=True)
    else:
        await query.answer(ok=False, error_message="Invalid order payload! Purchase aborted.")

async def successful_payment_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    # 3. Fulfillment & Receipt
    payment = update.message.successful_payment
    charge_id = payment.telegram_payment_charge_id
    total_stars = payment.total_amount
    await update.message.reply_html(
        f"🎉 Payment Successful!
Received: {total_stars} Stars
Receipt ID: {charge_id}"
    )
Interactive Lab Simulator

Payments API Invoice & Stars Checkout Simulator

ENGINE: MTPROTO_PAYMENTS_V2

Invoice Configuration

Telegram Native Payment Sheet INVOICE READY
TGWAY Alpha VIP Pass
1-Month Alpha Signals & Community Forum Access
Total Price: ⭐ 150 Stars
PAYMENTS PROTOCOL TELEMETRY IDLE
[INIT] Payments API listener active.

6. Monetization Comparison: Stars vs. Stripe vs. TON Connect vs. Traditional IAP

Payment System Target Goods Store Policy Status Conversion Fee Settlement Asset
Telegram Stars (XTR) Digital / Virtual Only 100% Compliant (Apple/Google) Standard IAP split TON Cryptocurrency
Credit Cards (Stripe) Physical Goods Only 100% Compliant 0% Telegram Fee (2.9% Stripe) Fiat Bank Transfer (USD/EUR)
TON Connect 2.0 Web3 / Decentralized External Web3 Wallet 0% (Only ~$0.005 network gas) Native TON / USDT-TON
Legacy Native IAP Digital In-App Only Heavy App Store Lock-in 30% Platform Cut Fiat Payout (60-day delay)
insights Master Summary Blueprint

Step 094 Visual Recap: Telegram Payments & Stars Checkout Blueprint

Four-tier in-app monetization architecture: sendInvoice dispatch, dual checkout modal (Stars vs. Credit Card), strict 10-second pre-checkout validation, and Fragment TON cashout pipeline.

Telegram Payments & Stars Checkout Visual Blueprint
Continue Curriculum
Next: Step 095 • TDLib & MTProto Deep Dive: Custom Telegram Clients & Automation
Deconstruct Telegram's binary MTProto 2.0 protocol, TDLib C++ engine bindings, and 4GB multi-socket streaming pipelines.
admin_panel_settings ADMIN Guide #18467 Actions
Enlarged Preview
Click anywhere outside or press ESC to close viewer
smart_display Telegram Video Short
1080p HD
Official Source: @TelegramTips Post #44 Press ESC or click outside to close