swell-js 5.9.1 → 5.9.3

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.
@@ -4871,8 +4871,36 @@
4871
4871
  };
4872
4872
  }
4873
4873
 
4874
+ function isCreateIntentRequest(method, url) {
4875
+ if (String(method).toLowerCase() !== 'post') {
4876
+ return false;
4877
+ }
4878
+
4879
+ return trimEnd(trimStart(url)) === 'intent';
4880
+ }
4881
+
4882
+ // Attach a captcha token to create-intent requests. The host supplies it via
4883
+ // the getCaptchaToken option and owns the widget and token lifecycle; here we
4884
+ // only forward it, so every intent-based payment method is covered. A rejection
4885
+ // from the host (timeout / failed challenge) propagates and aborts the request.
4886
+ async function addCaptchaToken(method, url, data, options) {
4887
+ if (
4888
+ typeof options.getCaptchaToken !== 'function' ||
4889
+ !isCreateIntentRequest(method, url)
4890
+ ) {
4891
+ return data;
4892
+ }
4893
+
4894
+ const token = await options.getCaptchaToken();
4895
+
4896
+ return token ? { ...data, captcha_token: token } : data;
4897
+ }
4898
+
4874
4899
  async function vaultRequest(method, url, data, options) {
4875
4900
  const { vaultUrl, timeout, key } = options;
4901
+
4902
+ data = await addCaptchaToken(method, url, data, options);
4903
+
4876
4904
  const requestData = {
4877
4905
  $jsonp: {
4878
4906
  method,
@@ -13182,7 +13210,30 @@
13182
13210
 
13183
13211
  const FORMATTERS = {};
13184
13212
 
13213
+ // A currency code is three letters. Anything else, such as a pending settings
13214
+ // Promise that was stringified to "[object Promise]", is not a currency.
13215
+ function isCurrencyCode(value) {
13216
+ return typeof value === 'string' && /^[a-z]{3}$/i.test(value);
13217
+ }
13218
+
13219
+ function defaultLocale() {
13220
+ return typeof navigator === 'object' ? navigator.language : 'en-US';
13221
+ }
13222
+
13185
13223
  function methods$1(api, opt) {
13224
+ // Reads a store setting without ever returning a Promise. Until settings
13225
+ // load, this starts the request and reports `loaded: false`.
13226
+ function readSetting(path, def) {
13227
+ const value = opt.api.settings.get(path, def);
13228
+
13229
+ if (value && typeof value.then === 'function') {
13230
+ value.catch(() => {}); // callers that await settings report the error
13231
+ return { loaded: false, value: def };
13232
+ }
13233
+
13234
+ return { loaded: true, value };
13235
+ }
13236
+
13186
13237
  return {
13187
13238
  code: null,
13188
13239
  state: null,
@@ -13200,52 +13251,72 @@
13200
13251
 
13201
13252
  selected() {
13202
13253
  if (!this.code) {
13203
- this.set(
13204
- opt.getCookie('swell-currency') ||
13205
- opt.api.settings.get('store.currency'),
13206
- );
13254
+ const cookieCode = opt.getCookie('swell-currency');
13255
+
13256
+ if (isCurrencyCode(cookieCode)) {
13257
+ this.set(cookieCode);
13258
+ } else {
13259
+ const storeCode = readSetting('store.currency');
13260
+
13261
+ if (!storeCode.loaded) {
13262
+ // Settings are still loading: use a stand-in without saving it,
13263
+ // so the store currency is picked up once they arrive.
13264
+ return 'USD';
13265
+ }
13266
+
13267
+ this.set(isCurrencyCode(storeCode.value) ? storeCode.value : 'USD');
13268
+ }
13207
13269
  }
13208
13270
 
13209
13271
  return this.code;
13210
13272
  },
13211
13273
 
13212
13274
  get() {
13213
- if (!this.code) {
13214
- this.code = this.selected();
13275
+ const code = this.selected();
13276
+
13277
+ if (this.state) {
13278
+ return this.state;
13215
13279
  }
13216
- if (!this.state) {
13217
- this.state = this.set(this.code);
13280
+
13281
+ if (!readSetting('store.currencies').loaded) {
13282
+ // Not cached, so the currency's rate and decimals apply once
13283
+ // settings load.
13284
+ return { code };
13218
13285
  }
13219
- return this.state;
13286
+
13287
+ return this.set(code);
13220
13288
  },
13221
13289
 
13222
13290
  set(code = 'USD') {
13223
- this.code = code;
13224
- this.state = find$1(this.list(), { code }) || { code };
13291
+ if (!isCurrencyCode(code)) {
13292
+ // Never store or persist something that isn't a currency code.
13293
+ return this.state || { code: this.code || 'USD' };
13294
+ }
13225
13295
 
13226
- this.locale = String(
13227
- opt.api.settings.get(
13228
- 'store.locale',
13229
- typeof navigator === 'object' ? navigator.language : 'en-US',
13230
- ),
13231
- );
13296
+ const list = readSetting('store.currencies', []);
13297
+ const state = (list.loaded && find$1(list.value, { code })) || { code };
13298
+
13299
+ this.code = code;
13300
+ // Only cache a state built from the loaded currency list.
13301
+ this.state = list.loaded ? state : null;
13302
+ this.locale = String(readSetting('store.locale', defaultLocale()).value);
13232
13303
 
13233
13304
  opt.setCookie('swell-currency', code);
13234
13305
 
13235
- return this.state;
13306
+ return state;
13236
13307
  },
13237
13308
 
13238
13309
  format(amount, params = {}) {
13239
13310
  let state = this.get();
13240
13311
  if (params.code && params.code !== state.code) {
13241
- const list = this.list();
13312
+ const list = readSetting('store.currencies', []).value;
13242
13313
  state = find$1(list, { code: params.code }) || { code: params.code };
13243
13314
  }
13244
13315
 
13245
13316
  const { code = 'USD', type, decimals, rate } = state;
13246
13317
  const formatCode = params.code || code;
13247
13318
  const formatRate = params.rate || rate;
13248
- const formatLocale = params.locale || this.locale;
13319
+ const formatLocale = params.locale || this.locale || defaultLocale();
13249
13320
  const formatDecimals = 'decimals' in params ? params.decimals : decimals;
13250
13321
  const { convert = true } = params;
13251
13322
 
@@ -13470,7 +13541,7 @@
13470
13541
  const api = {};
13471
13542
 
13472
13543
  Object.assign(api, {
13473
- version: '5.9.1',
13544
+ version: '5.9.3',
13474
13545
  options,
13475
13546
  request,
13476
13547
 
@@ -13500,6 +13571,7 @@
13500
13571
  options.getCart = opt.getCart;
13501
13572
  options.updateCart = opt.updateCart;
13502
13573
  options.headers = opt.headers || {};
13574
+ options.getCaptchaToken = opt.getCaptchaToken;
13503
13575
  },
13504
13576
 
13505
13577
  // Backward compatibility
@@ -13586,8 +13658,10 @@
13586
13658
 
13587
13659
  const session = allOptions.session || allOptions.getCookie('swell-session');
13588
13660
  const locale = allOptions.locale || allOptions.getCookie('swell-locale');
13661
+ const cookieCurrency = allOptions.getCookie('swell-currency');
13589
13662
  const currency =
13590
- allOptions.currency || allOptions.getCookie('swell-currency');
13663
+ allOptions.currency ||
13664
+ (isCurrencyCode(cookieCurrency) ? cookieCurrency : undefined);
13591
13665
  const path = allOptions.path || '/api';
13592
13666
 
13593
13667
  const baseUrl = `${allOptions.url}${allOptions.base || ''}${path}`;