1 | type Stripe = { |
2 | token: string; |
3 | }; |
4 | |
5 | * Post tax registrations |
6 | * Creates a new Tax Registration object. |
7 | */ |
8 | export async function main( |
9 | auth: Stripe, |
10 | body: { |
11 | active_from: "now" | number; |
12 | country: string; |
13 | country_options: { |
14 | ae?: { type: "standard"; [k: string]: unknown }; |
15 | at?: { |
16 | standard?: { |
17 | place_of_supply_scheme: "small_seller" | "standard"; |
18 | [k: string]: unknown; |
19 | }; |
20 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
21 | [k: string]: unknown; |
22 | }; |
23 | au?: { type: "standard"; [k: string]: unknown }; |
24 | be?: { |
25 | standard?: { |
26 | place_of_supply_scheme: "small_seller" | "standard"; |
27 | [k: string]: unknown; |
28 | }; |
29 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
30 | [k: string]: unknown; |
31 | }; |
32 | bg?: { |
33 | standard?: { |
34 | place_of_supply_scheme: "small_seller" | "standard"; |
35 | [k: string]: unknown; |
36 | }; |
37 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
38 | [k: string]: unknown; |
39 | }; |
40 | ca?: { |
41 | province_standard?: { province: string; [k: string]: unknown }; |
42 | type: "province_standard" | "simplified" | "standard"; |
43 | [k: string]: unknown; |
44 | }; |
45 | ch?: { type: "standard"; [k: string]: unknown }; |
46 | cl?: { type: "simplified"; [k: string]: unknown }; |
47 | co?: { type: "simplified"; [k: string]: unknown }; |
48 | cy?: { |
49 | standard?: { |
50 | place_of_supply_scheme: "small_seller" | "standard"; |
51 | [k: string]: unknown; |
52 | }; |
53 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
54 | [k: string]: unknown; |
55 | }; |
56 | cz?: { |
57 | standard?: { |
58 | place_of_supply_scheme: "small_seller" | "standard"; |
59 | [k: string]: unknown; |
60 | }; |
61 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
62 | [k: string]: unknown; |
63 | }; |
64 | de?: { |
65 | standard?: { |
66 | place_of_supply_scheme: "small_seller" | "standard"; |
67 | [k: string]: unknown; |
68 | }; |
69 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
70 | [k: string]: unknown; |
71 | }; |
72 | dk?: { |
73 | standard?: { |
74 | place_of_supply_scheme: "small_seller" | "standard"; |
75 | [k: string]: unknown; |
76 | }; |
77 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
78 | [k: string]: unknown; |
79 | }; |
80 | ee?: { |
81 | standard?: { |
82 | place_of_supply_scheme: "small_seller" | "standard"; |
83 | [k: string]: unknown; |
84 | }; |
85 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
86 | [k: string]: unknown; |
87 | }; |
88 | es?: { |
89 | standard?: { |
90 | place_of_supply_scheme: "small_seller" | "standard"; |
91 | [k: string]: unknown; |
92 | }; |
93 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
94 | [k: string]: unknown; |
95 | }; |
96 | fi?: { |
97 | standard?: { |
98 | place_of_supply_scheme: "small_seller" | "standard"; |
99 | [k: string]: unknown; |
100 | }; |
101 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
102 | [k: string]: unknown; |
103 | }; |
104 | fr?: { |
105 | standard?: { |
106 | place_of_supply_scheme: "small_seller" | "standard"; |
107 | [k: string]: unknown; |
108 | }; |
109 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
110 | [k: string]: unknown; |
111 | }; |
112 | gb?: { type: "standard"; [k: string]: unknown }; |
113 | gr?: { |
114 | standard?: { |
115 | place_of_supply_scheme: "small_seller" | "standard"; |
116 | [k: string]: unknown; |
117 | }; |
118 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
119 | [k: string]: unknown; |
120 | }; |
121 | hr?: { |
122 | standard?: { |
123 | place_of_supply_scheme: "small_seller" | "standard"; |
124 | [k: string]: unknown; |
125 | }; |
126 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
127 | [k: string]: unknown; |
128 | }; |
129 | hu?: { |
130 | standard?: { |
131 | place_of_supply_scheme: "small_seller" | "standard"; |
132 | [k: string]: unknown; |
133 | }; |
134 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
135 | [k: string]: unknown; |
136 | }; |
137 | id?: { type: "simplified"; [k: string]: unknown }; |
138 | ie?: { |
139 | standard?: { |
140 | place_of_supply_scheme: "small_seller" | "standard"; |
141 | [k: string]: unknown; |
142 | }; |
143 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
144 | [k: string]: unknown; |
145 | }; |
146 | is?: { type: "standard"; [k: string]: unknown }; |
147 | it?: { |
148 | standard?: { |
149 | place_of_supply_scheme: "small_seller" | "standard"; |
150 | [k: string]: unknown; |
151 | }; |
152 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
153 | [k: string]: unknown; |
154 | }; |
155 | jp?: { type: "standard"; [k: string]: unknown }; |
156 | kr?: { type: "simplified"; [k: string]: unknown }; |
157 | lt?: { |
158 | standard?: { |
159 | place_of_supply_scheme: "small_seller" | "standard"; |
160 | [k: string]: unknown; |
161 | }; |
162 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
163 | [k: string]: unknown; |
164 | }; |
165 | lu?: { |
166 | standard?: { |
167 | place_of_supply_scheme: "small_seller" | "standard"; |
168 | [k: string]: unknown; |
169 | }; |
170 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
171 | [k: string]: unknown; |
172 | }; |
173 | lv?: { |
174 | standard?: { |
175 | place_of_supply_scheme: "small_seller" | "standard"; |
176 | [k: string]: unknown; |
177 | }; |
178 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
179 | [k: string]: unknown; |
180 | }; |
181 | mt?: { |
182 | standard?: { |
183 | place_of_supply_scheme: "small_seller" | "standard"; |
184 | [k: string]: unknown; |
185 | }; |
186 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
187 | [k: string]: unknown; |
188 | }; |
189 | mx?: { type: "simplified"; [k: string]: unknown }; |
190 | my?: { type: "simplified"; [k: string]: unknown }; |
191 | nl?: { |
192 | standard?: { |
193 | place_of_supply_scheme: "small_seller" | "standard"; |
194 | [k: string]: unknown; |
195 | }; |
196 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
197 | [k: string]: unknown; |
198 | }; |
199 | no?: { type: "standard"; [k: string]: unknown }; |
200 | nz?: { type: "standard"; [k: string]: unknown }; |
201 | pl?: { |
202 | standard?: { |
203 | place_of_supply_scheme: "small_seller" | "standard"; |
204 | [k: string]: unknown; |
205 | }; |
206 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
207 | [k: string]: unknown; |
208 | }; |
209 | pt?: { |
210 | standard?: { |
211 | place_of_supply_scheme: "small_seller" | "standard"; |
212 | [k: string]: unknown; |
213 | }; |
214 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
215 | [k: string]: unknown; |
216 | }; |
217 | ro?: { |
218 | standard?: { |
219 | place_of_supply_scheme: "small_seller" | "standard"; |
220 | [k: string]: unknown; |
221 | }; |
222 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
223 | [k: string]: unknown; |
224 | }; |
225 | sa?: { type: "simplified"; [k: string]: unknown }; |
226 | se?: { |
227 | standard?: { |
228 | place_of_supply_scheme: "small_seller" | "standard"; |
229 | [k: string]: unknown; |
230 | }; |
231 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
232 | [k: string]: unknown; |
233 | }; |
234 | sg?: { type: "standard"; [k: string]: unknown }; |
235 | si?: { |
236 | standard?: { |
237 | place_of_supply_scheme: "small_seller" | "standard"; |
238 | [k: string]: unknown; |
239 | }; |
240 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
241 | [k: string]: unknown; |
242 | }; |
243 | sk?: { |
244 | standard?: { |
245 | place_of_supply_scheme: "small_seller" | "standard"; |
246 | [k: string]: unknown; |
247 | }; |
248 | type: "ioss" | "oss_non_union" | "oss_union" | "standard"; |
249 | [k: string]: unknown; |
250 | }; |
251 | th?: { type: "simplified"; [k: string]: unknown }; |
252 | tr?: { type: "simplified"; [k: string]: unknown }; |
253 | us?: { |
254 | local_amusement_tax?: { jurisdiction: string; [k: string]: unknown }; |
255 | local_lease_tax?: { jurisdiction: string; [k: string]: unknown }; |
256 | state: string; |
257 | type: |
258 | | "local_amusement_tax" |
259 | | "local_lease_tax" |
260 | | "state_communications_tax" |
261 | | "state_sales_tax"; |
262 | [k: string]: unknown; |
263 | }; |
264 | vn?: { type: "simplified"; [k: string]: unknown }; |
265 | za?: { type: "standard"; [k: string]: unknown }; |
266 | [k: string]: unknown; |
267 | }; |
268 | expand?: string[]; |
269 | expires_at?: number; |
270 | } |
271 | ) { |
272 | const url = new URL(`https://api.stripe.com/v1/tax/registrations`); |
273 |
|
274 | const response = await fetch(url, { |
275 | method: "POST", |
276 | headers: { |
277 | "Content-Type": "application/x-www-form-urlencoded", |
278 | Authorization: "Bearer " + auth.token, |
279 | }, |
280 | body: encodeParams(body), |
281 | }); |
282 | if (!response.ok) { |
283 | const text = await response.text(); |
284 | throw new Error(`${response.status} ${text}`); |
285 | } |
286 | return await response.json(); |
287 | } |
288 |
|
289 | function encodeParams(o: any) { |
290 | function iter(o: any, path: string) { |
291 | if (Array.isArray(o)) { |
292 | o.forEach(function (a) { |
293 | iter(a, path + "[]"); |
294 | }); |
295 | return; |
296 | } |
297 | if (o !== null && typeof o === "object") { |
298 | Object.keys(o).forEach(function (k) { |
299 | iter(o[k], path + "[" + k + "]"); |
300 | }); |
301 | return; |
302 | } |
303 | data.push(path + "=" + o); |
304 | } |
305 | const data: string[] = []; |
306 | Object.keys(o).forEach(function (k) { |
307 | if (o[k] !== undefined) { |
308 | iter(o[k], k); |
309 | } |
310 | }); |
311 | return new URLSearchParams(data.join("&")); |
312 | } |
313 |
|