Post tax registrations id

Updates an existing Tax Registration object. A registration cannot be deleted after it has been created. If you wish to end a registration you may do so by setting expires_at.

Script stripe Verified

by hugo697 ยท 3/6/2024

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 368 days ago
1
type Stripe = {
2
  token: string;
3
};
4
/**
5
 * Post tax registrations id
6
 * Updates an existing Tax Registration object.
7

8
A registration cannot be deleted after it has been created. If you wish to end a registration you may do so by setting expires_at.
9
 */
10
export async function main(
11
  auth: Stripe,
12
  id: string,
13
  body: {
14
    active_from?: "now" | number;
15
    expand?: string[];
16
    expires_at?: "now" | number | "";
17
  }
18
) {
19
  const url = new URL(`https://api.stripe.com/v1/tax/registrations/${id}`);
20

21
  const response = await fetch(url, {
22
    method: "POST",
23
    headers: {
24
      "Content-Type": "application/x-www-form-urlencoded",
25
      Authorization: "Bearer " + auth.token,
26
    },
27
    body: encodeParams(body),
28
  });
29
  if (!response.ok) {
30
    const text = await response.text();
31
    throw new Error(`${response.status} ${text}`);
32
  }
33
  return await response.json();
34
}
35

36
function encodeParams(o: any) {
37
  function iter(o: any, path: string) {
38
    if (Array.isArray(o)) {
39
      o.forEach(function (a) {
40
        iter(a, path + "[]");
41
      });
42
      return;
43
    }
44
    if (o !== null && typeof o === "object") {
45
      Object.keys(o).forEach(function (k) {
46
        iter(o[k], path + "[" + k + "]");
47
      });
48
      return;
49
    }
50
    data.push(path + "=" + o);
51
  }
52
  const data: string[] = [];
53
  Object.keys(o).forEach(function (k) {
54
    if (o[k] !== undefined) {
55
      iter(o[k], k);
56
    }
57
  });
58
  return new URLSearchParams(data.join("&"));
59
}
60