I’m working on a Node.js Telegram bot that downloads video content from social media posts. Everything works fine but I want to add a payment system where users need to pay after using the service a few times.
I implemented Stripe payments using Telegram’s invoice feature. The bot sends an invoice and users can enter their card details, but when they click the final pay button, it just keeps loading forever and eventually times out. I’m using Stripe in test mode since my account isn’t verified yet.
I tried both the regular invoice method and the invoice link approach but got the same timeout issue. I think maybe I need to set up some kind of webhook to handle the payment completion, but I’m not sure how to connect that with Telegram’s API.
Currently I’m using ngrok for local development and the standard webhook setup for receiving messages. Here’s my payment code:
const server = express();
const serverPort = 8000;
const telegramToken = process.env.BOT_TOKEN;
server.use(bodyParser.json());
// Message handler endpoint
server.post(`/webhook${telegramToken}`, async (req, res) => {
const { message } = req.body;
if (message && message.text) {
const userId = message.chat.id;
const userMessage = message.text;
// Send payment request after user hits usage limit
const paymentResult = await createPaymentInvoice(
userId,
"Pro Access",
"Get unlimited access to video downloads.",
"pro_access_payment",
process.env.STRIPE_PROVIDER_TOKEN,
"payment",
"USD",
[{ label: "Pro Access", amount: 1500 }],
);
console.log(JSON.stringify(paymentResult, null, 2));
}
res.status(200).end();
});
async function createPaymentInvoice(
userId,
invoiceTitle,
invoiceDescription,
invoicePayload,
stripeToken,
parameterStart,
currencyCode,
priceList,
) {
const telegramApiUrl = `https://api.telegram.org/bot${telegramToken}/sendInvoice`;
const paymentData = {
chat_id: userId,
title: invoiceTitle,
description: invoiceDescription,
payload: invoicePayload,
provider_token: stripeToken,
start_parameter: parameterStart,
currency: currencyCode,
prices: priceList,
};
try {
const apiResponse = await fetch(telegramApiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(paymentData),
});
const responseData = await apiResponse.json();
return responseData;
} catch (err) {
console.error("Invoice sending failed:", err);
return err.message;
}
}
I’ve searched everywhere but can’t find clear documentation about whether I need to configure anything special in Stripe or if there’s a specific webhook setup required for Telegram payments. Any help would be appreciated!