Post tax ids

Creates a new account or customer tax_id object.

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 ids
6
 * Creates a new account or customer tax_id object.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  body: {
11
    expand?: string[];
12
    owner?: {
13
      account?: string;
14
      customer?: string;
15
      type: "account" | "application" | "customer" | "self";
16
      [k: string]: unknown;
17
    };
18
    type:
19
      | "ad_nrt"
20
      | "ae_trn"
21
      | "ar_cuit"
22
      | "au_abn"
23
      | "au_arn"
24
      | "bg_uic"
25
      | "bo_tin"
26
      | "br_cnpj"
27
      | "br_cpf"
28
      | "ca_bn"
29
      | "ca_gst_hst"
30
      | "ca_pst_bc"
31
      | "ca_pst_mb"
32
      | "ca_pst_sk"
33
      | "ca_qst"
34
      | "ch_vat"
35
      | "cl_tin"
36
      | "cn_tin"
37
      | "co_nit"
38
      | "cr_tin"
39
      | "do_rcn"
40
      | "ec_ruc"
41
      | "eg_tin"
42
      | "es_cif"
43
      | "eu_oss_vat"
44
      | "eu_vat"
45
      | "gb_vat"
46
      | "ge_vat"
47
      | "hk_br"
48
      | "hu_tin"
49
      | "id_npwp"
50
      | "il_vat"
51
      | "in_gst"
52
      | "is_vat"
53
      | "jp_cn"
54
      | "jp_rn"
55
      | "jp_trn"
56
      | "ke_pin"
57
      | "kr_brn"
58
      | "li_uid"
59
      | "mx_rfc"
60
      | "my_frp"
61
      | "my_itn"
62
      | "my_sst"
63
      | "no_vat"
64
      | "no_voec"
65
      | "nz_gst"
66
      | "pe_ruc"
67
      | "ph_tin"
68
      | "ro_tin"
69
      | "rs_pib"
70
      | "ru_inn"
71
      | "ru_kpp"
72
      | "sa_vat"
73
      | "sg_gst"
74
      | "sg_uen"
75
      | "si_tin"
76
      | "sv_nit"
77
      | "th_vat"
78
      | "tr_tin"
79
      | "tw_vat"
80
      | "ua_vat"
81
      | "us_ein"
82
      | "uy_ruc"
83
      | "ve_rif"
84
      | "vn_tin"
85
      | "za_vat";
86
    value: string;
87
  }
88
) {
89
  const url = new URL(`https://api.stripe.com/v1/tax_ids`);
90

91
  const response = await fetch(url, {
92
    method: "POST",
93
    headers: {
94
      "Content-Type": "application/x-www-form-urlencoded",
95
      Authorization: "Bearer " + auth.token,
96
    },
97
    body: encodeParams(body),
98
  });
99
  if (!response.ok) {
100
    const text = await response.text();
101
    throw new Error(`${response.status} ${text}`);
102
  }
103
  return await response.json();
104
}
105

106
function encodeParams(o: any) {
107
  function iter(o: any, path: string) {
108
    if (Array.isArray(o)) {
109
      o.forEach(function (a) {
110
        iter(a, path + "[]");
111
      });
112
      return;
113
    }
114
    if (o !== null && typeof o === "object") {
115
      Object.keys(o).forEach(function (k) {
116
        iter(o[k], path + "[" + k + "]");
117
      });
118
      return;
119
    }
120
    data.push(path + "=" + o);
121
  }
122
  const data: string[] = [];
123
  Object.keys(o).forEach(function (k) {
124
    if (o[k] !== undefined) {
125
      iter(o[k], k);
126
    }
127
  });
128
  return new URLSearchParams(data.join("&"));
129
}
130