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