1 | type Stripe = { |
2 | token: string; |
3 | }; |
4 | |
5 | * Post charges charge |
6 | * Updates the specified charge by setting the values of the parameters passed. Any parameters not provided will be left unchanged. |
7 | */ |
8 | export async function main( |
9 | auth: Stripe, |
10 | charge: string, |
11 | body: { |
12 | customer?: string; |
13 | description?: string; |
14 | expand?: string[]; |
15 | fraud_details?: { |
16 | user_report: "" | "fraudulent" | "safe"; |
17 | [k: string]: unknown; |
18 | }; |
19 | metadata?: { [k: string]: string } | ""; |
20 | receipt_email?: string; |
21 | shipping?: { |
22 | address: { |
23 | city?: string; |
24 | country?: string; |
25 | line1?: string; |
26 | line2?: string; |
27 | postal_code?: string; |
28 | state?: string; |
29 | [k: string]: unknown; |
30 | }; |
31 | carrier?: string; |
32 | name: string; |
33 | phone?: string; |
34 | tracking_number?: string; |
35 | [k: string]: unknown; |
36 | }; |
37 | transfer_group?: string; |
38 | } |
39 | ) { |
40 | const url = new URL(`https://api.stripe.com/v1/charges/${charge}`); |
41 |
|
42 | const response = await fetch(url, { |
43 | method: "POST", |
44 | headers: { |
45 | "Content-Type": "application/x-www-form-urlencoded", |
46 | Authorization: "Bearer " + auth.token, |
47 | }, |
48 | body: encodeParams(body), |
49 | }); |
50 | if (!response.ok) { |
51 | const text = await response.text(); |
52 | throw new Error(`${response.status} ${text}`); |
53 | } |
54 | return await response.json(); |
55 | } |
56 |
|
57 | function encodeParams(o: any) { |
58 | function iter(o: any, path: string) { |
59 | if (Array.isArray(o)) { |
60 | o.forEach(function (a) { |
61 | iter(a, path + "[]"); |
62 | }); |
63 | return; |
64 | } |
65 | if (o !== null && typeof o === "object") { |
66 | Object.keys(o).forEach(function (k) { |
67 | iter(o[k], path + "[" + k + "]"); |
68 | }); |
69 | return; |
70 | } |
71 | data.push(path + "=" + o); |
72 | } |
73 | const data: string[] = []; |
74 | Object.keys(o).forEach(function (k) { |
75 | if (o[k] !== undefined) { |
76 | iter(o[k], k); |
77 | } |
78 | }); |
79 | return new URLSearchParams(data.join("&")); |
80 | } |
81 |
|