1 | type Stripe = { |
2 | token: string; |
3 | }; |
4 | |
5 | * Post terminal readers reader process setup intent |
6 | * Initiates a setup intent flow on a Reader. |
7 | */ |
8 | export async function main( |
9 | auth: Stripe, |
10 | reader: string, |
11 | body: { |
12 | customer_consent_collected: boolean; |
13 | expand?: string[]; |
14 | process_config?: { |
15 | enable_customer_cancellation?: boolean; |
16 | [k: string]: unknown; |
17 | }; |
18 | setup_intent: string; |
19 | } |
20 | ) { |
21 | const url = new URL( |
22 | `https://api.stripe.com/v1/terminal/readers/${reader}/process_setup_intent` |
23 | ); |
24 |
|
25 | const response = await fetch(url, { |
26 | method: "POST", |
27 | headers: { |
28 | "Content-Type": "application/x-www-form-urlencoded", |
29 | Authorization: "Bearer " + auth.token, |
30 | }, |
31 | body: encodeParams(body), |
32 | }); |
33 | if (!response.ok) { |
34 | const text = await response.text(); |
35 | throw new Error(`${response.status} ${text}`); |
36 | } |
37 | return await response.json(); |
38 | } |
39 |
|
40 | function encodeParams(o: any) { |
41 | function iter(o: any, path: string) { |
42 | if (Array.isArray(o)) { |
43 | o.forEach(function (a) { |
44 | iter(a, path + "[]"); |
45 | }); |
46 | return; |
47 | } |
48 | if (o !== null && typeof o === "object") { |
49 | Object.keys(o).forEach(function (k) { |
50 | iter(o[k], path + "[" + k + "]"); |
51 | }); |
52 | return; |
53 | } |
54 | data.push(path + "=" + o); |
55 | } |
56 | const data: string[] = []; |
57 | Object.keys(o).forEach(function (k) { |
58 | if (o[k] !== undefined) { |
59 | iter(o[k], k); |
60 | } |
61 | }); |
62 | return new URLSearchParams(data.join("&")); |
63 | } |
64 |
|