1 | type Stripe = { |
2 | token: string; |
3 | }; |
4 | |
5 | * Get treasury outbound payments |
6 | * Returns a list of OutboundPayments sent from the specified FinancialAccount. |
7 | */ |
8 | export async function main( |
9 | auth: Stripe, |
10 | created: any, |
11 | customer: string | undefined, |
12 | ending_before: string | undefined, |
13 | expand: any, |
14 | financial_account: string | undefined, |
15 | limit: string | undefined, |
16 | starting_after: string | undefined, |
17 | status: |
18 | | "canceled" |
19 | | "failed" |
20 | | "posted" |
21 | | "processing" |
22 | | "returned" |
23 | | undefined |
24 | ) { |
25 | const url = new URL(`https://api.stripe.com/v1/treasury/outbound_payments`); |
26 | for (const [k, v] of [ |
27 | ["customer", customer], |
28 | ["ending_before", ending_before], |
29 | ["financial_account", financial_account], |
30 | ["limit", limit], |
31 | ["starting_after", starting_after], |
32 | ["status", status], |
33 | ]) { |
34 | if (v !== undefined && v !== "") { |
35 | url.searchParams.append(k, v); |
36 | } |
37 | } |
38 | encodeParams({ created, expand }).forEach((v, k) => { |
39 | if (v !== undefined && v !== "") { |
40 | url.searchParams.append(k, v); |
41 | } |
42 | }); |
43 | const response = await fetch(url, { |
44 | method: "GET", |
45 | headers: { |
46 | "Content-Type": "application/x-www-form-urlencoded", |
47 | Authorization: "Bearer " + auth.token, |
48 | }, |
49 | body: undefined, |
50 | }); |
51 | if (!response.ok) { |
52 | const text = await response.text(); |
53 | throw new Error(`${response.status} ${text}`); |
54 | } |
55 | return await response.json(); |
56 | } |
57 |
|
58 | function encodeParams(o: any) { |
59 | function iter(o: any, path: string) { |
60 | if (Array.isArray(o)) { |
61 | o.forEach(function (a) { |
62 | iter(a, path + "[]"); |
63 | }); |
64 | return; |
65 | } |
66 | if (o !== null && typeof o === "object") { |
67 | Object.keys(o).forEach(function (k) { |
68 | iter(o[k], path + "[" + k + "]"); |
69 | }); |
70 | return; |
71 | } |
72 | data.push(path + "=" + o); |
73 | } |
74 | const data: string[] = []; |
75 | Object.keys(o).forEach(function (k) { |
76 | if (o[k] !== undefined) { |
77 | iter(o[k], k); |
78 | } |
79 | }); |
80 | return new URLSearchParams(data.join("&")); |
81 | } |
82 |
|