1 | |
2 |
|
3 | |
4 | * Create Payout |
5 | * Send funds from the Stripe balance to the account's default bank account or debit card. `amount` is in the smallest currency unit and must be available in the balance. Set `idempotency_key` to make retries safe: Stripe returns the first result for a repeated key within 24 hours. `extra_params` passes any other documented field through as-is. |
6 | */ |
7 | export async function main( |
8 | auth: RT.Stripe, |
9 | amount: number, |
10 | currency: string, |
11 | description: string | undefined, |
12 | method: "standard" | "instant" | undefined, |
13 | statement_descriptor: string | undefined, |
14 | metadata: { [key: string]: string } | undefined, |
15 | idempotency_key: string | undefined, |
16 | extra_params: { [key: string]: any } | undefined |
17 | ) { |
18 | const response = await fetch(`https://api.stripe.com/v1/payouts`, { |
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 | ...(idempotency_key ? { "Idempotency-Key": idempotency_key } : {}), |
26 | }, |
27 | body: toForm({ |
28 | amount, |
29 | currency, |
30 | description, |
31 | method, |
32 | statement_descriptor, |
33 | metadata, |
34 | ...extra_params, |
35 | }), |
36 | }) |
37 |
|
38 | if (!response.ok) { |
39 | throw new Error(`${response.status} ${await response.text()}`) |
40 | } |
41 |
|
42 | return await response.json() |
43 | } |
44 |
|
45 | |
46 | function toForm( |
47 | params: { [key: string]: any }, |
48 | prefix = "", |
49 | out = new URLSearchParams() |
50 | ) { |
51 | for (const [key, value] of Object.entries(params)) { |
52 | if (value == null || value === "") continue |
53 | const name = prefix ? `${prefix}[${key}]` : key |
54 | if (typeof value === "object") toForm(value, name, out) |
55 | else out.append(name, String(value)) |
56 | } |
57 | return out |
58 | } |
59 |
|