Task
TON payments had to be accepted right in Telegram without checking transfers by hand. The wallet keys must not live on the server, and every payment has to find its own invoice without doubt.
Solution
A python-telegram-bot bot runs the dialog and writes invoices to PostgreSQL. A background job in the same process reads transfers through toncenter every 20 seconds, continues from the last processed transaction and matches a transfer to an invoice by its comment. All database queries are parameterised.
What was done
- Built invoices for any amount with a unique comment from the bot menu
- Gave a Tonkeeper button with the address, amount and comment filled in
- Built a watcher that reads the merchant wallet and matches transfers to invoices
- Settle a payment only when the comment matches and the amount covers the invoice
- Expire unpaid invoices on time and tell the buyer
- Store amounts as whole nanotons, with no floating point
- Needed no private keys: the service only uses the public address
Result
The buyer pays in a couple of taps and gets a confirmation in the chat right away, and the merchant no longer checks transfers. The code is open on GitHub, and the bot can be tried on the TON testnet first.
Technologies
Pythonpython-telegram-botasyncioPostgreSQLasyncpgtoncenter API