1 | |
2 |
|
3 | |
4 | * Confirm Payment Intent |
5 | * Confirm a PaymentIntent to attempt the payment. `return_url` is needed for redirect-based methods (3D Secure, bank redirects); `off_session` marks a payment made while the customer is not present. `extra_params` passes any other documented field through as-is. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Stripe, |
9 | payment_intent_id: string, |
10 | payment_method: string | undefined, |
11 | return_url: string | undefined, |
12 | receipt_email: string | undefined, |
13 | off_session: boolean | undefined, |
14 | extra_params: { [key: string]: any } | undefined |
15 | ) { |
16 | const response = await fetch( |
17 | `https://api.stripe.com/v1/payment_intents/${payment_intent_id}/confirm`, |
18 | { |
19 | method: "POST", |
20 | headers: { |
21 | Authorization: `Bearer ${auth.token}`, |
22 | "Content-Type": "application/x-www-form-urlencoded", |
23 | Accept: "application/json", |
24 | "Stripe-Version": "2025-03-31.basil", |
25 | }, |
26 | body: toForm({ |
27 | payment_method, |
28 | return_url, |
29 | receipt_email, |
30 | off_session: off_session || undefined, |
31 | ...extra_params, |
32 | }), |
33 | } |
34 | ) |
35 |
|
36 | if (!response.ok) { |
37 | throw new Error(`${response.status} ${await response.text()}`) |
38 | } |
39 |
|
40 | return await response.json() |
41 | } |
42 |
|
43 | |
44 | function toForm( |
45 | params: { [key: string]: any }, |
46 | prefix = "", |
47 | out = new URLSearchParams() |
48 | ) { |
49 | for (const [key, value] of Object.entries(params)) { |
50 | if (value == null || value === "") continue |
51 | const name = prefix ? `${prefix}[${key}]` : key |
52 | if (typeof value === "object") toForm(value, name, out) |
53 | else out.append(name, String(value)) |
54 | } |
55 | return out |
56 | } |
57 |
|