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 making a few requests.
I implemented payment functionality using Telegram’s invoice system with Stripe as the payment provider in test mode. The issue occurs when users try to complete the payment - they can enter their card details, but the payment just keeps loading and eventually times out.
I tried both the invoice sending method and creating payment links, but both have the same timeout problem. I’m directly using Telegram API endpoints without any external libraries.
Here’s my payment implementation:
const server = express();
const serverPort = 8080;
const telegramToken = process.env.BOT_TOKEN;
server.use(bodyParser.json());
// Bot webhook handler
server.post(`/webhook${telegramToken}`, async (req, res) => {
const { message } = req.body;
console.log({ message });
if (message && message.text) {
const userId = message.chat.id;
const userMessage = message.text;
// Send payment request after user reaches limit
const paymentResult = await createPaymentInvoice(
userId,
"Pro Access",
"Get unlimited downloads with pro access.",
"pro_access_payment",
process.env.STRIPE_TOKEN,
"access",
"USD",
[{ label: "Pro Access", amount: 500 }],
);
console.log(JSON.stringify(paymentResult, null, 2));
}
res.status(200).end();
});
async function createPaymentInvoice(
userId,
invoiceTitle,
invoiceDesc,
invoicePayload,
stripeToken,
paramStart,
currencyType,
priceList,
) {
const telegramApiUrl = `https://api.telegram.org/bot${telegramToken}/sendInvoice`;
const paymentData = {
chat_id: userId,
title: invoiceTitle,
description: invoiceDesc,
payload: invoicePayload,
provider_token: stripeToken,
start_parameter: paramStart,
currency: currencyType,
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 think I might need to set up some kind of webhook to handle payment completion, but I’m not sure how to configure this properly. The Telegram docs don’t clearly explain if I need to add webhook endpoints in my Stripe dashboard or how Telegram would know about my payment webhook URL.
I’m using ngrok for local development with the webhook setup. My Stripe account is still unverified, which might be causing issues.
Any guidance on what I’m missing would be really helpful. Do I need to handle payment webhooks differently, or is there some configuration I’m missing?