Post tax calculations

Calculates tax based on input and returns a Tax Calculation object.

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 tax calculations
6
 * Calculates tax based on input and returns a Tax Calculation object.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  body: {
11
    currency: string;
12
    customer?: string;
13
    customer_details?: {
14
      address?: {
15
        city?: string | "";
16
        country: string;
17
        line1?: string | "";
18
        line2?: string | "";
19
        postal_code?: string | "";
20
        state?: string | "";
21
        [k: string]: unknown;
22
      };
23
      address_source?: "billing" | "shipping";
24
      ip_address?: string;
25
      tax_ids?: {
26
        type:
27
          | "ad_nrt"
28
          | "ae_trn"
29
          | "ar_cuit"
30
          | "au_abn"
31
          | "au_arn"
32
          | "bg_uic"
33
          | "bo_tin"
34
          | "br_cnpj"
35
          | "br_cpf"
36
          | "ca_bn"
37
          | "ca_gst_hst"
38
          | "ca_pst_bc"
39
          | "ca_pst_mb"
40
          | "ca_pst_sk"
41
          | "ca_qst"
42
          | "ch_vat"
43
          | "cl_tin"
44
          | "cn_tin"
45
          | "co_nit"
46
          | "cr_tin"
47
          | "do_rcn"
48
          | "ec_ruc"
49
          | "eg_tin"
50
          | "es_cif"
51
          | "eu_oss_vat"
52
          | "eu_vat"
53
          | "gb_vat"
54
          | "ge_vat"
55
          | "hk_br"
56
          | "hu_tin"
57
          | "id_npwp"
58
          | "il_vat"
59
          | "in_gst"
60
          | "is_vat"
61
          | "jp_cn"
62
          | "jp_rn"
63
          | "jp_trn"
64
          | "ke_pin"
65
          | "kr_brn"
66
          | "li_uid"
67
          | "mx_rfc"
68
          | "my_frp"
69
          | "my_itn"
70
          | "my_sst"
71
          | "no_vat"
72
          | "no_voec"
73
          | "nz_gst"
74
          | "pe_ruc"
75
          | "ph_tin"
76
          | "ro_tin"
77
          | "rs_pib"
78
          | "ru_inn"
79
          | "ru_kpp"
80
          | "sa_vat"
81
          | "sg_gst"
82
          | "sg_uen"
83
          | "si_tin"
84
          | "sv_nit"
85
          | "th_vat"
86
          | "tr_tin"
87
          | "tw_vat"
88
          | "ua_vat"
89
          | "us_ein"
90
          | "uy_ruc"
91
          | "ve_rif"
92
          | "vn_tin"
93
          | "za_vat";
94
        value: string;
95
        [k: string]: unknown;
96
      }[];
97
      taxability_override?: "customer_exempt" | "none" | "reverse_charge";
98
      [k: string]: unknown;
99
    };
100
    expand?: string[];
101
    line_items: {
102
      amount: number;
103
      product?: string;
104
      quantity?: number;
105
      reference?: string;
106
      tax_behavior?: "exclusive" | "inclusive";
107
      tax_code?: string;
108
      [k: string]: unknown;
109
    }[];
110
    shipping_cost?: {
111
      amount?: number;
112
      shipping_rate?: string;
113
      tax_behavior?: "exclusive" | "inclusive";
114
      tax_code?: string;
115
      [k: string]: unknown;
116
    };
117
    tax_date?: number;
118
  }
119
) {
120
  const url = new URL(`https://api.stripe.com/v1/tax/calculations`);
121

122
  const response = await fetch(url, {
123
    method: "POST",
124
    headers: {
125
      "Content-Type": "application/x-www-form-urlencoded",
126
      Authorization: "Bearer " + auth.token,
127
    },
128
    body: encodeParams(body),
129
  });
130
  if (!response.ok) {
131
    const text = await response.text();
132
    throw new Error(`${response.status} ${text}`);
133
  }
134
  return await response.json();
135
}
136

137
function encodeParams(o: any) {
138
  function iter(o: any, path: string) {
139
    if (Array.isArray(o)) {
140
      o.forEach(function (a) {
141
        iter(a, path + "[]");
142
      });
143
      return;
144
    }
145
    if (o !== null && typeof o === "object") {
146
      Object.keys(o).forEach(function (k) {
147
        iter(o[k], path + "[" + k + "]");
148
      });
149
      return;
150
    }
151
    data.push(path + "=" + o);
152
  }
153
  const data: string[] = [];
154
  Object.keys(o).forEach(function (k) {
155
    if (o[k] !== undefined) {
156
      iter(o[k], k);
157
    }
158
  });
159
  return new URLSearchParams(data.join("&"));
160
}
161