在Bot father選擇Edit bot->Payments->Stripe,可以選擇Connect Stripe Test或者Connect Stripe Live
跳到對應的Stripe bot後,按提示完成認證後會得到payment token
使用node-telegram-bot-api
import TelegramBot from "node-telegram-bot-api";
const botToken = "TELEGRAM BOT TOKEN";
const paymentToken = "STRIPE_TOKEN";
const bot = new TelegramBot(botToken, { polling: true });
const productID = "P_ID_1";
bot.onText(/\/start/, (msg) => {
const chatId = msg.chat.id;
bot.sendMessage(
chatId,
"Welcome to the payment bot. Use /pay to initiate a payment."
);
});
bot.onText(/\/pay/, (msg) => {
const chatId = msg.chat.id;
const payKeyboard = {
reply_markup: JSON.stringify({
inline_keyboard: [
[
{
text: "Pay $10 HKD",
callback_data: productID,
},
],
],
}),
};
bot.sendInvoice(
chatId,
"Title"
"Description",
"Invoice for payment", //payload
paymentToken ,
"HKD",
[{ label: "Product Name", amount: 1000 }], //amount in cents (amount*100)
[payKeyboard]
);
});
bot.on("pre_checkout_query", (msg) => {
/*This function is required by telegram payment, after user perform paymend, this funciton will be called to check the goods is available*/
const queryId = msg.id;
bot.answerPreCheckoutQuery(queryId, true);
});
bot.on("successful_payment", (msg) => {
const chatId = msg.chat.id;
console.log("Payment success.")
});
