Post terminal configurations

Creates a new Configuration 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 terminal configurations
6
 * Creates a new Configuration object.
7
 */
8
export async function main(
9
  auth: Stripe,
10
  body: {
11
    bbpos_wisepos_e?: { splashscreen?: string | ""; [k: string]: unknown };
12
    expand?: string[];
13
    offline?: { enabled: boolean; [k: string]: unknown } | "";
14
    tipping?:
15
      | {
16
          aud?: {
17
            fixed_amounts?: number[];
18
            percentages?: number[];
19
            smart_tip_threshold?: number;
20
            [k: string]: unknown;
21
          };
22
          cad?: {
23
            fixed_amounts?: number[];
24
            percentages?: number[];
25
            smart_tip_threshold?: number;
26
            [k: string]: unknown;
27
          };
28
          chf?: {
29
            fixed_amounts?: number[];
30
            percentages?: number[];
31
            smart_tip_threshold?: number;
32
            [k: string]: unknown;
33
          };
34
          czk?: {
35
            fixed_amounts?: number[];
36
            percentages?: number[];
37
            smart_tip_threshold?: number;
38
            [k: string]: unknown;
39
          };
40
          dkk?: {
41
            fixed_amounts?: number[];
42
            percentages?: number[];
43
            smart_tip_threshold?: number;
44
            [k: string]: unknown;
45
          };
46
          eur?: {
47
            fixed_amounts?: number[];
48
            percentages?: number[];
49
            smart_tip_threshold?: number;
50
            [k: string]: unknown;
51
          };
52
          gbp?: {
53
            fixed_amounts?: number[];
54
            percentages?: number[];
55
            smart_tip_threshold?: number;
56
            [k: string]: unknown;
57
          };
58
          hkd?: {
59
            fixed_amounts?: number[];
60
            percentages?: number[];
61
            smart_tip_threshold?: number;
62
            [k: string]: unknown;
63
          };
64
          myr?: {
65
            fixed_amounts?: number[];
66
            percentages?: number[];
67
            smart_tip_threshold?: number;
68
            [k: string]: unknown;
69
          };
70
          nok?: {
71
            fixed_amounts?: number[];
72
            percentages?: number[];
73
            smart_tip_threshold?: number;
74
            [k: string]: unknown;
75
          };
76
          nzd?: {
77
            fixed_amounts?: number[];
78
            percentages?: number[];
79
            smart_tip_threshold?: number;
80
            [k: string]: unknown;
81
          };
82
          sek?: {
83
            fixed_amounts?: number[];
84
            percentages?: number[];
85
            smart_tip_threshold?: number;
86
            [k: string]: unknown;
87
          };
88
          sgd?: {
89
            fixed_amounts?: number[];
90
            percentages?: number[];
91
            smart_tip_threshold?: number;
92
            [k: string]: unknown;
93
          };
94
          usd?: {
95
            fixed_amounts?: number[];
96
            percentages?: number[];
97
            smart_tip_threshold?: number;
98
            [k: string]: unknown;
99
          };
100
          [k: string]: unknown;
101
        }
102
      | "";
103
    verifone_p400?: { splashscreen?: string | ""; [k: string]: unknown };
104
  }
105
) {
106
  const url = new URL(`https://api.stripe.com/v1/terminal/configurations`);
107

108
  const response = await fetch(url, {
109
    method: "POST",
110
    headers: {
111
      "Content-Type": "application/x-www-form-urlencoded",
112
      Authorization: "Bearer " + auth.token,
113
    },
114
    body: encodeParams(body),
115
  });
116
  if (!response.ok) {
117
    const text = await response.text();
118
    throw new Error(`${response.status} ${text}`);
119
  }
120
  return await response.json();
121
}
122

123
function encodeParams(o: any) {
124
  function iter(o: any, path: string) {
125
    if (Array.isArray(o)) {
126
      o.forEach(function (a) {
127
        iter(a, path + "[]");
128
      });
129
      return;
130
    }
131
    if (o !== null && typeof o === "object") {
132
      Object.keys(o).forEach(function (k) {
133
        iter(o[k], path + "[" + k + "]");
134
      });
135
      return;
136
    }
137
    data.push(path + "=" + o);
138
  }
139
  const data: string[] = [];
140
  Object.keys(o).forEach(function (k) {
141
    if (o[k] !== undefined) {
142
      iter(o[k], k);
143
    }
144
  });
145
  return new URLSearchParams(data.join("&"));
146
}
147