1 | type Stripe = { |
2 | token: string; |
3 | }; |
4 | |
5 | * Post customers customer funding instructions |
6 | * Retrieve funding instructions for a customer cash balance. If funding instructions do not yet exist for the customer, new |
7 | funding instructions will be created. If funding instructions have already been created for a given customer, the same |
8 | funding instructions will be retrieved. In other words, we will return the same funding instructions each time. |
9 | */ |
10 | export async function main( |
11 | auth: Stripe, |
12 | customer: string, |
13 | body: { |
14 | bank_transfer: { |
15 | eu_bank_transfer?: { country: string; [k: string]: unknown }; |
16 | requested_address_types?: ("iban" | "sort_code" | "spei" | "zengin")[]; |
17 | type: |
18 | | "eu_bank_transfer" |
19 | | "gb_bank_transfer" |
20 | | "jp_bank_transfer" |
21 | | "mx_bank_transfer" |
22 | | "us_bank_transfer"; |
23 | [k: string]: unknown; |
24 | }; |
25 | currency: string; |
26 | expand?: string[]; |
27 | funding_type: "bank_transfer"; |
28 | } |
29 | ) { |
30 | const url = new URL( |
31 | `https://api.stripe.com/v1/customers/${customer}/funding_instructions` |
32 | ); |
33 |
|
34 | const response = await fetch(url, { |
35 | method: "POST", |
36 | headers: { |
37 | "Content-Type": "application/x-www-form-urlencoded", |
38 | Authorization: "Bearer " + auth.token, |
39 | }, |
40 | body: encodeParams(body), |
41 | }); |
42 | if (!response.ok) { |
43 | const text = await response.text(); |
44 | throw new Error(`${response.status} ${text}`); |
45 | } |
46 | return await response.json(); |
47 | } |
48 |
|
49 | function encodeParams(o: any) { |
50 | function iter(o: any, path: string) { |
51 | if (Array.isArray(o)) { |
52 | o.forEach(function (a) { |
53 | iter(a, path + "[]"); |
54 | }); |
55 | return; |
56 | } |
57 | if (o !== null && typeof o === "object") { |
58 | Object.keys(o).forEach(function (k) { |
59 | iter(o[k], path + "[" + k + "]"); |
60 | }); |
61 | return; |
62 | } |
63 | data.push(path + "=" + o); |
64 | } |
65 | const data: string[] = []; |
66 | Object.keys(o).forEach(function (k) { |
67 | if (o[k] !== undefined) { |
68 | iter(o[k], k); |
69 | } |
70 | }); |
71 | return new URLSearchParams(data.join("&")); |
72 | } |
73 |
|