@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.
@@ -83,6 +83,9 @@ const isFunction = o => {
83
83
  const isArray = o => {
84
84
  return getType(o) === 'array';
85
85
  };
86
+ const isBoolean = o => {
87
+ return typeof o === 'boolean';
88
+ };
86
89
  const isElement = o => {
87
90
  return o && o.nodeType === 1;
88
91
  };
@@ -426,6 +429,13 @@ const loadExternalApi = (url, path) => {
426
429
  document.head.appendChild(script);
427
430
  return promise;
428
431
  };
432
+ const transitionDelay = el => {
433
+ let {
434
+ transitionDelay,
435
+ transitionDuration
436
+ } = getComputedStyle(el);
437
+ return (Number.parseFloat(transitionDuration) + Number.parseFloat(transitionDelay)) * 1000;
438
+ };
429
439
 
430
440
  var Utils = /*#__PURE__*/Object.freeze({
431
441
  __proto__: null,
@@ -448,6 +458,7 @@ var Utils = /*#__PURE__*/Object.freeze({
448
458
  hasProp: hasProp,
449
459
  isArguments: isArguments,
450
460
  isArray: isArray,
461
+ isBoolean: isBoolean,
451
462
  isDate: isDate,
452
463
  isElement: isElement,
453
464
  isError: isError,
@@ -468,6 +479,7 @@ var Utils = /*#__PURE__*/Object.freeze({
468
479
  removeElement: removeElement,
469
480
  stringFormat: stringFormat,
470
481
  toArray: toArray,
482
+ transitionDelay: transitionDelay,
471
483
  uuid: uuid
472
484
  });
473
485
 
@@ -546,6 +558,7 @@ const ButtonFrameCss = {
546
558
  overflow: 'hidden !important',
547
559
  height: '100% !important',
548
560
  outline: 'none !important',
561
+ 'color-scheme': 'light',
549
562
  'z-index': '1 !important'
550
563
  };
551
564
  const ButtonFrameAttrs = {
@@ -764,6 +777,14 @@ function Deferred() {
764
777
  isProgress() {
765
778
  return status === PROGRESS;
766
779
  },
780
+ clear() {
781
+ if (this.isPending()) {
782
+ resolveCallbacks.length = 0;
783
+ rejectCallbacks.length = 0;
784
+ notifyCallbacks.length = 0;
785
+ }
786
+ return this;
787
+ },
767
788
  resolveWith(context, args) {
768
789
  if (this.isPending()) {
769
790
  status = RESOLVED;
@@ -1678,6 +1699,9 @@ const PaymentRequestApi = Module.extend({
1678
1699
  setMerchant(merchant) {
1679
1700
  this.merchant = merchant;
1680
1701
  },
1702
+ getProviderNames() {
1703
+ return this.supported.provider.filter(name => !!~this.payload.allowed.indexOf(name));
1704
+ },
1681
1705
  setBeforeCallback(beforeCallback) {
1682
1706
  if (isFunction(beforeCallback)) {
1683
1707
  this.params.before = beforeCallback;
@@ -1721,29 +1745,28 @@ const PaymentRequestApi = Module.extend({
1721
1745
  update(data) {
1722
1746
  if (this.isPending()) return;
1723
1747
  this.setPending(true);
1724
- const context = this;
1725
- const defer = Deferred();
1726
1748
  this.params.data = data;
1727
- this.request('api.checkout.pay', 'methods', this.params.data, function (model) {
1728
- context.setPending(false);
1729
- context.setPayload(model.serialize());
1730
- defer.resolve(model);
1731
- }, function (model) {
1732
- context.setPending(false);
1733
- context.trigger('error', model);
1734
- defer.reject(model);
1735
- });
1736
- return defer;
1749
+ this.defer = Deferred();
1750
+ this.request('api.checkout.pay', 'methods', this.params.data, this.proxy(function (_cx, model) {
1751
+ this.setPending(false);
1752
+ this.setPayload(model.serialize());
1753
+ this.defer.resolve(model);
1754
+ }), this.proxy(function (_cx, model) {
1755
+ this.setPending(false);
1756
+ this.setPayload(model.serialize());
1757
+ this.trigger('error', model.serialize());
1758
+ this.defer.reject(model);
1759
+ }));
1760
+ return this.defer;
1737
1761
  },
1738
1762
  isPending() {
1739
1763
  return this.pendingState === true;
1740
1764
  },
1741
1765
  setPending(state) {
1742
- this.pendingState = state;
1743
- clearTimeout(this.pendingTimeoutEvent);
1744
- this.pendingTimeoutEvent = setTimeout(function (context) {
1745
- context.trigger('pending', state);
1746
- }, 50, this);
1766
+ if (this.pendingState !== state) {
1767
+ this.pendingState = state;
1768
+ this.trigger('pending', state);
1769
+ }
1747
1770
  },
1748
1771
  before() {
1749
1772
  if (this.isPending()) return;
@@ -1853,9 +1876,12 @@ const PaymentElement = Module.extend({
1853
1876
  },
1854
1877
  init(params) {
1855
1878
  this.state = {
1856
- isSupported: false,
1857
- isAllowed: false,
1858
- isMounted: false
1879
+ type: 'hide',
1880
+ transition: false,
1881
+ pending: false,
1882
+ mounted: false,
1883
+ supported: false,
1884
+ allowed: false
1859
1885
  };
1860
1886
  this.params = {};
1861
1887
  this.utils.extend(this.params, this.defaults);
@@ -1865,7 +1891,7 @@ const PaymentElement = Module.extend({
1865
1891
  timeout(callback, timeout) {
1866
1892
  this.timeoutMap = this.timeoutMap || {};
1867
1893
  clearTimeout(this.timeoutMap[timeout]);
1868
- this.timeoutMap[callback] = setTimeout(callback, timeout);
1894
+ this.timeoutMap[callback] = setTimeout(this.proxy(callback), timeout);
1869
1895
  return this;
1870
1896
  },
1871
1897
  getElementUrl() {
@@ -1914,13 +1940,13 @@ const PaymentElement = Module.extend({
1914
1940
  },
1915
1941
  onEvent(cx, ev) {
1916
1942
  ev.preventDefault();
1917
- if (this.pending) return false;
1943
+ if (this.state.pending) return false;
1918
1944
  this.send('event', {
1919
1945
  type: ev.type
1920
1946
  });
1921
1947
  },
1922
1948
  onClick() {
1923
- if (this.pending) return false;
1949
+ if (this.state.pending) return false;
1924
1950
  this.request.before().done(this.proxy('onClickDone')).fail(this.proxy('onClickFail'));
1925
1951
  },
1926
1952
  onClickDone() {
@@ -1928,19 +1954,19 @@ const PaymentElement = Module.extend({
1928
1954
  },
1929
1955
  onClickFail() {},
1930
1956
  onSupported(cx, supported) {
1931
- this.state.isSupported = supported.provider.includes(this.params.method);
1957
+ this.state.supported = supported.provider.includes(this.params.method);
1932
1958
  this.render();
1933
1959
  },
1934
1960
  onPayload(cx, payload) {
1935
- this.state.isAllowed = payload.allowed.includes(this.params.method);
1961
+ this.state.allowed = payload.allowed.includes(this.params.method);
1936
1962
  this.render();
1937
1963
  },
1938
1964
  onPending(cx, state) {
1939
- this.pending = state;
1965
+ this.state.pending = state;
1940
1966
  this.addCss(this.element, {
1941
1967
  transition: 'height 0.2s ease-out, opacity 0.4s ease-out',
1942
- pointerEvents: this.pending ? 'none' : '',
1943
- opacity: this.pending ? '0.5' : '1'
1968
+ pointerEvents: this.state.pending ? 'none' : '',
1969
+ opacity: this.state.pending ? '0.5' : '1'
1944
1970
  });
1945
1971
  },
1946
1972
  initEvents() {
@@ -1952,10 +1978,10 @@ const PaymentElement = Module.extend({
1952
1978
  },
1953
1979
  setPaymentRequest(request) {
1954
1980
  if (!(request instanceof PaymentRequestApi)) throw Error('request is not instance of PaymentRequestApi');
1955
- this.request = request;
1956
1981
  const onSupported = this.proxy('onSupported');
1957
1982
  const onPayload = this.proxy('onPayload');
1958
1983
  const onPending = this.proxy('onPending');
1984
+ this.request = request;
1959
1985
  this.request.off('supported', onSupported).on('supported', onSupported);
1960
1986
  this.request.off('payload', onPayload).on('payload', onPayload);
1961
1987
  this.request.off('pending', onPending).on('pending', onPending);
@@ -1968,50 +1994,93 @@ const PaymentElement = Module.extend({
1968
1994
  return this;
1969
1995
  },
1970
1996
  render() {
1971
- if (this.state.isSupported === false) return this;
1972
- if (this.state.isAllowed === false) return this;
1997
+ if (this.state.supported === false) return this.hide();
1998
+ if (this.state.allowed === false) return this.hide();
1973
1999
  this.mount();
1974
2000
  this.show();
1975
2001
  },
2002
+ notMounted() {
2003
+ return this.state.mounted === false;
2004
+ },
2005
+ isMounted() {
2006
+ return this.state.mounted === true;
2007
+ },
1976
2008
  unmount() {
1977
2009
  if (this.element.parentNode) {
1978
2010
  this.element.parentNode.removeChild(this.element);
2011
+ this.state.mounted = false;
1979
2012
  }
1980
2013
  return this;
1981
2014
  },
1982
2015
  mount() {
1983
- if (document.body.contains(this.element) === false) {
2016
+ if (!document.body.contains(this.element)) {
1984
2017
  this.appendTo(this.params.appendTo);
2018
+ this.state.mounted = true;
1985
2019
  }
1986
2020
  return this;
1987
2021
  },
2022
+ transitionValue(value) {
2023
+ return value;
2024
+ },
2025
+ getTimeoutValue() {
2026
+ return toArray(arguments).map(transitionDelay).sort().pop();
2027
+ },
2028
+ getState(state, transition) {
2029
+ return this.state.type === (state ? 'show' : 'hide') && this.state.transition === transition;
2030
+ },
2031
+ triggerState() {
2032
+ this.request.trigger(this.state.type, {
2033
+ transition: this.state.transition,
2034
+ method: this.params.method
2035
+ });
2036
+ },
1988
2037
  show() {
1989
- this.timeout(() => {
1990
- this.addCss(this.iframe, {
1991
- transition: 'opacity 0.6s 0.4s ease-out',
1992
- opacity: this.utils.cssUnit(1)
1993
- });
1994
- this.addCss(this.element, {
1995
- transition: 'height 0.2s ease-out',
1996
- height: this.utils.cssUnit(this.params.height, 'px')
1997
- });
1998
- this.trigger('show', {});
1999
- }, 25);
2038
+ if (this.notMounted()) return;
2039
+ this.timeout('showCallback', 25);
2000
2040
  return this;
2001
2041
  },
2002
2042
  hide() {
2003
- this.timeout(() => {
2004
- this.addCss(this.iframe, {
2005
- transition: 'opacity 0.4s ease-out',
2006
- opacity: this.utils.cssUnit(0)
2007
- });
2008
- this.addCss(this.element, {
2009
- transition: 'height 0.2s 0.4s ease-out',
2010
- height: this.utils.cssUnit(0, 'px')
2011
- });
2012
- this.trigger('hide', {});
2013
- }, 25);
2043
+ if (this.notMounted()) return;
2044
+ this.timeout('hideCallback', 25);
2014
2045
  return this;
2046
+ },
2047
+ showCallback() {
2048
+ this.addCss(this.element, {
2049
+ transition: this.transitionValue('height 0.2s ease-out'),
2050
+ height: this.utils.cssUnit(this.params.height, 'px'),
2051
+ 'will-change': 'height'
2052
+ });
2053
+ this.addCss(this.iframe, {
2054
+ transition: this.transitionValue('opacity 0.225s 0.225s ease-out'),
2055
+ opacity: this.utils.cssUnit(1),
2056
+ 'will-change': 'opacity'
2057
+ });
2058
+ this.afterCallback('show');
2059
+ },
2060
+ hideCallback() {
2061
+ this.addCss(this.element, {
2062
+ transition: this.transitionValue('height 0.225s 0.225s ease-out'),
2063
+ height: this.utils.cssUnit(0, 'px'),
2064
+ 'will-change': 'height'
2065
+ });
2066
+ this.addCss(this.iframe, {
2067
+ transition: this.transitionValue('opacity 0.225s ease-out'),
2068
+ opacity: this.utils.cssUnit(0),
2069
+ 'will-change': 'opacity'
2070
+ });
2071
+ this.afterCallback('hide');
2072
+ },
2073
+ afterCallback(state) {
2074
+ if (this.state.type === state) return;
2075
+ if (this.state.transition) return;
2076
+ this.state.transition = true;
2077
+ this.state.type = state;
2078
+ this.triggerState();
2079
+ this.timeout('afterTransition', this.getTimeoutValue(this.iframe, this.element));
2080
+ },
2081
+ afterTransition() {
2082
+ this.state.transition = false;
2083
+ this.triggerState();
2015
2084
  }
2016
2085
  });
2017
2086
 
@@ -2081,16 +2150,21 @@ const PaymentButton = Module.extend({
2081
2150
  });
2082
2151
  this.request.setApi(this.api);
2083
2152
  this.request.setMerchant(this.params.data.merchant_id);
2153
+ this.request.on('pending', this.proxy('onPending'));
2084
2154
  this.request.on('details', this.proxy('onDetails'));
2085
2155
  this.request.on('error', this.proxy('onError'));
2156
+ this.request.on('show', this.proxy('onShow'));
2157
+ this.request.on('hide', this.proxy('onHide'));
2086
2158
  },
2087
2159
  initElements() {
2160
+ const self = this;
2088
2161
  const style = this.params.style;
2089
2162
  const data = this.params.data;
2090
2163
  const origin = this.params.origin;
2091
2164
  const appendTo = this.params.element;
2092
2165
  const endpoint = this.params.endpoint.element;
2093
2166
  const request = this.request;
2167
+ this.buttons = [];
2094
2168
  this.container = this.utils.querySelector(this.params.element);
2095
2169
  this.addCss(this.container, {
2096
2170
  display: 'flex',
@@ -2109,24 +2183,51 @@ const PaymentButton = Module.extend({
2109
2183
  height: style.height
2110
2184
  });
2111
2185
  element.setPaymentRequest(request);
2186
+ self.buttons.push(element);
2112
2187
  });
2113
2188
  request.getSupportedMethods();
2114
2189
  },
2115
2190
  update(data) {
2116
2191
  return this.request.update(this.utils.extend(this.params.data, data || {}));
2117
2192
  },
2118
- onDetails(cx, data) {
2193
+ onDetails(_cx, data) {
2119
2194
  this.api.scope(() => {
2120
2195
  this.request.after(this.params.data).done(extendParams => {
2121
2196
  this.api.request('api.checkout.form', 'request', this.utils.extend({}, this.params.data, extendParams || {}, data)).done(this.proxy('onSuccess')).fail(this.proxy('onError'));
2122
2197
  });
2123
2198
  });
2124
2199
  },
2125
- onSuccess(cx, data) {
2200
+ onSuccess(_cx, data) {
2126
2201
  this.trigger('success', data);
2127
2202
  },
2128
- onError(cx, data) {
2203
+ onError(_cx, data) {
2129
2204
  this.trigger('error', data);
2205
+ },
2206
+ toggleEventType(state, transition) {
2207
+ return state ? transition ? 'show' : 'shown' : transition ? 'hide' : 'hidden';
2208
+ },
2209
+ toggleEventNamespace(state, data) {
2210
+ const name = [];
2211
+ name.push(this.toggleEventType(state, data.transition));
2212
+ name.push(data.method);
2213
+ return name.join(':');
2214
+ },
2215
+ triggerEventComplete(state, data) {
2216
+ const transition = data.transition;
2217
+ const name = this.toggleEventType(state, transition);
2218
+ const check = this.buttons.filter(i => i.isMounted()).every(i => i.getState(state, transition));
2219
+ if (check) this.trigger(name, {});
2220
+ },
2221
+ onShow(_cx, data) {
2222
+ this.trigger(this.toggleEventNamespace(true, data), {});
2223
+ this.triggerEventComplete(true, data);
2224
+ },
2225
+ onHide(_cx, data) {
2226
+ this.trigger(this.toggleEventNamespace(false, data), {});
2227
+ this.triggerEventComplete(false, data);
2228
+ },
2229
+ onPending(_cx, state) {
2230
+ this.trigger('pending', state);
2130
2231
  }
2131
2232
  });
2132
2233