@flittpayments/js-sdk 2.0.11 → 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
 
@@ -765,6 +777,14 @@ function Deferred() {
765
777
  isProgress() {
766
778
  return status === PROGRESS;
767
779
  },
780
+ clear() {
781
+ if (this.isPending()) {
782
+ resolveCallbacks.length = 0;
783
+ rejectCallbacks.length = 0;
784
+ notifyCallbacks.length = 0;
785
+ }
786
+ return this;
787
+ },
768
788
  resolveWith(context, args) {
769
789
  if (this.isPending()) {
770
790
  status = RESOLVED;
@@ -1679,6 +1699,9 @@ const PaymentRequestApi = Module.extend({
1679
1699
  setMerchant(merchant) {
1680
1700
  this.merchant = merchant;
1681
1701
  },
1702
+ getProviderNames() {
1703
+ return this.supported.provider.filter(name => !!~this.payload.allowed.indexOf(name));
1704
+ },
1682
1705
  setBeforeCallback(beforeCallback) {
1683
1706
  if (isFunction(beforeCallback)) {
1684
1707
  this.params.before = beforeCallback;
@@ -1722,29 +1745,28 @@ const PaymentRequestApi = Module.extend({
1722
1745
  update(data) {
1723
1746
  if (this.isPending()) return;
1724
1747
  this.setPending(true);
1725
- const context = this;
1726
- const defer = Deferred();
1727
1748
  this.params.data = data;
1728
- this.request('api.checkout.pay', 'methods', this.params.data, function (model) {
1729
- context.setPending(false);
1730
- context.setPayload(model.serialize());
1731
- defer.resolve(model);
1732
- }, function (model) {
1733
- context.setPending(false);
1734
- context.trigger('error', model);
1735
- defer.reject(model);
1736
- });
1737
- 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;
1738
1761
  },
1739
1762
  isPending() {
1740
1763
  return this.pendingState === true;
1741
1764
  },
1742
1765
  setPending(state) {
1743
- this.pendingState = state;
1744
- clearTimeout(this.pendingTimeoutEvent);
1745
- this.pendingTimeoutEvent = setTimeout(function (context) {
1746
- context.trigger('pending', state);
1747
- }, 50, this);
1766
+ if (this.pendingState !== state) {
1767
+ this.pendingState = state;
1768
+ this.trigger('pending', state);
1769
+ }
1748
1770
  },
1749
1771
  before() {
1750
1772
  if (this.isPending()) return;
@@ -1854,9 +1876,12 @@ const PaymentElement = Module.extend({
1854
1876
  },
1855
1877
  init(params) {
1856
1878
  this.state = {
1857
- isSupported: false,
1858
- isAllowed: false,
1859
- isMounted: false
1879
+ type: 'hide',
1880
+ transition: false,
1881
+ pending: false,
1882
+ mounted: false,
1883
+ supported: false,
1884
+ allowed: false
1860
1885
  };
1861
1886
  this.params = {};
1862
1887
  this.utils.extend(this.params, this.defaults);
@@ -1866,7 +1891,7 @@ const PaymentElement = Module.extend({
1866
1891
  timeout(callback, timeout) {
1867
1892
  this.timeoutMap = this.timeoutMap || {};
1868
1893
  clearTimeout(this.timeoutMap[timeout]);
1869
- this.timeoutMap[callback] = setTimeout(callback, timeout);
1894
+ this.timeoutMap[callback] = setTimeout(this.proxy(callback), timeout);
1870
1895
  return this;
1871
1896
  },
1872
1897
  getElementUrl() {
@@ -1915,13 +1940,13 @@ const PaymentElement = Module.extend({
1915
1940
  },
1916
1941
  onEvent(cx, ev) {
1917
1942
  ev.preventDefault();
1918
- if (this.pending) return false;
1943
+ if (this.state.pending) return false;
1919
1944
  this.send('event', {
1920
1945
  type: ev.type
1921
1946
  });
1922
1947
  },
1923
1948
  onClick() {
1924
- if (this.pending) return false;
1949
+ if (this.state.pending) return false;
1925
1950
  this.request.before().done(this.proxy('onClickDone')).fail(this.proxy('onClickFail'));
1926
1951
  },
1927
1952
  onClickDone() {
@@ -1929,19 +1954,19 @@ const PaymentElement = Module.extend({
1929
1954
  },
1930
1955
  onClickFail() {},
1931
1956
  onSupported(cx, supported) {
1932
- this.state.isSupported = supported.provider.includes(this.params.method);
1957
+ this.state.supported = supported.provider.includes(this.params.method);
1933
1958
  this.render();
1934
1959
  },
1935
1960
  onPayload(cx, payload) {
1936
- this.state.isAllowed = payload.allowed.includes(this.params.method);
1961
+ this.state.allowed = payload.allowed.includes(this.params.method);
1937
1962
  this.render();
1938
1963
  },
1939
1964
  onPending(cx, state) {
1940
- this.pending = state;
1965
+ this.state.pending = state;
1941
1966
  this.addCss(this.element, {
1942
1967
  transition: 'height 0.2s ease-out, opacity 0.4s ease-out',
1943
- pointerEvents: this.pending ? 'none' : '',
1944
- opacity: this.pending ? '0.5' : '1'
1968
+ pointerEvents: this.state.pending ? 'none' : '',
1969
+ opacity: this.state.pending ? '0.5' : '1'
1945
1970
  });
1946
1971
  },
1947
1972
  initEvents() {
@@ -1953,10 +1978,10 @@ const PaymentElement = Module.extend({
1953
1978
  },
1954
1979
  setPaymentRequest(request) {
1955
1980
  if (!(request instanceof PaymentRequestApi)) throw Error('request is not instance of PaymentRequestApi');
1956
- this.request = request;
1957
1981
  const onSupported = this.proxy('onSupported');
1958
1982
  const onPayload = this.proxy('onPayload');
1959
1983
  const onPending = this.proxy('onPending');
1984
+ this.request = request;
1960
1985
  this.request.off('supported', onSupported).on('supported', onSupported);
1961
1986
  this.request.off('payload', onPayload).on('payload', onPayload);
1962
1987
  this.request.off('pending', onPending).on('pending', onPending);
@@ -1969,50 +1994,93 @@ const PaymentElement = Module.extend({
1969
1994
  return this;
1970
1995
  },
1971
1996
  render() {
1972
- if (this.state.isSupported === false) return this;
1973
- 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();
1974
1999
  this.mount();
1975
2000
  this.show();
1976
2001
  },
2002
+ notMounted() {
2003
+ return this.state.mounted === false;
2004
+ },
2005
+ isMounted() {
2006
+ return this.state.mounted === true;
2007
+ },
1977
2008
  unmount() {
1978
2009
  if (this.element.parentNode) {
1979
2010
  this.element.parentNode.removeChild(this.element);
2011
+ this.state.mounted = false;
1980
2012
  }
1981
2013
  return this;
1982
2014
  },
1983
2015
  mount() {
1984
- if (document.body.contains(this.element) === false) {
2016
+ if (!document.body.contains(this.element)) {
1985
2017
  this.appendTo(this.params.appendTo);
2018
+ this.state.mounted = true;
1986
2019
  }
1987
2020
  return this;
1988
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
+ },
1989
2037
  show() {
1990
- this.timeout(() => {
1991
- this.addCss(this.iframe, {
1992
- transition: 'opacity 0.6s 0.4s ease-out',
1993
- opacity: this.utils.cssUnit(1)
1994
- });
1995
- this.addCss(this.element, {
1996
- transition: 'height 0.2s ease-out',
1997
- height: this.utils.cssUnit(this.params.height, 'px')
1998
- });
1999
- this.trigger('show', {});
2000
- }, 25);
2038
+ if (this.notMounted()) return;
2039
+ this.timeout('showCallback', 25);
2001
2040
  return this;
2002
2041
  },
2003
2042
  hide() {
2004
- this.timeout(() => {
2005
- this.addCss(this.iframe, {
2006
- transition: 'opacity 0.4s ease-out',
2007
- opacity: this.utils.cssUnit(0)
2008
- });
2009
- this.addCss(this.element, {
2010
- transition: 'height 0.2s 0.4s ease-out',
2011
- height: this.utils.cssUnit(0, 'px')
2012
- });
2013
- this.trigger('hide', {});
2014
- }, 25);
2043
+ if (this.notMounted()) return;
2044
+ this.timeout('hideCallback', 25);
2015
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();
2016
2084
  }
2017
2085
  });
2018
2086
 
@@ -2082,16 +2150,21 @@ const PaymentButton = Module.extend({
2082
2150
  });
2083
2151
  this.request.setApi(this.api);
2084
2152
  this.request.setMerchant(this.params.data.merchant_id);
2153
+ this.request.on('pending', this.proxy('onPending'));
2085
2154
  this.request.on('details', this.proxy('onDetails'));
2086
2155
  this.request.on('error', this.proxy('onError'));
2156
+ this.request.on('show', this.proxy('onShow'));
2157
+ this.request.on('hide', this.proxy('onHide'));
2087
2158
  },
2088
2159
  initElements() {
2160
+ const self = this;
2089
2161
  const style = this.params.style;
2090
2162
  const data = this.params.data;
2091
2163
  const origin = this.params.origin;
2092
2164
  const appendTo = this.params.element;
2093
2165
  const endpoint = this.params.endpoint.element;
2094
2166
  const request = this.request;
2167
+ this.buttons = [];
2095
2168
  this.container = this.utils.querySelector(this.params.element);
2096
2169
  this.addCss(this.container, {
2097
2170
  display: 'flex',
@@ -2110,24 +2183,51 @@ const PaymentButton = Module.extend({
2110
2183
  height: style.height
2111
2184
  });
2112
2185
  element.setPaymentRequest(request);
2186
+ self.buttons.push(element);
2113
2187
  });
2114
2188
  request.getSupportedMethods();
2115
2189
  },
2116
2190
  update(data) {
2117
2191
  return this.request.update(this.utils.extend(this.params.data, data || {}));
2118
2192
  },
2119
- onDetails(cx, data) {
2193
+ onDetails(_cx, data) {
2120
2194
  this.api.scope(() => {
2121
2195
  this.request.after(this.params.data).done(extendParams => {
2122
2196
  this.api.request('api.checkout.form', 'request', this.utils.extend({}, this.params.data, extendParams || {}, data)).done(this.proxy('onSuccess')).fail(this.proxy('onError'));
2123
2197
  });
2124
2198
  });
2125
2199
  },
2126
- onSuccess(cx, data) {
2200
+ onSuccess(_cx, data) {
2127
2201
  this.trigger('success', data);
2128
2202
  },
2129
- onError(cx, data) {
2203
+ onError(_cx, data) {
2130
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);
2131
2231
  }
2132
2232
  });
2133
2233