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