1 | type Stripe = { |
2 | token: string; |
3 | }; |
4 | |
5 | * Post transfers id reversals |
6 | * When you create a new reversal, you must specify a transfer to create it on. |
7 |
|
8 | When reversing transfers, you can optionally reverse part of the transfer. You can do so as many times as you wish until the entire transfer has been reversed. |
9 |
|
10 | Once entirely reversed, a transfer can’t be reversed again. This method will return an error when called on an already-reversed transfer, or when trying to reverse more money than is left on a transfer. |
11 | */ |
12 | export async function main( |
13 | auth: Stripe, |
14 | id: string, |
15 | body: { |
16 | amount?: number; |
17 | description?: string; |
18 | expand?: string[]; |
19 | metadata?: { [k: string]: string } | ""; |
20 | refund_application_fee?: boolean; |
21 | } |
22 | ) { |
23 | const url = new URL(`https://api.stripe.com/v1/transfers/${id}/reversals`); |
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 |
|