1 | type Stripe = { |
2 | token: string; |
3 | }; |
4 | |
5 | * Post test helpers issuing authorizations authorization capture |
6 | * Capture a test-mode authorization. |
7 | */ |
8 | export async function main( |
9 | auth: Stripe, |
10 | authorization: string, |
11 | body: { |
12 | capture_amount?: number; |
13 | close_authorization?: boolean; |
14 | expand?: string[]; |
15 | purchase_details?: { |
16 | flight?: { |
17 | departure_at?: number; |
18 | passenger_name?: string; |
19 | refundable?: boolean; |
20 | segments?: { |
21 | arrival_airport_code?: string; |
22 | carrier?: string; |
23 | departure_airport_code?: string; |
24 | flight_number?: string; |
25 | service_class?: string; |
26 | stopover_allowed?: boolean; |
27 | [k: string]: unknown; |
28 | }[]; |
29 | travel_agency?: string; |
30 | [k: string]: unknown; |
31 | }; |
32 | fuel?: { |
33 | type?: |
34 | | "diesel" |
35 | | "other" |
36 | | "unleaded_plus" |
37 | | "unleaded_regular" |
38 | | "unleaded_super"; |
39 | unit?: "liter" | "us_gallon"; |
40 | unit_cost_decimal?: string; |
41 | volume_decimal?: string; |
42 | [k: string]: unknown; |
43 | }; |
44 | lodging?: { check_in_at?: number; nights?: number; [k: string]: unknown }; |
45 | receipt?: { |
46 | description?: string; |
47 | quantity?: string; |
48 | total?: number; |
49 | unit_cost?: number; |
50 | [k: string]: unknown; |
51 | }[]; |
52 | reference?: string; |
53 | [k: string]: unknown; |
54 | }; |
55 | } |
56 | ) { |
57 | const url = new URL( |
58 | `https://api.stripe.com/v1/test_helpers/issuing/authorizations/${authorization}/capture` |
59 | ); |
60 |
|
61 | const response = await fetch(url, { |
62 | method: "POST", |
63 | headers: { |
64 | "Content-Type": "application/x-www-form-urlencoded", |
65 | Authorization: "Bearer " + auth.token, |
66 | }, |
67 | body: encodeParams(body), |
68 | }); |
69 | if (!response.ok) { |
70 | const text = await response.text(); |
71 | throw new Error(`${response.status} ${text}`); |
72 | } |
73 | return await response.json(); |
74 | } |
75 |
|
76 | function encodeParams(o: any) { |
77 | function iter(o: any, path: string) { |
78 | if (Array.isArray(o)) { |
79 | o.forEach(function (a) { |
80 | iter(a, path + "[]"); |
81 | }); |
82 | return; |
83 | } |
84 | if (o !== null && typeof o === "object") { |
85 | Object.keys(o).forEach(function (k) { |
86 | iter(o[k], path + "[" + k + "]"); |
87 | }); |
88 | return; |
89 | } |
90 | data.push(path + "=" + o); |
91 | } |
92 | const data: string[] = []; |
93 | Object.keys(o).forEach(function (k) { |
94 | if (o[k] !== undefined) { |
95 | iter(o[k], k); |
96 | } |
97 | }); |
98 | return new URLSearchParams(data.join("&")); |
99 | } |
100 |
|