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

Stripe Pre-Checkout Timeout Error while making a Test Payment from Telegram Bot #552

Open
sojinsamuel opened this issue Mar 31, 2024 · 1 comment

Comments

@sojinsamuel
Copy link

i have being building a telegram bot with nodejs where people can send in a url of a tweet and if it contains a media of type video. it downloads it and sends it to the user. Thats the purpose, which i already completed.

The problem i have is with setting a paywall on this bot account so that after 2 or 3 request, the user interacting with the bot have to make a payment to continue.

so i used the sendInvoice method.

FYI, i'm not using any external librarys, i'm directly interacting with the telegram api endpoints.

I'm using stripe for payments in test mode (my acc is unverified if that helps). the problem i have is as you can see in this picture:

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

After i send the invoice, then the user clicks the pay button and adds all the necessary card details then after hitting the pay. it keeps buffering and then time outs.

I also tried the createInvoiceLink method and the same thing happened.

Ofcourse its some mistake in my solution, but i dont know where that is. May be i have to use a webhook or something to catch the checkout initiation/completion. but how can i let the Telegram api know about my payment webhook path (assuming something like that exists).

The only one i found is the method setWebhook which is an alternative approach for polling. And that is what i am doing locally with ngrok.

A Part of my code, this is an abstract version of the functionality:

const app = express();
const port = 3000;

const botToken = process.env.TELEGRAM_BOT_TOKEN;
app.use(bodyParser.json());

// Webhook endpoint that receives incoming messages (exposed via ngrok)
app.post(`/bot${botToken}`, async (req, res) => {
  const { message } = req.body;

  console.log({ message });

  if (message && message.text) {
    const chatId = message.chat.id;
    const messageText = message.text;

    // after successfull 3 responses from the bot, send an invoice

    const invoiceRes = await sendInvoice(
      chatId,
      "Premium Subscription",
      "Unlock premium content with a subscription.",
      "premium_subscription",
      process.env.PROVIDER_TOKEN,
      "subscription",
      "USD",
      [{ label: "Subscription", amount: 1000 }],
    );

    console.log(JSON.stringify(invoiceRes, null, 2));
  }

  res.status(200).end();
});

async function sendInvoice(
  chatId,
  title,
  description,
  payload,
  providerToken,
  startParameter,
  currency,
  prices,
) {
  const apiUrl = `https://api.telegram.org/bot${botToken}/sendInvoice`;

  const invoice = {
    chat_id: chatId,
    title: title,
    description: description,
    payload: payload,
    provider_token: providerToken,
    start_parameter: startParameter,
    currency: currency,
    prices: prices,
  };

  try {
    const response = await fetch(apiUrl, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(invoice),
    });

    const data = await response.json();
    // console.log(data);
    return data;
  } catch (error) {
    console.error("There was a problem sending the invoice:", error);
    return error.message;
  }
}

I googled a lot to find some answers, there isnt anything specified in the payment section of the docs either rather than getting the provider token (not anything specifically if i have to do on the stripe account dashboard like adding a webhook endpoint, even if that was the case how would telegram know that is the webhook it should communicate too)

I have been mentioning webhook a lot, because in my mind i am assuming that is my missing solution. if it isnt i'm sorry for doing so. I don't have much experience with stripe or building a telegram bot.

I hope i could get some help with this problem. even a small guidance will be enough if you are busy. just something i can go with that's all i ask.

https://stackoverflow.com/questions/78251916/stripe-pre-checkout-timeout-error-while-making-a-test-payment-from-telegram-bot

@KnorpelSenf
Copy link
Member

Is this in any way related to grammY?

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

2 participants