Search code examples
javascriptnode.jstelegramtelegram-bottelegram-api

Telegram bot payments - show receipt after successful payment


I have a Telegram bot that processes payments. The payments work as they should, however, I have not been able to show receipt after a successful payment.

The current behavior is:

  1. user clicks on PAY button, fills the card information and pays for the service
  2. payment is processed and message about successful transaction is sent
  3. at this point, I would like for the PAY button to change to RECEIPT button

The current behavior on screenshot:

current behavior

Desired behavior:

desired behavior

The desired behavior was screenshoted from chat with @ShopBot, which is mentioned in Telegram docs as a test tool.

The only mention about how the "receipt" is handled I could find in the Telegram docs were these two sentences at https://core.telegram.org/bots/payments :

If the invoice message was sent in the chat with @merchantbot, it becomes a Receipt in the UI for the user — they can open this receipt at any time and see all the details of the transaction.

If the message was sent to any other chat, the Pay button remains and can be used again. It is up to the merchant bot whether to actually accept multiple payments.

However, I don't understand how to achieve this in the code. As far as I know, the invoice message WAS sent to the chat with my bot (as in the first sentence) so it SHOULD become a Receipt.

The bot is written in Node.js and uses webhook to handle messages. The code section of the webhook important for this question:

router.route('/')
    .post(async (req, res) => {
        try {

            // if pre_checkout_query is defined, there was an attempt for payment
            if (req.body.pre_checkout_query) {
                // use answerPreCheckoutQuery Telegram method
                ...
            }

            const message = req.body.message || req.body.edited_message;

            // this means user's payment was successful
            if (message.successful_payment) {
                // success, deliver goods or services
                // send message about successful payment
                ...
            }


        } catch (err) {
            ...
        }
    })

The invoice is sent with sendInvoice method like this:


const url = `https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}/sendInvoice`;
const response = await axios.get(url, {
   params: {
      chat_id: chatID,
      title: 'SOME TITLE',
      description: 'some decription',
      payload: 'SOME-PAYLOAD',
      provider_token: process.env.STRIPE_API_TOKEN,
      currency: 'EUR',
      prices: JSON.stringify([
         {
            label: 'some label',
            amount: 200,
         },
      ]),
   },
});

The two methods from the API that are used for processing the payments are sendInvoice and answerPreCheckoutQuery but neither of them contains any argument that would possibly change the output the way I want. Am I missing something?

Note at the end: despite all this, the payments works. This is just a cosmetic change I would like to achieve.


Solution

  • I also had this problem, specify a parameter: start_parameter='unique-string'