Handling successful_payment reliably in a Telegram Stars bot
How to handle successful_payment reliably in a Telegram Stars bot: idempotent orders, storing the charge ID, retries, crash recovery and daily reconciliation.
Published:

Handling successful_payment reliably in a Telegram Stars bot means that every paid order is delivered exactly once, even if your server restarts, an update arrives twice or delivery fails halfway. The core recipe is simple: save the payment first, using telegram_payment_charge_id as a unique key, then deliver from a queue that can retry, and reconcile your records with Telegram regularly. This guide explains each step.
What you need before you start
- A database with transactions. Any database that supports unique constraints and atomic writes is enough.
- An orders table. Create the order before you send the invoice, and put its ID in the
invoice_payloadso you can match the payment later. - A clear delivery function. One function that grants the item for an order and can safely run more than once.
- Basic monitoring. Logs and an alert when payments are recorded but not delivered.
If you are new to the payment flow itself, read our step-by-step guide to Stars invoices first.
Step-by-step instructions
- Validate in pre-checkout. When the
pre_checkout_queryarrives, check that the order exists, is still unpaid and the price matches. Answer withanswerPreCheckoutQueryquickly; the Bot API documentation sets a short deadline of a few seconds, so do not run slow work here. - Record the payment first. When
successful_paymentarrives, write a payment row with thetelegram_payment_charge_id, user ID, amount, currencyXTRand payload. Put a unique constraint on the charge ID. - Make the write idempotent. If the same update arrives again, the unique constraint rejects the duplicate. Treat that as "already recorded" and continue, not as an error.
- Mark the order as paid in the same transaction. Updating the order and inserting the payment together means you never have a payment without a paid order, or the other way round.
- Acknowledge the update. Return success to Telegram only after the payment is safely stored. With webhooks, a quick successful response tells Telegram the update was received.
- Deliver from a job queue. Put a delivery job in a queue or a "pending delivery" table. A worker grants the item and marks the order as delivered. If delivery fails, the job is retried with a delay.
- Notify the user. After delivery, send a short confirmation with the item and an order reference. If delivery is delayed, tell the user that the payment was received and the item is on the way.
- Reconcile daily. Compare your payment records with Telegram's transaction list, which bots can read with
getStarTransactions. Investigate anything that appears on one side only.
Options and settings explained
| Choice | Option | When it fits |
|---|---|---|
| Receiving updates | Webhook | Production bots; fast and scalable, needs HTTPS |
| Receiving updates | getUpdates (long polling) | Small bots and testing; confirm updates with the offset only after saving |
| Delivery | Inline, in the update handler | Very simple, fast items; risky if delivery can fail |
| Delivery | Queue with retries | Anything that calls other services, generates files or can time out |
| Duplicate protection | Unique key on charge ID | Always; the simplest reliable guard |
Crash recovery
Think about each point where the process can stop. If the server crashes before the payment is saved, Telegram can deliver the update again, and your idempotent write handles it. If it crashes after saving but before delivery, the pending job stays in the queue and runs after restart. If it crashes after delivery but before marking the job done, the delivery function runs again, so it must be safe to repeat, for example by checking whether access is already granted.
Subscriptions
For Stars subscriptions, each renewal arrives as its own successful_payment with a new charge ID. Treat every renewal as a new payment row linked to the same subscription, and extend access only when that row is saved.
Common mistakes and how to avoid them
- Delivering before saving. If the save then fails, you have no record of the payment and cannot refund it. Save first, deliver second.
- Using the payload as the unique key. A user may pay for the same order twice by mistake. The charge ID is unique per payment; the payload is not.
- Slow pre-checkout handlers. Database migrations or external calls in pre-checkout cause failed payments. Keep it fast.
- Silent failures. A delivery job that fails forever without an alert leaves paid users waiting. Alert after a few failed attempts.
- No reconciliation. Small gaps grow unnoticed. A daily comparison catches lost or duplicated records early.
If a user reports a missing item, look up the payment by charge ID, check the job status and re-run delivery. If you cannot deliver, refund with refundStarPayment and say so clearly. Our article on refunds and /paysupport explains what users expect.
Where PayStars fits
The same principles apply when your bot buys Stars or Premium for users through the PayStars API: send an Idempotency-Key header with every purchase so a retry returns the original order instead of creating a second one, and check order status with GET /order/{id}. See the Python example for a working pattern.
FAQ
Why might my bot receive successful_payment twice?
Network problems or server restarts can cause an update to be delivered again. A unique key on the charge ID makes the second copy harmless.
What should be the unique key for a Stars payment?
Use telegram_payment_charge_id. It is unique for each payment, while your invoice payload may repeat if a user pays twice.
Should I deliver the item inside the update handler?
Only for very simple and fast items. For anything that can fail or take time, record the payment and deliver from a queue with retries.
How do I check that my records match Telegram's?
Read the bot's Stars transactions with getStarTransactions and compare them with your payment table on a regular schedule.
What if delivery fails permanently?
Tell the user, then refund the payment with refundStarPayment using the stored charge ID, and log the case.
Sources
Buy Stars and Premium with USD (USDT)
Via API key: 0% (plain Fragment price). Via the website: 5%.
More news
Refunding a Telegram Stars payment as a developer: when and howRefunding a Telegram Stars payment as a developer: when to refund, how refundStarPayment works, what to log, how to tell the user and how to limit abuse.Read →
How to price a Telegram channel subscription in StarsHow to price a Telegram channel subscription in Stars: define your value, compare similar channels, use separate channels as tiers and offer fair trials.Read →
Delivering digital goods after a Telegram Stars payment: files, keys and linksDelivering digital goods after a Telegram Stars payment: sending files, licence keys and access links, expiring links, re-delivery and what to do when delivery fails.Read →