1 | type Stripe = { |
2 | token: string; |
3 | }; |
4 | |
5 | * Post test helpers treasury outbound payments id return |
6 | * Transitions a test mode created OutboundPayment to the returned status. The OutboundPayment must already be in the processing state. |
7 | */ |
8 | export async function main( |
9 | auth: Stripe, |
10 | id: string, |
11 | body: { |
12 | expand?: string[]; |
13 | returned_details?: { |
14 | code?: |
15 | | "account_closed" |
16 | | "account_frozen" |
17 | | "bank_account_restricted" |
18 | | "bank_ownership_changed" |
19 | | "declined" |
20 | | "incorrect_account_holder_name" |
21 | | "invalid_account_number" |
22 | | "invalid_currency" |
23 | | "no_account" |
24 | | "other"; |
25 | [k: string]: unknown; |
26 | }; |
27 | } |
28 | ) { |
29 | const url = new URL( |
30 | `https://api.stripe.com/v1/test_helpers/treasury/outbound_payments/${id}/return` |
31 | ); |
32 |
|
33 | const response = await fetch(url, { |
34 | method: "POST", |
35 | headers: { |
36 | "Content-Type": "application/x-www-form-urlencoded", |
37 | Authorization: "Bearer " + auth.token, |
38 | }, |
39 | body: encodeParams(body), |
40 | }); |
41 | if (!response.ok) { |
42 | const text = await response.text(); |
43 | throw new Error(`${response.status} ${text}`); |
44 | } |
45 | return await response.json(); |
46 | } |
47 |
|
48 | function encodeParams(o: any) { |
49 | function iter(o: any, path: string) { |
50 | if (Array.isArray(o)) { |
51 | o.forEach(function (a) { |
52 | iter(a, path + "[]"); |
53 | }); |
54 | return; |
55 | } |
56 | if (o !== null && typeof o === "object") { |
57 | Object.keys(o).forEach(function (k) { |
58 | iter(o[k], path + "[" + k + "]"); |
59 | }); |
60 | return; |
61 | } |
62 | data.push(path + "=" + o); |
63 | } |
64 | const data: string[] = []; |
65 | Object.keys(o).forEach(function (k) { |
66 | if (o[k] !== undefined) { |
67 | iter(o[k], k); |
68 | } |
69 | }); |
70 | return new URLSearchParams(data.join("&")); |
71 | } |
72 |
|