@flittpayments/js-sdk 2.0.11-alpha.0 → 2.0.12

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.
@@ -91,6 +91,10 @@ const isArray = (o) => {
91
91
  return getType(o) === 'array'
92
92
  };
93
93
 
94
+ const isBoolean = (o) => {
95
+ return typeof o === 'boolean'
96
+ };
97
+
94
98
  const isElement = (o) => {
95
99
  return o && o.nodeType === 1
96
100
  };
@@ -464,6 +468,11 @@ const loadExternalApi = (url, path) => {
464
468
  return promise
465
469
  };
466
470
 
471
+ const transitionDelay = (el) => {
472
+ let { transitionDelay, transitionDuration } = getComputedStyle(el);
473
+ return (Number.parseFloat(transitionDuration) + Number.parseFloat(transitionDelay)) * 1000
474
+ };
475
+
467
476
  var Utils = /*#__PURE__*/Object.freeze({
468
477
  __proto__: null,
469
478
  PromiseWithResolvers: PromiseWithResolvers,
@@ -485,6 +494,7 @@ var Utils = /*#__PURE__*/Object.freeze({
485
494
  hasProp: hasProp,
486
495
  isArguments: isArguments,
487
496
  isArray: isArray,
497
+ isBoolean: isBoolean,
488
498
  isDate: isDate,
489
499
  isElement: isElement,
490
500
  isError: isError,
@@ -505,6 +515,7 @@ var Utils = /*#__PURE__*/Object.freeze({
505
515
  removeElement: removeElement,
506
516
  stringFormat: stringFormat,
507
517
  toArray: toArray,
518
+ transitionDelay: transitionDelay,
508
519
  uuid: uuid
509
520
  });
510
521
 
@@ -629,6 +640,7 @@ const ButtonFrameCss = {
629
640
  overflow: 'hidden !important',
630
641
  height: '100% !important',
631
642
  outline: 'none !important',
643
+ 'color-scheme': 'light',
632
644
  'z-index': '1 !important',
633
645
  };
634
646
 
@@ -855,6 +867,14 @@ function Deferred() {
855
867
  isProgress() {
856
868
  return status === PROGRESS
857
869
  },
870
+ clear() {
871
+ if (this.isPending()) {
872
+ resolveCallbacks.length = 0;
873
+ rejectCallbacks.length = 0;
874
+ notifyCallbacks.length = 0;
875
+ }
876
+ return this
877
+ },
858
878
  resolveWith(context, args) {
859
879
  if (this.isPending()) {
860
880
  status = RESOLVED;
@@ -1808,6 +1828,9 @@ const PaymentRequestApi = Module.extend({
1808
1828
  setMerchant(merchant) {
1809
1829
  this.merchant = merchant;
1810
1830
  },
1831
+ getProviderNames() {
1832
+ return this.supported.provider.filter((name) => !!~this.payload.allowed.indexOf(name))
1833
+ },
1811
1834
  setBeforeCallback(beforeCallback) {
1812
1835
  if (isFunction(beforeCallback)) {
1813
1836
  this.params.before = beforeCallback;
@@ -1857,39 +1880,34 @@ const PaymentRequestApi = Module.extend({
1857
1880
  update(data) {
1858
1881
  if (this.isPending()) return
1859
1882
  this.setPending(true);
1860
- const context = this;
1861
- const defer = Deferred();
1862
1883
  this.params.data = data;
1884
+ this.defer = Deferred();
1863
1885
  this.request(
1864
1886
  'api.checkout.pay',
1865
1887
  'methods',
1866
1888
  this.params.data,
1867
- function (model) {
1868
- context.setPending(false);
1869
- context.setPayload(model.serialize());
1870
- defer.resolve(model);
1871
- },
1872
- function (model) {
1873
- context.setPending(false);
1874
- context.trigger('error', model);
1875
- defer.reject(model);
1876
- }
1889
+ this.proxy(function (_cx, model) {
1890
+ this.setPending(false);
1891
+ this.setPayload(model.serialize());
1892
+ this.defer.resolve(model);
1893
+ }),
1894
+ this.proxy(function (_cx, model) {
1895
+ this.setPending(false);
1896
+ this.setPayload(model.serialize());
1897
+ this.trigger('error', model.serialize());
1898
+ this.defer.reject(model);
1899
+ })
1877
1900
  );
1878
- return defer
1901
+ return this.defer
1879
1902
  },
1880
1903
  isPending() {
1881
1904
  return this.pendingState === true
1882
1905
  },
1883
1906
  setPending(state) {
1884
- this.pendingState = state;
1885
- clearTimeout(this.pendingTimeoutEvent);
1886
- this.pendingTimeoutEvent = setTimeout(
1887
- function (context) {
1888
- context.trigger('pending', state);
1889
- },
1890
- 50,
1891
- this
1892
- );
1907
+ if (this.pendingState !== state) {
1908
+ this.pendingState = state;
1909
+ this.trigger('pending', state);
1910
+ }
1893
1911
  },
1894
1912
  before() {
1895
1913
  if (this.isPending()) return
@@ -1993,9 +2011,12 @@ const PaymentElement = Module.extend({
1993
2011
  },
1994
2012
  init(params) {
1995
2013
  this.state = {
1996
- isSupported: false,
1997
- isAllowed: false,
1998
- isMounted: false,
2014
+ type: 'hide',
2015
+ transition: false,
2016
+ pending: false,
2017
+ mounted: false,
2018
+ supported: false,
2019
+ allowed: false,
1999
2020
  };
2000
2021
  this.params = {};
2001
2022
  this.utils.extend(this.params, this.defaults);
@@ -2005,7 +2026,7 @@ const PaymentElement = Module.extend({
2005
2026
  timeout(callback, timeout) {
2006
2027
  this.timeoutMap = this.timeoutMap || {};
2007
2028
  clearTimeout(this.timeoutMap[timeout]);
2008
- this.timeoutMap[callback] = setTimeout(callback, timeout);
2029
+ this.timeoutMap[callback] = setTimeout(this.proxy(callback), timeout);
2009
2030
  return this
2010
2031
  },
2011
2032
  getElementUrl() {
@@ -2054,11 +2075,11 @@ const PaymentElement = Module.extend({
2054
2075
  },
2055
2076
  onEvent(cx, ev) {
2056
2077
  ev.preventDefault();
2057
- if (this.pending) return false
2078
+ if (this.state.pending) return false
2058
2079
  this.send('event', { type: ev.type });
2059
2080
  },
2060
2081
  onClick() {
2061
- if (this.pending) return false
2082
+ if (this.state.pending) return false
2062
2083
  this.request.before().done(this.proxy('onClickDone')).fail(this.proxy('onClickFail'));
2063
2084
  },
2064
2085
  onClickDone() {
@@ -2066,19 +2087,19 @@ const PaymentElement = Module.extend({
2066
2087
  },
2067
2088
  onClickFail() {},
2068
2089
  onSupported(cx, supported) {
2069
- this.state.isSupported = supported.provider.includes(this.params.method);
2090
+ this.state.supported = supported.provider.includes(this.params.method);
2070
2091
  this.render();
2071
2092
  },
2072
2093
  onPayload(cx, payload) {
2073
- this.state.isAllowed = payload.allowed.includes(this.params.method);
2094
+ this.state.allowed = payload.allowed.includes(this.params.method);
2074
2095
  this.render();
2075
2096
  },
2076
2097
  onPending(cx, state) {
2077
- this.pending = state;
2098
+ this.state.pending = state;
2078
2099
  this.addCss(this.element, {
2079
2100
  transition: 'height 0.2s ease-out, opacity 0.4s ease-out',
2080
- pointerEvents: this.pending ? 'none' : '',
2081
- opacity: this.pending ? '0.5' : '1',
2101
+ pointerEvents: this.state.pending ? 'none' : '',
2102
+ opacity: this.state.pending ? '0.5' : '1',
2082
2103
  });
2083
2104
  },
2084
2105
  initEvents() {
@@ -2091,10 +2112,10 @@ const PaymentElement = Module.extend({
2091
2112
  setPaymentRequest(request) {
2092
2113
  if (!(request instanceof PaymentRequestApi))
2093
2114
  throw Error('request is not instance of PaymentRequestApi')
2094
- this.request = request;
2095
2115
  const onSupported = this.proxy('onSupported');
2096
2116
  const onPayload = this.proxy('onPayload');
2097
2117
  const onPending = this.proxy('onPending');
2118
+ this.request = request;
2098
2119
  this.request.off('supported', onSupported).on('supported', onSupported);
2099
2120
  this.request.off('payload', onPayload).on('payload', onPayload);
2100
2121
  this.request.off('pending', onPending).on('pending', onPending);
@@ -2107,51 +2128,94 @@ const PaymentElement = Module.extend({
2107
2128
  return this
2108
2129
  },
2109
2130
  render() {
2110
- if (this.state.isSupported === false) return this
2111
- if (this.state.isAllowed === false) return this
2131
+ if (this.state.supported === false) return this.hide()
2132
+ if (this.state.allowed === false) return this.hide()
2112
2133
  this.mount();
2113
2134
  this.show();
2114
2135
  },
2136
+ notMounted() {
2137
+ return this.state.mounted === false
2138
+ },
2139
+ isMounted() {
2140
+ return this.state.mounted === true
2141
+ },
2115
2142
  unmount() {
2116
2143
  if (this.element.parentNode) {
2117
2144
  this.element.parentNode.removeChild(this.element);
2145
+ this.state.mounted = false;
2118
2146
  }
2119
2147
  return this
2120
2148
  },
2121
2149
  mount() {
2122
- if (document.body.contains(this.element) === false) {
2150
+ if (!document.body.contains(this.element)) {
2123
2151
  this.appendTo(this.params.appendTo);
2152
+ this.state.mounted = true;
2124
2153
  }
2125
2154
  return this
2126
2155
  },
2156
+ transitionValue(value) {
2157
+ return value
2158
+ },
2159
+ getTimeoutValue() {
2160
+ return toArray(arguments).map(transitionDelay).sort().pop()
2161
+ },
2162
+ getState(state, transition) {
2163
+ return this.state.type === (state ? 'show' : 'hide') && this.state.transition === transition
2164
+ },
2165
+ triggerState() {
2166
+ this.request.trigger(this.state.type, {
2167
+ transition: this.state.transition,
2168
+ method: this.params.method,
2169
+ });
2170
+ },
2127
2171
  show() {
2128
- this.timeout(() => {
2129
- this.addCss(this.iframe, {
2130
- transition: 'opacity 0.6s 0.4s ease-out',
2131
- opacity: this.utils.cssUnit(1),
2132
- });
2133
- this.addCss(this.element, {
2134
- transition: 'height 0.2s ease-out',
2135
- height: this.utils.cssUnit(this.params.height, 'px'),
2136
- });
2137
- this.trigger('show', {});
2138
- }, 25);
2172
+ if (this.notMounted()) return
2173
+ this.timeout('showCallback', 25);
2139
2174
  return this
2140
2175
  },
2141
2176
  hide() {
2142
- this.timeout(() => {
2143
- this.addCss(this.iframe, {
2144
- transition: 'opacity 0.4s ease-out',
2145
- opacity: this.utils.cssUnit(0),
2146
- });
2147
- this.addCss(this.element, {
2148
- transition: 'height 0.2s 0.4s ease-out',
2149
- height: this.utils.cssUnit(0, 'px'),
2150
- });
2151
- this.trigger('hide', {});
2152
- }, 25);
2177
+ if (this.notMounted()) return
2178
+ this.timeout('hideCallback', 25);
2153
2179
  return this
2154
2180
  },
2181
+ showCallback() {
2182
+ this.addCss(this.element, {
2183
+ transition: this.transitionValue('height 0.2s ease-out'),
2184
+ height: this.utils.cssUnit(this.params.height, 'px'),
2185
+ 'will-change': 'height',
2186
+ });
2187
+ this.addCss(this.iframe, {
2188
+ transition: this.transitionValue('opacity 0.225s 0.225s ease-out'),
2189
+ opacity: this.utils.cssUnit(1),
2190
+ 'will-change': 'opacity',
2191
+ });
2192
+ this.afterCallback('show');
2193
+ },
2194
+ hideCallback() {
2195
+ this.addCss(this.element, {
2196
+ transition: this.transitionValue('height 0.225s 0.225s ease-out'),
2197
+ height: this.utils.cssUnit(0, 'px'),
2198
+ 'will-change': 'height',
2199
+ });
2200
+ this.addCss(this.iframe, {
2201
+ transition: this.transitionValue('opacity 0.225s ease-out'),
2202
+ opacity: this.utils.cssUnit(0),
2203
+ 'will-change': 'opacity',
2204
+ });
2205
+ this.afterCallback('hide');
2206
+ },
2207
+ afterCallback(state) {
2208
+ if (this.state.type === state) return
2209
+ if (this.state.transition) return
2210
+ this.state.transition = true;
2211
+ this.state.type = state;
2212
+ this.triggerState();
2213
+ this.timeout('afterTransition', this.getTimeoutValue(this.iframe, this.element));
2214
+ },
2215
+ afterTransition() {
2216
+ this.state.transition = false;
2217
+ this.triggerState();
2218
+ },
2155
2219
  });
2156
2220
 
2157
2221
  const PaymentButton = Module.extend({
@@ -2220,16 +2284,21 @@ const PaymentButton = Module.extend({
2220
2284
  });
2221
2285
  this.request.setApi(this.api);
2222
2286
  this.request.setMerchant(this.params.data.merchant_id);
2287
+ this.request.on('pending', this.proxy('onPending'));
2223
2288
  this.request.on('details', this.proxy('onDetails'));
2224
2289
  this.request.on('error', this.proxy('onError'));
2290
+ this.request.on('show', this.proxy('onShow'));
2291
+ this.request.on('hide', this.proxy('onHide'));
2225
2292
  },
2226
2293
  initElements() {
2294
+ const self = this;
2227
2295
  const style = this.params.style;
2228
2296
  const data = this.params.data;
2229
2297
  const origin = this.params.origin;
2230
2298
  const appendTo = this.params.element;
2231
2299
  const endpoint = this.params.endpoint.element;
2232
2300
  const request = this.request;
2301
+ this.buttons = [];
2233
2302
  this.container = this.utils.querySelector(this.params.element);
2234
2303
  this.addCss(this.container, {
2235
2304
  display: 'flex',
@@ -2248,13 +2317,14 @@ const PaymentButton = Module.extend({
2248
2317
  height: style.height,
2249
2318
  });
2250
2319
  element.setPaymentRequest(request);
2320
+ self.buttons.push(element);
2251
2321
  });
2252
2322
  request.getSupportedMethods();
2253
2323
  },
2254
2324
  update(data) {
2255
2325
  return this.request.update(this.utils.extend(this.params.data, data || {}))
2256
2326
  },
2257
- onDetails(cx, data) {
2327
+ onDetails(_cx, data) {
2258
2328
  this.api.scope(() => {
2259
2329
  this.request.after(this.params.data).done((extendParams) => {
2260
2330
  this.api
@@ -2268,12 +2338,40 @@ const PaymentButton = Module.extend({
2268
2338
  });
2269
2339
  });
2270
2340
  },
2271
- onSuccess(cx, data) {
2341
+ onSuccess(_cx, data) {
2272
2342
  this.trigger('success', data);
2273
2343
  },
2274
- onError(cx, data) {
2344
+ onError(_cx, data) {
2275
2345
  this.trigger('error', data);
2276
2346
  },
2347
+ toggleEventType(state, transition) {
2348
+ return state ? (transition ? 'show' : 'shown') : transition ? 'hide' : 'hidden'
2349
+ },
2350
+ toggleEventNamespace(state, data) {
2351
+ const name = [];
2352
+ name.push(this.toggleEventType(state, data.transition));
2353
+ name.push(data.method);
2354
+ return name.join(':')
2355
+ },
2356
+ triggerEventComplete(state, data) {
2357
+ const transition = data.transition;
2358
+ const name = this.toggleEventType(state, transition);
2359
+ const check = this.buttons
2360
+ .filter((i) => i.isMounted())
2361
+ .every((i) => i.getState(state, transition));
2362
+ if (check) this.trigger(name, {});
2363
+ },
2364
+ onShow(_cx, data) {
2365
+ this.trigger(this.toggleEventNamespace(true, data), {});
2366
+ this.triggerEventComplete(true, data);
2367
+ },
2368
+ onHide(_cx, data) {
2369
+ this.trigger(this.toggleEventNamespace(false, data), {});
2370
+ this.triggerEventComplete(false, data);
2371
+ },
2372
+ onPending(_cx, state) {
2373
+ this.trigger('pending', state);
2374
+ },
2277
2375
  });
2278
2376
 
2279
2377
  const parseConfig = (config) => {
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes