Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-Checkout Timeout Error while making a Payment #1187

Open
sojinsamuel opened this issue Apr 1, 2024 · 0 comments
Open

Pre-Checkout Timeout Error while making a Payment #1187

sojinsamuel opened this issue Apr 1, 2024 · 0 comments

Comments

@sojinsamuel
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant