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