1 | type Stripe = { |
2 | token: string; |
3 | }; |
4 | |
5 | * Post tax rates tax rate |
6 | * Updates an existing tax rate. |
7 | */ |
8 | export async function main( |
9 | auth: Stripe, |
10 | tax_rate: string, |
11 | body: { |
12 | active?: boolean; |
13 | country?: string; |
14 | description?: string; |
15 | display_name?: string; |
16 | expand?: string[]; |
17 | jurisdiction?: string; |
18 | metadata?: { [k: string]: string } | ""; |
19 | state?: string; |
20 | tax_type?: |
21 | | "amusement_tax" |
22 | | "communications_tax" |
23 | | "gst" |
24 | | "hst" |
25 | | "igst" |
26 | | "jct" |
27 | | "lease_tax" |
28 | | "pst" |
29 | | "qst" |
30 | | "rst" |
31 | | "sales_tax" |
32 | | "vat"; |
33 | } |
34 | ) { |
35 | const url = new URL(`https://api.stripe.com/v1/tax_rates/${tax_rate}`); |
36 |
|
37 | const response = await fetch(url, { |
38 | method: "POST", |
39 | headers: { |
40 | "Content-Type": "application/x-www-form-urlencoded", |
41 | Authorization: "Bearer " + auth.token, |
42 | }, |
43 | body: encodeParams(body), |
44 | }); |
45 | if (!response.ok) { |
46 | const text = await response.text(); |
47 | throw new Error(`${response.status} ${text}`); |
48 | } |
49 | return await response.json(); |
50 | } |
51 |
|
52 | function encodeParams(o: any) { |
53 | function iter(o: any, path: string) { |
54 | if (Array.isArray(o)) { |
55 | o.forEach(function (a) { |
56 | iter(a, path + "[]"); |
57 | }); |
58 | return; |
59 | } |
60 | if (o !== null && typeof o === "object") { |
61 | Object.keys(o).forEach(function (k) { |
62 | iter(o[k], path + "[" + k + "]"); |
63 | }); |
64 | return; |
65 | } |
66 | data.push(path + "=" + o); |
67 | } |
68 | const data: string[] = []; |
69 | Object.keys(o).forEach(function (k) { |
70 | if (o[k] !== undefined) { |
71 | iter(o[k], k); |
72 | } |
73 | }); |
74 | return new URLSearchParams(data.join("&")); |
75 | } |
76 |
|