1 | type Stripe = { |
2 | token: string; |
3 | }; |
4 | |
5 | * Post treasury outbound payments |
6 | * Creates an OutboundPayment. |
7 | */ |
8 | export async function main( |
9 | auth: Stripe, |
10 | body: { |
11 | amount: number; |
12 | currency: string; |
13 | customer?: string; |
14 | description?: string; |
15 | destination_payment_method?: string; |
16 | destination_payment_method_data?: { |
17 | billing_details?: { |
18 | address?: |
19 | | { |
20 | city?: string; |
21 | country?: string; |
22 | line1?: string; |
23 | line2?: string; |
24 | postal_code?: string; |
25 | state?: string; |
26 | [k: string]: unknown; |
27 | } |
28 | | ""; |
29 | email?: string | ""; |
30 | name?: string | ""; |
31 | phone?: string | ""; |
32 | [k: string]: unknown; |
33 | }; |
34 | financial_account?: string; |
35 | metadata?: { [k: string]: string }; |
36 | type: "financial_account" | "us_bank_account"; |
37 | us_bank_account?: { |
38 | account_holder_type?: "company" | "individual"; |
39 | account_number?: string; |
40 | account_type?: "checking" | "savings"; |
41 | financial_connections_account?: string; |
42 | routing_number?: string; |
43 | [k: string]: unknown; |
44 | }; |
45 | [k: string]: unknown; |
46 | }; |
47 | destination_payment_method_options?: { |
48 | us_bank_account?: |
49 | | { network?: "ach" | "us_domestic_wire"; [k: string]: unknown } |
50 | | ""; |
51 | [k: string]: unknown; |
52 | }; |
53 | end_user_details?: { |
54 | ip_address?: string; |
55 | present: boolean; |
56 | [k: string]: unknown; |
57 | }; |
58 | expand?: string[]; |
59 | financial_account: string; |
60 | metadata?: { [k: string]: string }; |
61 | statement_descriptor?: string; |
62 | } |
63 | ) { |
64 | const url = new URL(`https://api.stripe.com/v1/treasury/outbound_payments`); |
65 |
|
66 | const response = await fetch(url, { |
67 | method: "POST", |
68 | headers: { |
69 | "Content-Type": "application/x-www-form-urlencoded", |
70 | Authorization: "Bearer " + auth.token, |
71 | }, |
72 | body: encodeParams(body), |
73 | }); |
74 | if (!response.ok) { |
75 | const text = await response.text(); |
76 | throw new Error(`${response.status} ${text}`); |
77 | } |
78 | return await response.json(); |
79 | } |
80 |
|
81 | function encodeParams(o: any) { |
82 | function iter(o: any, path: string) { |
83 | if (Array.isArray(o)) { |
84 | o.forEach(function (a) { |
85 | iter(a, path + "[]"); |
86 | }); |
87 | return; |
88 | } |
89 | if (o !== null && typeof o === "object") { |
90 | Object.keys(o).forEach(function (k) { |
91 | iter(o[k], path + "[" + k + "]"); |
92 | }); |
93 | return; |
94 | } |
95 | data.push(path + "=" + o); |
96 | } |
97 | const data: string[] = []; |
98 | Object.keys(o).forEach(function (k) { |
99 | if (o[k] !== undefined) { |
100 | iter(o[k], k); |
101 | } |
102 | }); |
103 | return new URLSearchParams(data.join("&")); |
104 | } |
105 |
|