Post customers customer tax ids

Creates a new tax_id object for a customer.

Script stripe Verified

by hugo697 ยท 10/30/2023

The script

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

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

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