1 | type Stripe = { |
2 | token: string; |
3 | }; |
4 | |
5 | * Post account sessions |
6 | * Creates a AccountSession object that includes a single-use token that the platform can use on their front-end to grant client-side API access. |
7 | */ |
8 | export async function main( |
9 | auth: Stripe, |
10 | body: { |
11 | account: string; |
12 | components: { |
13 | account_onboarding?: { |
14 | enabled: boolean; |
15 | features?: { [k: string]: unknown }; |
16 | [k: string]: unknown; |
17 | }; |
18 | documents?: { |
19 | enabled: boolean; |
20 | features?: { [k: string]: unknown }; |
21 | [k: string]: unknown; |
22 | }; |
23 | payment_details?: { |
24 | enabled: boolean; |
25 | features?: { |
26 | capture_payments?: boolean; |
27 | dispute_management?: boolean; |
28 | refund_management?: boolean; |
29 | [k: string]: unknown; |
30 | }; |
31 | [k: string]: unknown; |
32 | }; |
33 | payments?: { |
34 | enabled: boolean; |
35 | features?: { |
36 | capture_payments?: boolean; |
37 | dispute_management?: boolean; |
38 | refund_management?: boolean; |
39 | [k: string]: unknown; |
40 | }; |
41 | [k: string]: unknown; |
42 | }; |
43 | payouts?: { |
44 | enabled: boolean; |
45 | features?: { |
46 | edit_payout_schedule?: boolean; |
47 | instant_payouts?: boolean; |
48 | standard_payouts?: boolean; |
49 | [k: string]: unknown; |
50 | }; |
51 | [k: string]: unknown; |
52 | }; |
53 | [k: string]: unknown; |
54 | }; |
55 | expand?: string[]; |
56 | } |
57 | ) { |
58 | const url = new URL(`https://api.stripe.com/v1/account_sessions`); |
59 |
|
60 | const response = await fetch(url, { |
61 | method: "POST", |
62 | headers: { |
63 | "Content-Type": "application/x-www-form-urlencoded", |
64 | Authorization: "Bearer " + auth.token, |
65 | }, |
66 | body: encodeParams(body), |
67 | }); |
68 | if (!response.ok) { |
69 | const text = await response.text(); |
70 | throw new Error(`${response.status} ${text}`); |
71 | } |
72 | return await response.json(); |
73 | } |
74 |
|
75 | function encodeParams(o: any) { |
76 | function iter(o: any, path: string) { |
77 | if (Array.isArray(o)) { |
78 | o.forEach(function (a) { |
79 | iter(a, path + "[]"); |
80 | }); |
81 | return; |
82 | } |
83 | if (o !== null && typeof o === "object") { |
84 | Object.keys(o).forEach(function (k) { |
85 | iter(o[k], path + "[" + k + "]"); |
86 | }); |
87 | return; |
88 | } |
89 | data.push(path + "=" + o); |
90 | } |
91 | const data: string[] = []; |
92 | Object.keys(o).forEach(function (k) { |
93 | if (o[k] !== undefined) { |
94 | iter(o[k], k); |
95 | } |
96 | }); |
97 | return new URLSearchParams(data.join("&")); |
98 | } |
99 |
|