@zonos/elements 1.0.16 → 1.0.17-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/{cart-e27fd17c.js → cart-108bc407.js} +8 -1
- package/dist/cjs/cart-108bc407.js.map +1 -0
- package/dist/cjs/zonos-address_2.cjs.entry.js +1 -1
- package/dist/cjs/zonos-banner_2.cjs.entry.js +1 -1
- package/dist/cjs/zonos-cart-items_10.cjs.entry.js +5 -3
- package/dist/cjs/zonos-cart-items_10.cjs.entry.js.map +1 -1
- package/dist/cjs/zonos-checkout.cjs.entry.js +6 -2
- package/dist/cjs/zonos-checkout.cjs.entry.js.map +1 -1
- package/dist/cjs/zonos-controller.cjs.entry.js +1 -1
- package/dist/collection/components/checkout/zonos-checkout/zonos-cart-items/zonos-cart-items.js +3 -1
- package/dist/collection/components/checkout/zonos-checkout/zonos-cart-items/zonos-cart-items.js.map +1 -1
- package/dist/collection/components/checkout/zonos-checkout/zonos-checkout-finish/zonos-checkout-finish.js +1 -1
- package/dist/collection/components/checkout/zonos-checkout/zonos-checkout-finish/zonos-checkout-finish.js.map +1 -1
- package/dist/collection/components/checkout/zonos-checkout/zonos-checkout.js +5 -1
- package/dist/collection/components/checkout/zonos-checkout/zonos-checkout.js.map +1 -1
- package/dist/collection/components/store/checkout/cart.js +6 -0
- package/dist/collection/components/store/checkout/cart.js.map +1 -1
- package/dist/collection/components/store/checkout/stripe.js +2 -0
- package/dist/collection/components/store/checkout/stripe.js.map +1 -1
- package/dist/collection/environment.js +9 -1
- package/dist/collection/environment.js.map +1 -1
- package/dist/collection/scripts/loadStripe.js +3 -3
- package/dist/collection/scripts/loadStripe.js.map +1 -1
- package/dist/collection/scripts/stripe.js +6 -6
- package/dist/collection/scripts/stripe.js.map +1 -1
- package/dist/collection/scripts/zonos.js +2 -2
- package/dist/collection/scripts/zonos.js.map +1 -1
- package/dist/collection/types/generated/graphql.customer.types.js +5 -0
- package/dist/collection/types/generated/graphql.customer.types.js.map +1 -1
- package/dist/components/stripe.js +8 -1
- package/dist/components/stripe.js.map +1 -1
- package/dist/components/zonos-cart-items2.js +3 -1
- package/dist/components/zonos-cart-items2.js.map +1 -1
- package/dist/components/zonos-checkout-finish2.js +1 -1
- package/dist/components/zonos-checkout-finish2.js.map +1 -1
- package/dist/components/zonos-checkout.js +5 -1
- package/dist/components/zonos-checkout.js.map +1 -1
- package/dist/esm/{cart-36c33342.js → cart-2f83e048.js} +8 -1
- package/dist/esm/cart-2f83e048.js.map +1 -0
- package/dist/esm/zonos-address_2.entry.js +1 -1
- package/dist/esm/zonos-banner_2.entry.js +1 -1
- package/dist/esm/zonos-cart-items_10.entry.js +5 -3
- package/dist/esm/zonos-cart-items_10.entry.js.map +1 -1
- package/dist/esm/zonos-checkout.entry.js +6 -2
- package/dist/esm/zonos-checkout.entry.js.map +1 -1
- package/dist/esm/zonos-controller.entry.js +1 -1
- package/dist/esm-es5/cart-2f83e048.js +2 -0
- package/dist/esm-es5/cart-2f83e048.js.map +1 -0
- package/dist/esm-es5/zonos-address_2.entry.js +1 -1
- package/dist/esm-es5/zonos-banner_2.entry.js +1 -1
- package/dist/esm-es5/zonos-cart-items_10.entry.js +1 -1
- package/dist/esm-es5/zonos-cart-items_10.entry.js.map +1 -1
- package/dist/esm-es5/zonos-checkout.entry.js +1 -1
- package/dist/esm-es5/zonos-checkout.entry.js.map +1 -1
- package/dist/esm-es5/zonos-controller.entry.js +1 -1
- package/dist/scripts/loadStripe.js +118 -0
- package/dist/scripts/loadZonos.js +300 -0
- package/dist/scripts/stripe.js +126 -0
- package/dist/scripts/zonos.js +300 -0
- package/dist/types/components/store/checkout/cart.d.ts +14 -0
- package/dist/types/components/store/checkout/stripe.d.ts +1 -1
- package/dist/types/scripts/loadStripe.d.ts +1 -1
- package/dist/types/scripts/stripe.d.ts +2 -2
- package/dist/types/types/generated/graphql.customer.types.d.ts +48 -0
- package/dist/types/types/generated/graphql.customer.types.ts +50 -0
- package/dist/zonos-elements/{cart-36c33342.js → cart-2f83e048.js} +8 -1
- package/dist/zonos-elements/cart-2f83e048.js.map +1 -0
- package/dist/zonos-elements/cart-9780fa28.system.js +2 -0
- package/dist/zonos-elements/cart-9780fa28.system.js.map +1 -0
- package/dist/zonos-elements/zonos-address_2.entry.js +1 -1
- package/dist/zonos-elements/zonos-address_2.system.entry.js +1 -1
- package/dist/zonos-elements/zonos-banner_2.entry.js +1 -1
- package/dist/zonos-elements/zonos-banner_2.system.entry.js +1 -1
- package/dist/zonos-elements/zonos-cart-items_10.entry.js +5 -3
- package/dist/zonos-elements/zonos-cart-items_10.entry.js.map +1 -1
- package/dist/zonos-elements/zonos-cart-items_10.system.entry.js +1 -1
- package/dist/zonos-elements/zonos-cart-items_10.system.entry.js.map +1 -1
- package/dist/zonos-elements/zonos-checkout.entry.js +6 -2
- package/dist/zonos-elements/zonos-checkout.entry.js.map +1 -1
- package/dist/zonos-elements/zonos-checkout.system.entry.js +1 -1
- package/dist/zonos-elements/zonos-checkout.system.entry.js.map +1 -1
- package/dist/zonos-elements/zonos-controller.entry.js +1 -1
- package/dist/zonos-elements/zonos-controller.system.entry.js +1 -1
- package/package.json +2 -2
- package/dist/cjs/cart-e27fd17c.js.map +0 -1
- package/dist/esm/cart-36c33342.js.map +0 -1
- package/dist/esm-es5/cart-36c33342.js +0 -2
- package/dist/esm-es5/cart-36c33342.js.map +0 -1
- package/dist/zonos-elements/cart-36c33342.js.map +0 -1
- package/dist/zonos-elements/cart-ecd9011b.system.js +0 -2
- package/dist/zonos-elements/cart-ecd9011b.system.js.map +0 -1
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const log = (message, type, ...additionalInfo) => {
|
|
4
|
+
// if type is error and debug is false, do not log
|
|
5
|
+
if (!window.Zonos.debug) {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
switch (type) {
|
|
9
|
+
case 'warn':
|
|
10
|
+
// eslint-disable-next-line no-console
|
|
11
|
+
console.warn(message, ...additionalInfo);
|
|
12
|
+
break;
|
|
13
|
+
case 'info':
|
|
14
|
+
// eslint-disable-next-line no-console
|
|
15
|
+
console.info(message, ...additionalInfo);
|
|
16
|
+
break;
|
|
17
|
+
default:
|
|
18
|
+
// eslint-disable-next-line no-console
|
|
19
|
+
console.groupCollapsed(message, ...additionalInfo);
|
|
20
|
+
// eslint-disable-next-line no-console
|
|
21
|
+
console.trace();
|
|
22
|
+
// eslint-disable-next-line no-console
|
|
23
|
+
console.groupEnd();
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const logError = (message, ...rest) => {
|
|
29
|
+
// eslint-disable-next-line no-console
|
|
30
|
+
console.error(message, ...rest);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/* eslint-disable no-console */
|
|
34
|
+
/** Loading stripe script extracted from stripe.js repository */
|
|
35
|
+
const V3_URL = 'https://js.stripe.com/v3';
|
|
36
|
+
const V3_URL_REGEX = /^https:\/\/js\.stripe\.com\/v3\/?(\?.*)?$/;
|
|
37
|
+
const EXISTING_SCRIPT_MESSAGE = 'loadStripe.setLoadParameters was called but an existing Stripe.js script already exists in the document; existing script parameters will be used';
|
|
38
|
+
const zonosStripeFindScript = () => {
|
|
39
|
+
const scripts = document.querySelectorAll(`script[src^="${V3_URL}"]`);
|
|
40
|
+
for (let i = 0; i < scripts.length; i++) {
|
|
41
|
+
const script = scripts[i];
|
|
42
|
+
if (!script) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (!V3_URL_REGEX.test(script.src)) {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
return script;
|
|
49
|
+
}
|
|
50
|
+
return null;
|
|
51
|
+
};
|
|
52
|
+
let stripePromise$1 = null;
|
|
53
|
+
const injectScript = (params) => {
|
|
54
|
+
const queryString = params && !params.advancedFraudSignals ? '?advancedFraudSignals=false' : '';
|
|
55
|
+
const script = document.createElement('script');
|
|
56
|
+
script.src = `${V3_URL}${queryString}`;
|
|
57
|
+
const headBody = document.head || document.body;
|
|
58
|
+
if (!headBody) {
|
|
59
|
+
logError('Expected document.body not to be null. Stripe.js requires a <body> element.');
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
headBody.appendChild(script);
|
|
63
|
+
return script;
|
|
64
|
+
};
|
|
65
|
+
const zonosStripeLoadScript = (params) => {
|
|
66
|
+
// Ensure that we only attempt to load Stripe.js at most once
|
|
67
|
+
if (stripePromise$1 !== null) {
|
|
68
|
+
return stripePromise$1;
|
|
69
|
+
}
|
|
70
|
+
stripePromise$1 = new Promise((resolve, reject) => {
|
|
71
|
+
if (typeof window === 'undefined' || typeof document === 'undefined') {
|
|
72
|
+
// Resolve to null when imported server side. This makes the module
|
|
73
|
+
// safe to import in an isomorphic code base.
|
|
74
|
+
resolve(null);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (window.Stripe && params) {
|
|
78
|
+
console.warn(EXISTING_SCRIPT_MESSAGE);
|
|
79
|
+
}
|
|
80
|
+
if (window.Stripe) {
|
|
81
|
+
resolve(window.Stripe);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
let script = zonosStripeFindScript();
|
|
86
|
+
if (script && params) {
|
|
87
|
+
console.warn(EXISTING_SCRIPT_MESSAGE);
|
|
88
|
+
}
|
|
89
|
+
else if (!script) {
|
|
90
|
+
script = injectScript(params);
|
|
91
|
+
}
|
|
92
|
+
script === null || script === void 0 ? void 0 : script.addEventListener('load', () => {
|
|
93
|
+
if (window.Stripe) {
|
|
94
|
+
resolve(window.Stripe);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
reject(new Error('Stripe.js not available'));
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
script === null || script === void 0 ? void 0 : script.addEventListener('error', () => {
|
|
101
|
+
reject(new Error('Failed to load Stripe.js'));
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
reject(error);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
return stripePromise$1;
|
|
110
|
+
};
|
|
111
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
112
|
+
const registerWrapper = (stripe, startTime) => {
|
|
113
|
+
if (!stripe || !stripe._registerWrapper) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
stripe._registerWrapper({ name: 'stripe-js', startTime });
|
|
117
|
+
};
|
|
118
|
+
const initStripe = (maybeStripe, args, startTime) => {
|
|
119
|
+
if (maybeStripe === null) {
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
const stripe = maybeStripe.apply(undefined, args);
|
|
123
|
+
registerWrapper(stripe, startTime);
|
|
124
|
+
return stripe;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// Execute our own script injection after a tick to give users time to do their
|
|
128
|
+
// own script injection.
|
|
129
|
+
const stripePromise = Promise.resolve().then(() => zonosStripeLoadScript(null));
|
|
130
|
+
let loadCalled = false;
|
|
131
|
+
stripePromise.catch((err) => {
|
|
132
|
+
if (!loadCalled) {
|
|
133
|
+
// eslint-disable-next-line no-console
|
|
134
|
+
console.warn(err);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
const zonosLoadStripe = async (...args) => {
|
|
138
|
+
loadCalled = true;
|
|
139
|
+
const startTime = Date.now();
|
|
140
|
+
return stripePromise.then(maybeStripe => initStripe(maybeStripe, args, startTime));
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
var _a;
|
|
144
|
+
const ZONOS_URL = "https://unpkg.com/@zonos/elements@1.0.17-alpha.1";
|
|
145
|
+
const INTER_FONT_URL = 'https://rsms.me';
|
|
146
|
+
const INTER_FONT_REGEX = /^https:\/\/rsms\.me\/inter\/inter\.css(.*)$/;
|
|
147
|
+
class Zonos {
|
|
148
|
+
}
|
|
149
|
+
_a = Zonos;
|
|
150
|
+
Zonos.doneInit = false;
|
|
151
|
+
// if query 'zonosDebug=1' param from url, debug mode is on
|
|
152
|
+
Zonos.debug = !!window.location.search.match(/\?.*(&*)zonosDebug=1/);
|
|
153
|
+
Zonos.init = async ({ appearance, checkoutSettings, currencyConverter, helloSettings, zonosApiKey, }) => {
|
|
154
|
+
if (Zonos.doneInit) {
|
|
155
|
+
// do nothing if zonos is already initializeds
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
log('====== ZONOS DEBUG MODE: on ======');
|
|
159
|
+
if (!zonosApiKey) {
|
|
160
|
+
logError('Missing Zonos API key');
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
_a.zonosApiKey = zonosApiKey;
|
|
164
|
+
// eslint-disable-next-line no-console
|
|
165
|
+
log('init zonos');
|
|
166
|
+
// inject zonos cdn
|
|
167
|
+
Zonos.injectZonosCdn();
|
|
168
|
+
// inject css for zonos elements
|
|
169
|
+
Zonos.injectZonosCss();
|
|
170
|
+
// inject fonts for zonos elements
|
|
171
|
+
Zonos.injectFonts();
|
|
172
|
+
// init zonos controller
|
|
173
|
+
Zonos.injectController({
|
|
174
|
+
config: {
|
|
175
|
+
appearance,
|
|
176
|
+
checkoutSettings,
|
|
177
|
+
currencyConverter,
|
|
178
|
+
helloSettings,
|
|
179
|
+
},
|
|
180
|
+
zonosApiKey,
|
|
181
|
+
});
|
|
182
|
+
Zonos.doneInit = true;
|
|
183
|
+
};
|
|
184
|
+
Zonos.displayCurrency = () => {
|
|
185
|
+
const zonosHello = document.querySelector('zonos-hello');
|
|
186
|
+
if (zonosHello) {
|
|
187
|
+
void zonosHello.displayCurrency();
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
Zonos.showNotification = (notification) => {
|
|
191
|
+
const zonosController = document.querySelector('zonos-controller');
|
|
192
|
+
void (zonosController === null || zonosController === void 0 ? void 0 : zonosController.showNotification(notification));
|
|
193
|
+
};
|
|
194
|
+
Zonos.injectController = ({ config, zonosApiKey, }) => {
|
|
195
|
+
if (document.querySelector('zonos-controller')) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const zonosController = document.createElement('zonos-controller');
|
|
199
|
+
zonosController.config = config;
|
|
200
|
+
zonosController.zonosApiKey = zonosApiKey;
|
|
201
|
+
zonosController.addEventListener('stripeInitEvent', async (event) => {
|
|
202
|
+
const { detail } = event;
|
|
203
|
+
log('loading Stripe');
|
|
204
|
+
const stripe = await zonosLoadStripe(detail.publishableKey);
|
|
205
|
+
if (!stripe) {
|
|
206
|
+
logError('API seems to be invalid or missing. Failed to load Stripe');
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
window.Zonos.stripe = stripe;
|
|
210
|
+
void zonosController.initCheckoutElement(stripe);
|
|
211
|
+
});
|
|
212
|
+
document.body.appendChild(zonosController);
|
|
213
|
+
// eslint-disable-next-line no-console
|
|
214
|
+
log('Zonos controller injected!');
|
|
215
|
+
};
|
|
216
|
+
Zonos.injectZonosCdn = () => {
|
|
217
|
+
const findScript = () => {
|
|
218
|
+
const scripts = document.querySelectorAll(`script[src^="${ZONOS_URL}/dist/zonos-elements"]`);
|
|
219
|
+
scripts.forEach(script => {
|
|
220
|
+
if (script.src.indexOf('zonos-elements.js')) {
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
return false;
|
|
225
|
+
};
|
|
226
|
+
if (findScript()) ;
|
|
227
|
+
const headOrBody = document.head || document.body;
|
|
228
|
+
// init zonos elements when the zonosKey is valid
|
|
229
|
+
// inject esmodule cdn for zonos elements
|
|
230
|
+
const zonosEsmCdn = document.createElement('script');
|
|
231
|
+
zonosEsmCdn.type = 'module';
|
|
232
|
+
zonosEsmCdn.src = `${ZONOS_URL}/dist/zonos-elements/zonos-elements.esm.js`;
|
|
233
|
+
headOrBody.appendChild(zonosEsmCdn);
|
|
234
|
+
// inject non module cdn for zonos elements
|
|
235
|
+
const zonosCdn = document.createElement('script');
|
|
236
|
+
zonosCdn.noModule = true;
|
|
237
|
+
zonosCdn.src = `${ZONOS_URL}/dist/zonos-elements/zonos-elements.js`;
|
|
238
|
+
headOrBody.appendChild(zonosCdn);
|
|
239
|
+
// eslint-disable-next-line no-console
|
|
240
|
+
log('Zonos cdn script injected!');
|
|
241
|
+
};
|
|
242
|
+
Zonos.injectZonosCss = () => {
|
|
243
|
+
const findStyleLink = () => {
|
|
244
|
+
return !!document.querySelector(`link[rel="stylesheet"][href^="${ZONOS_URL}/dist/zonos-elements/zonos-elements.css"]`);
|
|
245
|
+
};
|
|
246
|
+
if (findStyleLink()) {
|
|
247
|
+
// eslint-disable-next-line no-console
|
|
248
|
+
log('Zonos style links already injected', 'warn');
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
const headOrBody = document.head || document.body;
|
|
252
|
+
// inject css for zonos elements
|
|
253
|
+
const zonosCssPreconnect = document.createElement('link');
|
|
254
|
+
zonosCssPreconnect.rel = 'preconnect';
|
|
255
|
+
zonosCssPreconnect.href = `${ZONOS_URL}/`;
|
|
256
|
+
headOrBody.appendChild(zonosCssPreconnect);
|
|
257
|
+
// inject css for zonos elements
|
|
258
|
+
const zonosCss = document.createElement('link');
|
|
259
|
+
zonosCss.rel = 'stylesheet';
|
|
260
|
+
zonosCss.href = `${ZONOS_URL}/dist/zonos-elements/zonos-elements.css`;
|
|
261
|
+
headOrBody.appendChild(zonosCss);
|
|
262
|
+
// eslint-disable-next-line no-console
|
|
263
|
+
log('Zonos style css injected!');
|
|
264
|
+
};
|
|
265
|
+
Zonos.injectFonts = () => {
|
|
266
|
+
const findFont = () => {
|
|
267
|
+
const links = document.querySelectorAll(`link[rel="stylesheet"][href^="${INTER_FONT_URL}"]`);
|
|
268
|
+
for (let i = 0; i < links.length; i++) {
|
|
269
|
+
const link = links[i];
|
|
270
|
+
if (!link) {
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
if (INTER_FONT_REGEX.test(link.href)) {
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
return link;
|
|
277
|
+
}
|
|
278
|
+
return null;
|
|
279
|
+
};
|
|
280
|
+
if (findFont()) {
|
|
281
|
+
// eslint-disable-next-line no-console
|
|
282
|
+
log('Inter font already injected', 'warn');
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const headOrBody = document.head || document.body;
|
|
286
|
+
// inject css for zonos elements
|
|
287
|
+
const fontPreconnect = document.createElement('link');
|
|
288
|
+
fontPreconnect.rel = 'preconnect';
|
|
289
|
+
fontPreconnect.href = INTER_FONT_URL;
|
|
290
|
+
headOrBody.appendChild(fontPreconnect);
|
|
291
|
+
// inject css for zonos elements
|
|
292
|
+
const font = document.createElement('link');
|
|
293
|
+
font.rel = 'stylesheet';
|
|
294
|
+
font.href = `${INTER_FONT_URL}/inter/inter.css`;
|
|
295
|
+
headOrBody.appendChild(font);
|
|
296
|
+
// eslint-disable-next-line no-console
|
|
297
|
+
log('Zonos font injected!');
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
window.Zonos = Zonos;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const logError = (message, ...rest) => {
|
|
4
|
+
// eslint-disable-next-line no-console
|
|
5
|
+
console.error(message, ...rest);
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
/* eslint-disable no-console */
|
|
9
|
+
/** Loading stripe script extracted from stripe.js repository */
|
|
10
|
+
const V3_URL = 'https://js.stripe.com/v3';
|
|
11
|
+
const V3_URL_REGEX = /^https:\/\/js\.stripe\.com\/v3\/?(\?.*)?$/;
|
|
12
|
+
const EXISTING_SCRIPT_MESSAGE = 'loadStripe.setLoadParameters was called but an existing Stripe.js script already exists in the document; existing script parameters will be used';
|
|
13
|
+
const zonosStripeFindScript = () => {
|
|
14
|
+
const scripts = document.querySelectorAll(`script[src^="${V3_URL}"]`);
|
|
15
|
+
for (let i = 0; i < scripts.length; i++) {
|
|
16
|
+
const script = scripts[i];
|
|
17
|
+
if (!script) {
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
if (!V3_URL_REGEX.test(script.src)) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
return script;
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
};
|
|
27
|
+
let stripePromise = null;
|
|
28
|
+
const injectScript = (params) => {
|
|
29
|
+
const queryString = params && !params.advancedFraudSignals ? '?advancedFraudSignals=false' : '';
|
|
30
|
+
const script = document.createElement('script');
|
|
31
|
+
script.src = `${V3_URL}${queryString}`;
|
|
32
|
+
const headBody = document.head || document.body;
|
|
33
|
+
if (!headBody) {
|
|
34
|
+
logError('Expected document.body not to be null. Stripe.js requires a <body> element.');
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
headBody.appendChild(script);
|
|
38
|
+
return script;
|
|
39
|
+
};
|
|
40
|
+
const zonosStripeLoadScript = (params) => {
|
|
41
|
+
// Ensure that we only attempt to load Stripe.js at most once
|
|
42
|
+
if (stripePromise !== null) {
|
|
43
|
+
return stripePromise;
|
|
44
|
+
}
|
|
45
|
+
stripePromise = new Promise((resolve, reject) => {
|
|
46
|
+
if (typeof window === 'undefined' || typeof document === 'undefined') {
|
|
47
|
+
// Resolve to null when imported server side. This makes the module
|
|
48
|
+
// safe to import in an isomorphic code base.
|
|
49
|
+
resolve(null);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (window.Stripe && params) {
|
|
53
|
+
console.warn(EXISTING_SCRIPT_MESSAGE);
|
|
54
|
+
}
|
|
55
|
+
if (window.Stripe) {
|
|
56
|
+
resolve(window.Stripe);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
let script = zonosStripeFindScript();
|
|
61
|
+
if (script && params) {
|
|
62
|
+
console.warn(EXISTING_SCRIPT_MESSAGE);
|
|
63
|
+
}
|
|
64
|
+
else if (!script) {
|
|
65
|
+
script = injectScript(params);
|
|
66
|
+
}
|
|
67
|
+
script === null || script === void 0 ? void 0 : script.addEventListener('load', () => {
|
|
68
|
+
if (window.Stripe) {
|
|
69
|
+
resolve(window.Stripe);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
reject(new Error('Stripe.js not available'));
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
script === null || script === void 0 ? void 0 : script.addEventListener('error', () => {
|
|
76
|
+
reject(new Error('Failed to load Stripe.js'));
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
reject(error);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
return stripePromise;
|
|
85
|
+
};
|
|
86
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
87
|
+
const registerWrapper = (stripe, startTime) => {
|
|
88
|
+
if (!stripe || !stripe._registerWrapper) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
stripe._registerWrapper({ name: 'stripe-js', startTime });
|
|
92
|
+
};
|
|
93
|
+
const initStripe = (maybeStripe, args, startTime) => {
|
|
94
|
+
if (maybeStripe === null) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
const stripe = maybeStripe.apply(undefined, args);
|
|
98
|
+
registerWrapper(stripe, startTime);
|
|
99
|
+
return stripe;
|
|
100
|
+
};
|
|
101
|
+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
102
|
+
const validateLoadParams = (params) => {
|
|
103
|
+
const errorMessage = `invalid load parameters; expected object of shape
|
|
104
|
+
|
|
105
|
+
{advancedFraudSignals: boolean}
|
|
106
|
+
|
|
107
|
+
but received
|
|
108
|
+
|
|
109
|
+
${JSON.stringify(params)}
|
|
110
|
+
`;
|
|
111
|
+
if (params === null || typeof params !== 'object') {
|
|
112
|
+
logError(errorMessage);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (Object.keys(params).length === 1 &&
|
|
116
|
+
typeof params.advancedFraudSignals === 'boolean') {
|
|
117
|
+
return params;
|
|
118
|
+
}
|
|
119
|
+
logError(errorMessage);
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
exports.initStripe = initStripe;
|
|
123
|
+
exports.injectScript = injectScript;
|
|
124
|
+
exports.validateLoadParams = validateLoadParams;
|
|
125
|
+
exports.zonosStripeFindScript = zonosStripeFindScript;
|
|
126
|
+
exports.zonosStripeLoadScript = zonosStripeLoadScript;
|