Post terminal configurations configuration

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

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

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