1 | |
2 |
|
3 | |
4 | * Capture Payment Intent |
5 | * Capture the funds of a PaymentIntent in `requires_capture` status. Leave `amount_to_capture` empty to capture the full amount; the remainder of a partial capture is released. Uncaptured intents are canceled after 7 days by default. `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 | amount_to_capture: number | undefined, |
11 | metadata: { [key: string]: string } | undefined, |
12 | extra_params: { [key: string]: any } | undefined |
13 | ) { |
14 | const response = await fetch( |
15 | `https://api.stripe.com/v1/payment_intents/${payment_intent_id}/capture`, |
16 | { |
17 | method: "POST", |
18 | headers: { |
19 | Authorization: `Bearer ${auth.token}`, |
20 | "Content-Type": "application/x-www-form-urlencoded", |
21 | Accept: "application/json", |
22 | "Stripe-Version": "2025-03-31.basil", |
23 | }, |
24 | body: toForm({ amount_to_capture, metadata, ...extra_params }), |
25 | } |
26 | ) |
27 |
|
28 | if (!response.ok) { |
29 | throw new Error(`${response.status} ${await response.text()}`) |
30 | } |
31 |
|
32 | return await response.json() |
33 | } |
34 |
|
35 | |
36 | function toForm( |
37 | params: { [key: string]: any }, |
38 | prefix = "", |
39 | out = new URLSearchParams() |
40 | ) { |
41 | for (const [key, value] of Object.entries(params)) { |
42 | if (value == null || value === "") continue |
43 | const name = prefix ? `${prefix}[${key}]` : key |
44 | if (typeof value === "object") toForm(value, name, out) |
45 | else out.append(name, String(value)) |
46 | } |
47 | return out |
48 | } |
49 |
|