yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.12k stars 1.49k forks source link

Pre-Checkout Timeout Error while making a Payment #1187

Open sojinsamuel opened 3 months ago

sojinsamuel commented 3 months ago

i've built a telegram bot using nodejs that downloads videos from tweet urls. Completed that part. now, Iim struggling to implement a paywall using the sendInvoice method in the Telegram API.

https://core.telegram.org/bots/api#sendinvoice

i'm using Stripe for payments in test mode. however, after sending the invoice, when users try to pay, it buffers and times out as in the image.

Screenshot from 2024-03-31 18-59-16

Question i asked on stackoverflow (my version 1 code without using any external libs for the bot): https://stackoverflow.com/questions/78251916/stripe-pre-checkout-timeout-error-while-making-a-test-payment-from-telegram-bot

Not sure what was wrong there at the first time; i did find this similar problem on stackoverflow: https://stackoverflow.com/questions/73341054/telegram-sendinvoice-method-is-failing

Which i almost thought i solved it, but i am not even receiving the update object as explained here https://core.telegram.org/bots/api#answershippingquery

So finally to implement what's being said and to make things simple i started to use this library node-telegram-bot-api (previously used rest api from telegram)

Even after making the changes suggested from the docs and on the similar stackoverflow solution above.

I have come to this (an abstracted version 2 of my source code):

import { config } from "dotenv";
import TelegramBot from "node-telegram-bot-api";
import getTwitterVideoURL from "./twitterfunction.js";

config();

const token = process.env.TELEGRAM_BOT_TOKEN;
// console.log(token);
const bot = new TelegramBot(token, { polling: true });

bot.on("message", async (msg) => {
  //   console.log(JSON.stringify(msg, null, 2));
  const chatId = msg.chat.id;
  const message = msg.text.trim();

  console.log(JSON.stringify(msg, null, 2));

  bot.sendInvoice(
    chatId,
    "Title goes here",
    "Unlock premium content with a subscription",
    "Invoice for payment",
    "MY_PROVIDER_TOKEN",
    "USD",
    [{ label: "tax", amount: 4000 }],
    {
      is_flexible: false,
      need_phone_number: false,
      need_email: true,
      need_shipping_address: false,
    }
  );

});

bot.on("pre_checkout_query", async (query) => {
  console.log("Received pre-checkout query:", query);

  // Answer the pre-checkout query within 10 seconds more info: https://core.telegram.org/bots/api#answerprecheckoutquery

  const preCheckoutQueryAnswer = await bot.answerPreCheckoutQuery(
    query.id,
    true
  );

  console.log("Pre-checkout query answer:", preCheckoutQueryAnswer);
});

bot.on("successful_payment", async (payload) => {
  console.log("Successful payment:", payload);

  const message = `Thank you for your payment of ${
    payload.total_amount / 100
  } ${payload.currency}!`;
  await bot.sendMessage(payload.chat_id, message);
});

bot.on("invoice", (error) => {
  console.log("invoice", error);
});

bot.on("shipping_query", async (query) => {
  console.log("Received shipping query:", query);

  // Don't have any shipping options, provide an empty array
  const shippingOptions = [];

  const shippingQueryAnswer = await bot.answerShippingQuery(
    query.id,
    true, // Set to false if the shipping cannot be processed
    { shipping_options: shippingOptions }
  );

  console.log("Shipping query answer:", shippingQueryAnswer);
});

Still the same problem. Tried other payment provider too no luck.

Can anyone help me out please.

werniq commented 1 month ago

Hi. Have you solved it?

sojinsamuel commented 1 month ago

no, I havent worked on it for a while and stripe doesnt work on india anymore. will you lmk if you find a solution

On Tue, Jun 4, 2024 at 5:55 PM qniwerss @.***> wrote:

Hi. Have you solved it?

— Reply to this email directly, view it on GitHub https://github.com/yagop/node-telegram-bot-api/issues/1187#issuecomment-2147406969, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASM4GGBZPSY5YPLROQVBKNLZFWW37AVCNFSM6AAAAABFR32QZ6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNBXGQYDMOJWHE . You are receiving this because you authored the thread.Message ID: @.***>

werniq commented 1 month ago

Yes, it is related to pre_checkout_query. So after client clicks Pay button, some pre checkout query is sent to your bot. You need to handle responding to this query, and everything will be ok.

sojinsamuel commented 1 month ago

Yes, it is related to pre_checkout_query. So after client clicks Pay button, some pre checkout query is sent to your bot. You need to handle responding to this query, and everything will be ok.

bot.on("pre_checkout_query", async (query) => {
  console.log("Received pre-checkout query:", query);

  // Answer the pre-checkout query within 10 seconds more info: https://core.telegram.org/bots/api#answerprecheckoutquery

  const preCheckoutQueryAnswer = await bot.answerPreCheckoutQuery(
    query.id,
    true
  );

  console.log("Pre-checkout query answer:", preCheckoutQueryAnswer);
});

But i didnt get any payload after clicking on pay button, is it possible to share any code snippets for this issue or an update. anyways thanks a lot for contributing to the conversation :smiley:

werniq commented 4 weeks ago

in python my handler looks like this

@dp.pre_checkout_query()
async def pre_checkout_query_handler(pre_checkout_query: types.pre_checkout_query):
    await bot.answer_pre_checkout_query(pre_checkout_query.id, ok=True)