Skip to content

Commit 653827c

Browse files
committed
Add Payments Walkthrough section
1 parent 34cdef1 commit 653827c

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

src/payments.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
title: Payments
3+
parent: /#walkthrough
4+
walkthrough:
5+
track: bot
6+
order: 4
7+
---
8+
9+
Bots can accept payments by sending invoices and handling checkout updates.
10+
11+
## Sending an Invoice
12+
13+
Use {{ "sendInvoice" |> m }} to send an invoice. This example charges 100 Telegram Stars.
14+
15+
```ts
16+
await client.sendInvoice(
17+
chatId,
18+
"Premium access",
19+
"Unlocks premium features.",
20+
"premium-access",
21+
"XTR",
22+
[{ label: "Premium access", amount: 100 }],
23+
);
24+
```
25+
26+
The payload identifies the purchase and is included in subsequent payment updates.
27+
28+
## Confirming Checkout
29+
30+
Validate each `preCheckoutQuery` before allowing the payment to proceed.
31+
32+
```ts
33+
client.on("preCheckoutQuery", async (ctx) => {
34+
const { invoicePayload } = ctx.update.preCheckoutQuery;
35+
36+
if (invoicePayload === "premium-access") {
37+
await ctx.answerPreCheckoutQuery(true);
38+
} else {
39+
await ctx.answerPreCheckoutQuery(false, {
40+
error: "This item is no longer available.",
41+
});
42+
}
43+
});
44+
```
45+
46+
## Handling Successful Payments
47+
48+
Fulfill the purchase after receiving a `successfulPayment` message.
49+
50+
```ts
51+
client.on("message:successfulPayment", (ctx) => {
52+
const payment = ctx.msg.successfulPayment;
53+
console.log("Payment received for", payment.invoicePayload);
54+
});
55+
```

0 commit comments

Comments
 (0)