1 | type Stripe = { |
2 | token: string; |
3 | }; |
4 |
|
5 | * Post accounts account bank accounts |
6 | * Create an external account for a given account. |
7 | */ |
8 | export async function main( |
9 | auth: Stripe, |
10 | account: string, |
11 | body: { |
12 | bank_account?: |
13 | | { |
14 | account_holder_name?: string; |
15 | account_holder_type?: "company" | "individual"; |
16 | account_number: string; |
17 | account_type?: "checking" | "futsu" | "savings" | "toza"; |
18 | country: string; |
19 | currency?: string; |
20 | documents?: { |
21 | bank_account_ownership_verification?: { |
22 | files?: string[]; |
23 | [k: string]: unknown; |
24 | }; |
25 | [k: string]: unknown; |
26 | }; |
27 | object?: "bank_account"; |
28 | routing_number?: string; |
29 | [k: string]: unknown; |
30 | } |
31 | | string; |
32 | default_for_currency?: boolean; |
33 | expand?: string[]; |
34 | external_account?: string; |
35 | metadata?: { [k: string]: string }; |
36 | } |
37 | ) { |
38 | const url = new URL( |
39 | `https://api.stripe.com/v1/accounts/${account}/bank_accounts` |
40 | ); |
41 |
|
42 | const response = await fetch(url, { |
43 | method: "POST", |
44 | headers: { |
45 | "Content-Type": "application/x-www-form-urlencoded", |
46 | Authorization: "Bearer " + auth.token, |
47 | }, |
48 | body: encodeParams(body), |
49 | }); |
50 | if (!response.ok) { |
51 | const text = await response.text(); |
52 | throw new Error(`${response.status} ${text}`); |
53 | } |
54 | return await response.json(); |
55 | } |
56 |
|
57 | function encodeParams(o: any) { |
58 | function iter(o: any, path: string) { |
59 | if (Array.isArray(o)) { |
60 | o.forEach(function (a) { |
61 | iter(a, path + "[]"); |
62 | }); |
63 | return; |
64 | } |
65 | if (o !== null && typeof o === "object") { |
66 | Object.keys(o).forEach(function (k) { |
67 | iter(o[k], path + "[" + k + "]"); |
68 | }); |
69 | return; |
70 | } |
71 | data.push(path + "=" + o); |
72 | } |
73 | const data: string[] = []; |
74 | Object.keys(o).forEach(function (k) { |
75 | if (o[k] !== undefined) { |
76 | iter(o[k], k); |
77 | } |
78 | }); |
79 | return new URLSearchParams(data.join("&")); |
80 | } |
81 |
|