@paystack/inline-js 2.23.0-dev.1 → 2.23.0-dev.2

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/lib/inline.js CHANGED
@@ -16,37 +16,6 @@ function _arrayWithoutHoles(r) {
16
16
  if (Array.isArray(r)) return _arrayLikeToArray(r);
17
17
  }
18
18
 
19
- function asyncGeneratorStep(n, t, e, r, o, a, c) {
20
- try {
21
- var i = n[a](c),
22
- u = i.value;
23
- } catch (n) {
24
- return void e(n);
25
- }
26
-
27
- i.done ? t(u) : Promise.resolve(u).then(r, o);
28
- }
29
-
30
- function _asyncToGenerator(n) {
31
- return function () {
32
- var t = this,
33
- e = arguments;
34
- return new Promise(function (r, o) {
35
- var a = n.apply(t, e);
36
-
37
- function _next(n) {
38
- asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
39
- }
40
-
41
- function _throw(n) {
42
- asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
43
- }
44
-
45
- _next(void 0);
46
- });
47
- };
48
- }
49
-
50
19
  function _classCallCheck(a, n) {
51
20
  if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
52
21
  }
@@ -402,9 +371,9 @@ var defaultCustomApplePayStyles = {
402
371
  height: '50px',
403
372
  width: 'auto',
404
373
  borderRadius: '3px',
405
- padding: '10px',
374
+ padding: '0px',
406
375
  locale: 'en',
407
- type: 'pay'
376
+ type: 'plain'
408
377
  };
409
378
  var parseApplePayCustomStyles = function parseApplePayCustomStyles(styles) {
410
379
  if (!(styles && _typeof(styles) === 'object')) {
@@ -434,6 +403,193 @@ var generateApplePayFallbackCSS = function generateApplePayFallbackCSS(_ref2) {
434
403
  };
435
404
  var preCheckoutModalCSS = "\n .pre-checkout-modal {\n display: none;\n position: fixed;\n z-index: 1;\n left: 0;\n top: 0;\n width: 100vw;\n height: 100%;\n overflow: auto;\n background-color: rgba(0, 0, 0, 0.75);\n transition: all 0.2s ease;\n }\n\n .pre-checkout-modal.show {\n display: block;\n }\n\n .pre-checkout-modal__content {\n position: absolute;\n bottom: 0;\n left: 0;\n right: 0;\n margin-left: auto;\n margin-right: auto;\n background-color: #fefefe;\n padding: 20px;\n padding-bottom: max(30px, env(safe-area-inset-bottom));\n width: 100%;\n border-radius: 6px 6px 0 0;\n display: flex;\n flex-direction: column;\n align-items: center;\n max-width: 350px;\n\n box-sizing: border-box;\n transform: translateY(238px);\n transition: transform 0.3s cubic-bezier(.16,.81,.32,1);\n }\n\n .modal-wrapper {\n width: 100%;\n }\n\n .payment-info {\n position: relative;\n padding-bottom: 15px;\n border-bottom: solid 1px whitesmoke;\n display: flex;\n align-items: flex-start;\n justify-content: space-between;\n width: 100%;\n font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", \"Roboto\", \"Oxygen\", \"Ubuntu\",\n \"Cantarell\", \"Fira Sans\", \"Droid Sans\", \"Helvetica Neue\", sans-serif;\n }\n\n .customer-email {\n color: #737373;\n font-size: 13px;\n line-height: 16px;\n }\n\n .customer-info {\n flex: 1;\n text-align: right;\n }\n\n .merchant-logo {\n display: flex;\n align-items: center;\n height: 30px;\n }\n .transaction-amount {\n margin-top: 5px;\n font-size: 13px;\n line-height: 16px;\n color: #737373;\n }\n\n .amount {\n color: #29b263;\n font-weight: bold;\n }\n\n @media only screen and (min-width: 500px) {\n .pre-checkout-modal__content {\n bottom: 0;\n top: 0;\n margin: auto;\n border-radius: 6px;\n height: fit-content;\n }\n }\n\n .pre-checkout-modal__content.show {\n transform: translateY(0);\n margin: 0 auto;\n margin-top: 100px;\n }\n\n .pre-checkout-modal__content > * {\n margin-top: 0;\n margin-bottom: 40px;\n }\n .pre-checkout-modal__content > *:last-child {\n margin-bottom: 0;\n }\n\n .pre-checkout-modal__content svg {\n margin: auto;\n width: 100%;\n }\n\n #inline-button-wordmark--white {\n position: absolute;\n bottom: -50px;\n margin: auto;\n left: 0;\n right: 0;\n width: fit-content;\n }\n\n #inline-button-wordmark--grey {\n display: none;\n }\n\n .pre-checkout-modal__content #apple-pay-mark--light {\n margin-bottom: 16px;\n }\n\n .pre-checkout-modal p {\n -webkit-text-size-adjust: 100%;\n -webkit-font-smoothing: antialiased;\n -moz-osx-font-smoothing: grayscale;\n text-rendering: optimizeLegibility;\n font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", \"Roboto\", \"Oxygen\", \"Ubuntu\",\n \"Cantarell\", \"Fira Sans\", \"Droid Sans\", \"Helvetica Neue\", sans-serif;\n color: #4E4E4E;\n line-height: 140%;\n font-size: 14px;\n font-weight: 500;\n margin: 0;\n padding: 0 20px;\n text-align: center;\n letter-spacing: -0.3px;\n }\n\n .pre-checkout-modal button {\n height: 42px;\n width: 100%;\n \n box-sizing: border-box;\n border-radius: 3px;\n font-size: 14px;\n line-height: 24px;\n cursor: pointer;\n -webkit-text-size-adjust: 100%;\n -webkit-font-smoothing: antialiased;\n -moz-osx-font-smoothing: grayscale;\n text-rendering: optimizeLegibility;\n font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", \"Roboto\", \"Oxygen\", \"Ubuntu\",\n \"Cantarell\", \"Fira Sans\", \"Droid Sans\", \"Helvetica Neue\", sans-serif;\n }\n\n .pre-checkout-modal .open-paystack-pop-button {\n background: #FAFAFA;\n border: 1px solid #F2F3F3;\n color: #4E4E4E;\n font-weight: 500;\n }\n\n .pre-checkout-modal .open-paystack-pop-button:hover, \n .pre-checkout-modal .open-paystack-pop-button:active, \n .pre-checkout-modal .open-paystack-pop-button:focus {\n background: #F2F3F3;\n }\n\n .pre-checkout-modal .pay-with-vault-button {\n font-weight: 700;\n background: #44b669;\n background: linear-gradient(to bottom, #44b669 0%, #40ad57 100%);\n border: solid 1px #49a861;\n text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.1);\n outline: none;\n color: white;\n transition: all 300ms;\n }\n\n .pre-checkout-modal .vault-instruction {\n color: #2f3d4d;\n font-size: 14px;\n letter-spacing: normal;\n line-height: 1.4;\n margin: 0 auto 24px;\n padding: 0;\n }\n .vault-logo-container {\n width: 74px;\n height: 20px;\n margin: 0 auto 24px\n }\n .vault-logo-container img {\n height: 100%;\n width: 100%;\n border-radius: 8px;\n }\n .vault-divider {\n margin-bottom: 16px;\n margin-top: 24px;\n position: relative;\n }\n .vault-divider__container {\n align-items: center;\n bottom: 0;\n display: flex;\n left: 0;\n position: absolute;\n right: 0;\n top: 0;\n }\n .vault-divider__line {\n border: 1px dashed #ccced0;\n width: 100%;\n }\n .vault-divider__text-container {\n display: flex;\n justify-content: center;\n position: relative;\n }\n .vault-divider__text {\n background-color: #fff;\n color: #999da1;\n font-size: 14px;\n font-weight: 500;\n letter-spacing: -.3px;\n line-height: 19.6px;\n margin-bottom: 2px;\n padding: 0 8px;\n }\n\n #payment-request-button {\n width: 100%;\n height: fit-content;\n margin: 24px 0 16px 0;\n }\n\n #paystackpop-button {\n padding: 0 16px;\n }\n\n #apple-pay-close-button {\n position: absolute;\n text-align: center;\n top: 0;\n right: -26px;\n height: 16px;\n width: 16px;\n padding: 0;\n display: inline-block;\n z-index: 3;\n border-radius: 50%;\n background: transparent;\n transition: all 300ms;\n outline: none;\n cursor: pointer;\n border: none;\n }\n\n #apple-pay-close-button svg {\n width: initial;\n }\n \n #apple-pay-close-button:hover {\n background-color: #e22b28;\n }\n\n @media only screen and (max-width: 500px) {\n .pre-checkout-modal__content {\n max-width: 500px;\n border-radius: 0;\n padding-bottom: 0;\n }\n\n .modal-wrapper {\n padding: 0;\n }\n\n .vault-logo-container {\n width: 74px;\n height: 20px;\n }\n\n #inline-button-wordmark--white {\n display: none\n }\n \n #inline-button-wordmark--grey {\n display: block;\n width: 100%;\n margin: 16px 0;\n height: 13px;\n }\n\n #apple-pay-close-button {\n display: none;\n }\n }\n";
436
405
 
406
+ var amountSubunitToBase = function amountSubunitToBase() {
407
+ var amount = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
408
+ return Number(parseFloat(amount / 100).toFixed(2));
409
+ };
410
+
411
+ var requestConfig = {
412
+ headers: {
413
+ accept: 'application/json, text/plain, */*',
414
+ 'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
415
+ 'content-type': 'application/x-www-form-urlencoded',
416
+ 'sec-ch-ua-mobile': '?0',
417
+ 'sec-fetch-dest': 'empty',
418
+ 'sec-fetch-mode': 'cors',
419
+ 'sec-fetch-site': 'cross-site'
420
+ },
421
+ referrerPolicy: 'no-referrer-when-downgrade',
422
+ method: 'POST',
423
+ mode: 'cors',
424
+ credentials: 'omit'
425
+ };
426
+ function createFormBody(params) {
427
+ var formBody = Object.keys(params).reduce(function (formBodyArray, key) {
428
+ var encodedKey = encodeURIComponent(key);
429
+ var encodedValue = encodeURIComponent(params[key]);
430
+ var encodedFormBody = "".concat(encodedKey, "=").concat(encodedValue);
431
+ return [].concat(_toConsumableArray(formBodyArray), [encodedFormBody]);
432
+ }, []);
433
+ return formBody.join('&');
434
+ }
435
+ var APPLE_PAY_CAPABILITY_STATUS = {
436
+ AVAILABLE: 'paymentCredentialsAvailable',
437
+ UNAVAILABLE: 'paymentCredentialsUnavailable',
438
+ UNKNOWN: 'paymentCredentialStatusUnknown',
439
+ UNSUPPORTED: 'applePayUnsupported'
440
+ };
441
+
442
+ var formatIntervalString = function formatIntervalString(interval) {
443
+ var plans = {
444
+ biannually: 'BIANNUAL PLAN',
445
+ annually: 'ANNUAL PLAN'
446
+ };
447
+ return plans[interval] || "".concat(interval.toUpperCase(), " PLAN");
448
+ };
449
+
450
+ var generateApplePaymentRequestObject = function generateApplePaymentRequestObject(_ref) {
451
+ var currency = _ref.currency,
452
+ amount = _ref.amount,
453
+ merchantName = _ref.merchantName,
454
+ interval = _ref.interval;
455
+ return _objectSpread2({
456
+ countryCode: 'NG',
457
+ currencyCode: currency,
458
+ merchantCapabilities: ['supports3DS', 'supportsCredit', 'supportsDebit'],
459
+ supportedNetworks: ['visa', 'masterCard'],
460
+ requiredBillingContactFields: ['postalAddress', 'name', 'phone', 'email'],
461
+ total: {
462
+ label: "".concat(merchantName, " - Paystack"),
463
+ type: 'final',
464
+ amount: String(amountSubunitToBase(amount))
465
+ }
466
+ }, typeof interval === 'string' && interval.trim() !== '' && {
467
+ lineItems: [{
468
+ label: formatIntervalString(interval),
469
+ amount: String(amountSubunitToBase(amount))
470
+ }]
471
+ });
472
+ };
473
+ var validateApplePaySession = function validateApplePaySession(_ref2) {
474
+ var transactionId = _ref2.transactionId,
475
+ validationURL = _ref2.validationURL,
476
+ merchantName = _ref2.merchantName,
477
+ _ref2$domainName = _ref2.domainName,
478
+ domainName = _ref2$domainName === void 0 ? window && window.location && window.location.hostname : _ref2$domainName;
479
+ var validateSessionUrl = "".concat(config.paymentBaseUrl).concat(config.applePayValidateSessionPath);
480
+ var params = {
481
+ transaction: transactionId,
482
+ sessionUrl: validationURL,
483
+ displayName: merchantName,
484
+ domainName: domainName
485
+ };
486
+ var formBody = createFormBody(params);
487
+ var validateSession = fetch(validateSessionUrl, _objectSpread2(_objectSpread2({}, requestConfig), {}, {
488
+ body: formBody
489
+ })).then(function (response) {
490
+ return response.json();
491
+ });
492
+ return validateSession;
493
+ };
494
+ var chargeApplePay = function chargeApplePay(_ref3) {
495
+ var transactionId = _ref3.transactionId,
496
+ payment = _ref3.payment;
497
+ var chargeApplePayUrl = "".concat(config.paymentBaseUrl).concat(config.applePayChargePath);
498
+ var params = {
499
+ transaction: transactionId,
500
+ paymentObject: JSON.stringify(payment)
501
+ };
502
+ var formBody = createFormBody(params);
503
+ var charge = fetch(chargeApplePayUrl, _objectSpread2(_objectSpread2({}, requestConfig), {}, {
504
+ body: formBody
505
+ })).then(function (response) {
506
+ return response.json();
507
+ });
508
+ return charge;
509
+ };
510
+ var deviceSupportsApplePay = function deviceSupportsApplePay() {
511
+ try {
512
+ return !!(window.ApplePaySession && window.ApplePaySession.supportsVersion(config.applePayVersion));
513
+ } catch (error) {
514
+ return false;
515
+ }
516
+ };
517
+
518
+ var getApplePayCapabilityStatusFromResponse = function getApplePayCapabilityStatusFromResponse(capabilities) {
519
+ if (!capabilities) {
520
+ return null;
521
+ }
522
+
523
+ if (typeof capabilities === 'string') {
524
+ return capabilities;
525
+ }
526
+
527
+ if (typeof capabilities.paymentCredentialStatus === 'string') {
528
+ return capabilities.paymentCredentialStatus;
529
+ }
530
+
531
+ return null;
532
+ };
533
+
534
+ var getApplePayCapabilityStatus = function getApplePayCapabilityStatus() {
535
+ try {
536
+ if (!deviceSupportsApplePay()) {
537
+ return Promise.resolve(null);
538
+ }
539
+
540
+ var _window = window,
541
+ ApplePaySession = _window.ApplePaySession;
542
+
543
+ if (typeof ApplePaySession.applePayCapabilities === 'function') {
544
+ return Promise.resolve(ApplePaySession.applePayCapabilities()).then(function (capabilities) {
545
+ return getApplePayCapabilityStatusFromResponse(capabilities);
546
+ }).catch(function () {
547
+ try {
548
+ if (window.ApplePaySession.canMakePayments()) {
549
+ return APPLE_PAY_CAPABILITY_STATUS.UNKNOWN;
550
+ }
551
+ } catch (fallbackError) {
552
+ return null;
553
+ }
554
+
555
+ return APPLE_PAY_CAPABILITY_STATUS.UNSUPPORTED;
556
+ });
557
+ }
558
+
559
+ if (typeof ApplePaySession.canMakePayments === 'function') {
560
+ return Promise.resolve(ApplePaySession.canMakePayments()).then(function (canMakePayments) {
561
+ return canMakePayments ? APPLE_PAY_CAPABILITY_STATUS.UNKNOWN : APPLE_PAY_CAPABILITY_STATUS.UNSUPPORTED;
562
+ });
563
+ }
564
+
565
+ return Promise.resolve(null);
566
+ } catch (error) {
567
+ try {
568
+ if (window.ApplePaySession.canMakePayments()) {
569
+ return Promise.resolve(APPLE_PAY_CAPABILITY_STATUS.UNKNOWN);
570
+ }
571
+ } catch (fallbackError) {
572
+ return Promise.resolve(null);
573
+ }
574
+
575
+ return Promise.resolve(APPLE_PAY_CAPABILITY_STATUS.UNSUPPORTED);
576
+ }
577
+ };
578
+ var applePayCapabilityCanShowButton = function applePayCapabilityCanShowButton(status) {
579
+ return [APPLE_PAY_CAPABILITY_STATUS.AVAILABLE, APPLE_PAY_CAPABILITY_STATUS.UNKNOWN].includes(status);
580
+ };
581
+ var canUseApplePay = function canUseApplePay(channels) {
582
+ var availableChannels = channels || [];
583
+
584
+ if (!availableChannels.includes('apple_pay')) {
585
+ return Promise.resolve(false);
586
+ }
587
+
588
+ return getApplePayCapabilityStatus().then(function (capabilityStatus) {
589
+ return applePayCapabilityCanShowButton(capabilityStatus);
590
+ });
591
+ };
592
+
437
593
  /* eslint-env browser */
438
594
  function findHighestZIndex() {
439
595
  var highestZIndex = 0;
@@ -493,6 +649,7 @@ function mountEmbedCheckoutIframe(id, src) {
493
649
  function getApplePayButton(container) {
494
650
  return container.querySelector('apple-pay-button') || container.querySelector('#apple-pay-button');
495
651
  }
652
+ var applePaySdkPromise;
496
653
  var loadScript = function loadScript(url, _ref) {
497
654
  var container = _ref.container,
498
655
  integrity = _ref.integrity;
@@ -524,6 +681,31 @@ var loadScript = function loadScript(url, _ref) {
524
681
  }
525
682
  });
526
683
  };
684
+
685
+ var applePayButtonComponentIsAvailable = function applePayButtonComponentIsAvailable() {
686
+ return !!(window && window.customElements && window.customElements.get('apple-pay-button'));
687
+ };
688
+
689
+ var ensureApplePaySdkLoaded = function ensureApplePaySdkLoaded(container) {
690
+ if (applePayButtonComponentIsAvailable()) {
691
+ return Promise.resolve(true);
692
+ }
693
+
694
+ if (applePaySdkPromise) {
695
+ return applePaySdkPromise;
696
+ }
697
+
698
+ applePaySdkPromise = loadScript(APPLE_PAY_SDK_SOURCE, {
699
+ container: container,
700
+ integrity: APPLE_PAY_SDK_INTEGRITY_HASH
701
+ }).then(function () {
702
+ return true;
703
+ }).catch(function (error) {
704
+ applePaySdkPromise = null;
705
+ throw error;
706
+ });
707
+ return applePaySdkPromise;
708
+ };
527
709
  function appendIframeToContainer(containerId, iframe) {
528
710
  var container = document.getElementById(containerId);
529
711
  container.innerHTML = '';
@@ -674,7 +856,7 @@ function mountApplePayFallbackButton(container, _ref3) {
674
856
  function getPaymentRequestContainer(container) {
675
857
  return document.querySelector("#".concat(container));
676
858
  }
677
- function mountPaymentRequestButtons(container, _ref4, merchantId) {
859
+ function mountPaymentRequestButtons(container, _ref4) {
678
860
  var _ref4$channels = _ref4.channels,
679
861
  channels = _ref4$channels === void 0 ? [] : _ref4$channels,
680
862
  _ref4$styles = _ref4.styles,
@@ -682,92 +864,45 @@ function mountPaymentRequestButtons(container, _ref4, merchantId) {
682
864
  var mountedElements = {
683
865
  applePay: false
684
866
  };
685
- return new Promise(function (resolve, reject) {
686
- if (container) {
687
- if (!channels.includes('apple_pay')) {
688
- reject('No wallet payment method is available on this device');
689
- return;
690
- }
691
-
692
- if (getApplePayButton(container)) {
693
- mountedElements.applePay = true;
694
- resolve(mountedElements);
695
- return;
696
- }
697
-
698
- loadScript(APPLE_PAY_SDK_SOURCE, {
699
- container: container,
700
- integrity: APPLE_PAY_SDK_INTEGRITY_HASH
701
- }).then( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
702
- var _window$ApplePaySessi, capabilities, unsupported, _window$ApplePaySessi2, _window$ApplePaySessi3;
703
-
704
- return regeneratorRuntime.wrap(function _callee$(_context) {
705
- while (1) {
706
- switch (_context.prev = _context.next) {
707
- case 0:
708
- _context.prev = 0;
709
- _context.next = 3;
710
- return (_window$ApplePaySessi = window.ApplePaySession) === null || _window$ApplePaySessi === void 0 ? void 0 : _window$ApplePaySessi.applePayCapabilities();
711
-
712
- case 3:
713
- capabilities = _context.sent;
714
- unsupported = (capabilities === null || capabilities === void 0 ? void 0 : capabilities.paymentCredentialStatus) === 'applePayUnsupported';
715
-
716
- if (!unsupported) {
717
- _context.next = 7;
718
- break;
719
- }
720
-
721
- throw new Error('Apple Pay is not supported on this device');
722
-
723
- case 7:
724
- _context.next = 15;
725
- break;
726
-
727
- case 9:
728
- _context.prev = 9;
729
- _context.t0 = _context["catch"](0);
730
867
 
731
- if (!(_context.t0.message === 'Apple Pay is not supported on this device')) {
732
- _context.next = 13;
733
- break;
734
- }
735
-
736
- throw _context.t0;
868
+ if (!container) {
869
+ return Promise.reject(new Error('Container to mount elements was not provided'));
870
+ }
737
871
 
738
- case 13:
739
- if ((_window$ApplePaySessi2 = window.ApplePaySession) !== null && _window$ApplePaySessi2 !== void 0 && (_window$ApplePaySessi3 = _window$ApplePaySessi2.canMakePayments) !== null && _window$ApplePaySessi3 !== void 0 && _window$ApplePaySessi3.call(_window$ApplePaySessi2)) {
740
- _context.next = 15;
741
- break;
742
- }
872
+ if (!channels.includes('apple_pay')) {
873
+ return Promise.reject(new Error('No wallet payment method is available on this device'));
874
+ }
743
875
 
744
- throw new Error('Apple Pay is not supported on this device');
876
+ return ensureApplePaySdkLoaded(container).catch(function () {
877
+ // Safari can still render the fallback button using the native ApplePaySession.
878
+ return null;
879
+ }).then(function () {
880
+ return canUseApplePay(channels);
881
+ }).then(function (applePayAvailable) {
882
+ if (!applePayAvailable) {
883
+ throw new Error('No wallet payment method is available on this device');
884
+ }
745
885
 
746
- case 15:
747
- mountApplePayButton(container, {
748
- styles: parseApplePayCustomStyles(styles.applePay),
749
- theme: styles.theme
750
- });
751
- mountedElements.applePay = true;
752
- resolve(mountedElements);
886
+ if (getApplePayButton(container)) {
887
+ mountedElements.applePay = true;
888
+ return mountedElements;
889
+ }
753
890
 
754
- case 18:
755
- case "end":
756
- return _context.stop();
757
- }
758
- }
759
- }, _callee, null, [[0, 9]]);
760
- }))).catch(function () {
761
- mountApplePayFallbackButton(container, {
762
- styles: parseApplePayCustomStyles(styles.applePay),
763
- theme: styles.theme
764
- });
765
- mountedElements.applePay = true;
766
- resolve(mountedElements);
891
+ try {
892
+ mountApplePayButton(container, {
893
+ styles: parseApplePayCustomStyles(styles.applePay),
894
+ theme: styles.theme
895
+ });
896
+ } catch (error) {
897
+ // If the web component is unavailable, fall back to the CSS-based Apple Pay button.
898
+ mountApplePayFallbackButton(container, {
899
+ styles: parseApplePayCustomStyles(styles.applePay),
900
+ theme: styles.theme
767
901
  });
768
- } else {
769
- reject('Container to mount elements was not provided');
770
902
  }
903
+
904
+ mountedElements.applePay = true;
905
+ return mountedElements;
771
906
  });
772
907
  }
773
908
  function removeAllChildNodes(parent) {
@@ -848,8 +983,8 @@ function vaultPreCheckoutModalView(container) {
848
983
  }
849
984
  }
850
985
 
851
- function defaultPreCheckoutModalView(container, _ref6) {
852
- var hasOtherPaymentMethods = _ref6.hasOtherPaymentMethods;
986
+ function defaultPreCheckoutModalView(container, _ref5) {
987
+ var hasOtherPaymentMethods = _ref5.hasOtherPaymentMethods;
853
988
  var applePayIcon = document.createElement('div');
854
989
  applePayIcon.innerHTML = applePayMarkLight;
855
990
  container.appendChild(applePayIcon);
@@ -892,14 +1027,14 @@ function mountPreCheckoutModal(id, transaction) {
892
1027
  content.classList.add('pre-checkout-modal__content');
893
1028
  modal.appendChild(content);
894
1029
 
895
- var _ref7 = transaction || {},
896
- merchantLogo = _ref7.merchant_logo,
897
- merchantName = _ref7.merchant_name,
898
- email = _ref7.email,
899
- amount = _ref7.amount,
900
- currency = _ref7.currency,
901
- label = _ref7.label,
902
- channels = _ref7.channels;
1030
+ var _ref6 = transaction || {},
1031
+ merchantLogo = _ref6.merchant_logo,
1032
+ merchantName = _ref6.merchant_name,
1033
+ email = _ref6.email,
1034
+ amount = _ref6.amount,
1035
+ currency = _ref6.currency,
1036
+ label = _ref6.label,
1037
+ channels = _ref6.channels;
903
1038
 
904
1039
  var formattedAmount = new Intl.NumberFormat('en', {
905
1040
  style: 'currency',
@@ -1712,116 +1847,6 @@ var InlineTransaction = /*#__PURE__*/function () {
1712
1847
  return InlineTransaction;
1713
1848
  }();
1714
1849
 
1715
- var amountSubunitToBase = function amountSubunitToBase() {
1716
- var amount = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
1717
- return Number(parseFloat(amount / 100).toFixed(2));
1718
- };
1719
-
1720
- var requestConfig = {
1721
- headers: {
1722
- accept: 'application/json, text/plain, */*',
1723
- 'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
1724
- 'content-type': 'application/x-www-form-urlencoded',
1725
- 'sec-ch-ua-mobile': '?0',
1726
- 'sec-fetch-dest': 'empty',
1727
- 'sec-fetch-mode': 'cors',
1728
- 'sec-fetch-site': 'cross-site'
1729
- },
1730
- referrerPolicy: 'no-referrer-when-downgrade',
1731
- method: 'POST',
1732
- mode: 'cors',
1733
- credentials: 'omit'
1734
- };
1735
- function createFormBody(params) {
1736
- var formBody = Object.keys(params).reduce(function (formBodyArray, key) {
1737
- var encodedKey = encodeURIComponent(key);
1738
- var encodedValue = encodeURIComponent(params[key]);
1739
- var encodedFormBody = "".concat(encodedKey, "=").concat(encodedValue);
1740
- return [].concat(_toConsumableArray(formBodyArray), [encodedFormBody]);
1741
- }, []);
1742
- return formBody.join('&');
1743
- }
1744
-
1745
- var formatIntervalString = function formatIntervalString(interval) {
1746
- var plans = {
1747
- biannually: 'BIANNUAL PLAN',
1748
- annually: 'ANNUAL PLAN'
1749
- };
1750
- return plans[interval] || "".concat(interval.toUpperCase(), " PLAN");
1751
- };
1752
-
1753
- var generateApplePaymentRequestObject = function generateApplePaymentRequestObject(_ref) {
1754
- var currency = _ref.currency,
1755
- amount = _ref.amount,
1756
- merchantName = _ref.merchantName,
1757
- interval = _ref.interval;
1758
- return _objectSpread2({
1759
- countryCode: 'NG',
1760
- currencyCode: currency,
1761
- merchantCapabilities: ['supports3DS', 'supportsCredit', 'supportsDebit'],
1762
- supportedNetworks: ['visa', 'masterCard'],
1763
- requiredBillingContactFields: ['postalAddress', 'name', 'phone', 'email'],
1764
- total: {
1765
- label: "".concat(merchantName, " - Paystack"),
1766
- type: 'final',
1767
- amount: String(amountSubunitToBase(amount))
1768
- }
1769
- }, typeof interval === 'string' && interval.trim() !== '' && {
1770
- lineItems: [{
1771
- label: formatIntervalString(interval),
1772
- amount: String(amountSubunitToBase(amount))
1773
- }]
1774
- });
1775
- };
1776
- var validateApplePaySession = function validateApplePaySession(_ref2) {
1777
- var transactionId = _ref2.transactionId,
1778
- validationURL = _ref2.validationURL,
1779
- merchantName = _ref2.merchantName,
1780
- _ref2$domainName = _ref2.domainName,
1781
- domainName = _ref2$domainName === void 0 ? window && window.location && window.location.hostname : _ref2$domainName;
1782
- var validateSessionUrl = "".concat(config.paymentBaseUrl).concat(config.applePayValidateSessionPath);
1783
- var params = {
1784
- transaction: transactionId,
1785
- sessionUrl: validationURL,
1786
- displayName: merchantName,
1787
- domainName: domainName
1788
- };
1789
- var formBody = createFormBody(params);
1790
- var validateSession = fetch(validateSessionUrl, _objectSpread2(_objectSpread2({}, requestConfig), {}, {
1791
- body: formBody
1792
- })).then(function (response) {
1793
- return response.json();
1794
- });
1795
- return validateSession;
1796
- };
1797
- var chargeApplePay = function chargeApplePay(_ref3) {
1798
- var transactionId = _ref3.transactionId,
1799
- payment = _ref3.payment;
1800
- var chargeApplePayUrl = "".concat(config.paymentBaseUrl).concat(config.applePayChargePath);
1801
- var params = {
1802
- transaction: transactionId,
1803
- paymentObject: JSON.stringify(payment)
1804
- };
1805
- var formBody = createFormBody(params);
1806
- var charge = fetch(chargeApplePayUrl, _objectSpread2(_objectSpread2({}, requestConfig), {}, {
1807
- body: formBody
1808
- })).then(function (response) {
1809
- return response.json();
1810
- });
1811
- return charge;
1812
- };
1813
- var deviceSupportsApplePay = function deviceSupportsApplePay() {
1814
- try {
1815
- return window.location && window.location.protocol === 'https:' && window.ApplePaySession && window.ApplePaySession.supportsVersion(config.applePayVersion);
1816
- } catch (error) {
1817
- return false;
1818
- }
1819
- };
1820
- var canUseApplePay = function canUseApplePay() {
1821
- var channels = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
1822
- return deviceSupportsApplePay() && channels.includes('apple_pay');
1823
- };
1824
-
1825
1850
  var canUseVault = function canUseVault() {
1826
1851
  var _transaction$link_con, _transaction$link_con2;
1827
1852
 
@@ -1880,6 +1905,19 @@ function handleError(message) {
1880
1905
  throw new Error(message);
1881
1906
  }
1882
1907
 
1908
+ function transactionCanUseApplePay(response, container) {
1909
+ if (!(response && response.channels && response.channels.includes('apple_pay'))) {
1910
+ return Promise.resolve(false);
1911
+ }
1912
+
1913
+ return ensureApplePaySdkLoaded(container).catch(function () {
1914
+ // Native Apple Pay support can still be available even if the SDK fails to load.
1915
+ return null;
1916
+ }).then(function () {
1917
+ return canUseApplePay(response.channels);
1918
+ });
1919
+ }
1920
+
1883
1921
  var Inline = /*#__PURE__*/function () {
1884
1922
  function Inline(params) {
1885
1923
  _classCallCheck(this, Inline);
@@ -1945,7 +1983,15 @@ var Inline = /*#__PURE__*/function () {
1945
1983
  var transaction = resumableTransaction || new InlineTransaction(params);
1946
1984
  return new Promise(function (resolve, reject) {
1947
1985
  transaction.requestInline().then(function (response) {
1948
- var canPayWithApplePay = response.channels.includes('apple_pay');
1986
+ return transactionCanUseApplePay(response).then(function (canPayWithApplePay) {
1987
+ return {
1988
+ response: response,
1989
+ canPayWithApplePay: canPayWithApplePay
1990
+ };
1991
+ });
1992
+ }).then(function (_ref) {
1993
+ var response = _ref.response,
1994
+ canPayWithApplePay = _ref.canPayWithApplePay;
1949
1995
  var canPayWithVault = canUseVault(response);
1950
1996
  var shouldLoadPreCheckoutModal = canPayWithVault || canPayWithApplePay;
1951
1997
 
@@ -1983,7 +2029,7 @@ var Inline = /*#__PURE__*/function () {
1983
2029
  }
1984
2030
  }
1985
2031
  }, response.merchant_id).then(function () {
1986
- inlineInstance.registerPaymentRequestEventListeners();
2032
+ return inlineInstance.registerPaymentRequestEventListeners();
1987
2033
  }).catch(function () {
1988
2034
  /**
1989
2035
  * If an error occurs when mounting the apple pay button,
@@ -2096,6 +2142,7 @@ var Inline = /*#__PURE__*/function () {
2096
2142
 
2097
2143
  if (transactionStatus === 'failed') {
2098
2144
  showErrorInPreCheckoutModal(this.preCheckoutModal);
2145
+ animatePreCheckoutModalIn(this.preCheckoutModal);
2099
2146
  } else {
2100
2147
  hidePreCheckoutModal(this.preCheckoutModal);
2101
2148
  this.preCheckoutModal.remove();
@@ -2117,11 +2164,11 @@ var Inline = /*#__PURE__*/function () {
2117
2164
  }
2118
2165
  }, {
2119
2166
  key: "newTransaction",
2120
- value: function newTransaction(_ref) {
2121
- var preload = _ref.preload,
2122
- inlineTransaction = _ref.inlineTransaction,
2123
- transactionData = _ref.transactionData,
2124
- options = _objectWithoutProperties(_ref, _excluded);
2167
+ value: function newTransaction(_ref2) {
2168
+ var preload = _ref2.preload,
2169
+ inlineTransaction = _ref2.inlineTransaction,
2170
+ transactionData = _ref2.transactionData,
2171
+ options = _objectWithoutProperties(_ref2, _excluded);
2125
2172
 
2126
2173
  var hasPaymentRequestButtons = this.paymentRequestContainer && getApplePayButton(this.paymentRequestContainer);
2127
2174
 
@@ -2200,25 +2247,6 @@ var Inline = /*#__PURE__*/function () {
2200
2247
  return new Promise(function (resolve, reject) {
2201
2248
  var loadPaystackCheckoutButton = document.querySelector("#".concat(parameters.loadPaystackCheckoutButton));
2202
2249
 
2203
- if (!deviceSupportsApplePay()) {
2204
- if (parameters && parameters.loadPaystackCheckoutButton) {
2205
- if (loadPaystackCheckoutButton) {
2206
- var loadPopup = inlineInstance.preloadTransaction(transactionParams);
2207
- loadPaystackCheckoutButton.onclick = loadPopup;
2208
- } else {
2209
- logWarning("This device does not support any payment request wallet options. Please consult our documentation at https://developers.paystack.co/docs/paystack-inline to see how to load alternative payment options using 'loadPaystackCheckoutButton'");
2210
- }
2211
- }
2212
-
2213
- if (onElementsMount) {
2214
- onElementsMount(null);
2215
- }
2216
-
2217
- var activeTransaction = inlineInstance.activeTransaction();
2218
- resolve(activeTransaction);
2219
- return;
2220
- }
2221
-
2222
2250
  if (inlineInstance.activeTransaction()) {
2223
2251
  inlineInstance.activeTransaction().cancel();
2224
2252
  }
@@ -2252,12 +2280,11 @@ var Inline = /*#__PURE__*/function () {
2252
2280
  inlineInstance.registerPaymentRequestEventListeners();
2253
2281
 
2254
2282
  if (loadPaystackCheckoutButton) {
2255
- var _loadPopup = inlineInstance.preloadTransaction({
2283
+ var loadPopup = inlineInstance.preloadTransaction({
2256
2284
  inlineTransaction: transaction,
2257
2285
  transactionData: response
2258
2286
  });
2259
-
2260
- loadPaystackCheckoutButton.onclick = _loadPopup;
2287
+ loadPaystackCheckoutButton.onclick = loadPopup;
2261
2288
  }
2262
2289
 
2263
2290
  resolve(transaction);
@@ -2278,22 +2305,21 @@ var Inline = /*#__PURE__*/function () {
2278
2305
 
2279
2306
  var applePayButton = getApplePayButton(this.paymentRequestContainer);
2280
2307
 
2281
- applePayButton.onclick = function () {
2282
- return _this4.startApplePay();
2283
- };
2308
+ if (applePayButton) {
2309
+ applePayButton.onclick = function () {
2310
+ return _this4.startApplePay();
2311
+ };
2312
+ }
2284
2313
  }
2285
2314
  }, {
2286
2315
  key: "registerPaymentRequestEventListeners",
2287
2316
  value: function registerPaymentRequestEventListeners() {
2288
2317
  var transaction = this.activeTransaction();
2318
+ var applePayButton = this.paymentRequestContainer && getApplePayButton(this.paymentRequestContainer);
2289
2319
 
2290
- if (transaction) {
2291
- if (canUseApplePay(transaction.response.channels)) {
2292
- this.registerApplePayEventListener();
2293
- } else {
2294
- removeAllChildNodes(this.paymentRequestContainer);
2295
- }
2296
- } else {
2320
+ if (transaction && applePayButton) {
2321
+ this.registerApplePayEventListener();
2322
+ } else if (this.paymentRequestContainer) {
2297
2323
  removeAllChildNodes(this.paymentRequestContainer);
2298
2324
  }
2299
2325
  }
@@ -2318,6 +2344,7 @@ var Inline = /*#__PURE__*/function () {
2318
2344
  amount: transaction.amount,
2319
2345
  timeSpent: transaction.getTimeSpent()
2320
2346
  };
2347
+ var pendingPreCheckoutModalStatus = null;
2321
2348
 
2322
2349
  try {
2323
2350
  transaction.logAttempt(paymentMethod);
@@ -2330,8 +2357,12 @@ var Inline = /*#__PURE__*/function () {
2330
2357
  });
2331
2358
  var applePaySession = new window.ApplePaySession(config.applePayVersion, applePayPaymentRequest);
2332
2359
 
2333
- applePaySession.onvalidatemerchant = function (_ref2) {
2334
- var validationURL = _ref2.validationURL;
2360
+ if (inlineInstance.preCheckoutModal) {
2361
+ hidePreCheckoutModal(inlineInstance.preCheckoutModal);
2362
+ }
2363
+
2364
+ applePaySession.onvalidatemerchant = function (_ref3) {
2365
+ var validationURL = _ref3.validationURL;
2335
2366
  var validateSession = validateApplePaySession({
2336
2367
  validationURL: validationURL,
2337
2368
  transactionId: transaction.id,
@@ -2340,6 +2371,7 @@ var Inline = /*#__PURE__*/function () {
2340
2371
  validateSession.then(function (response) {
2341
2372
  if (response.status !== 'success') {
2342
2373
  transaction.onSetupError(response);
2374
+ pendingPreCheckoutModalStatus = 'failed';
2343
2375
  } else {
2344
2376
  applePaySession.completeMerchantValidation(response.data);
2345
2377
  }
@@ -2347,17 +2379,25 @@ var Inline = /*#__PURE__*/function () {
2347
2379
  transaction.logValidationResponse(response.message);
2348
2380
  }).catch(function (error) {
2349
2381
  transaction.onSetupError(error);
2382
+ pendingPreCheckoutModalStatus = 'failed';
2350
2383
  });
2351
2384
  };
2352
2385
 
2353
2386
  applePaySession.oncancel = function () {
2354
- if (!inlineInstance.preCheckoutModal) {
2387
+ if (inlineInstance.preCheckoutModal) {
2388
+ if (pendingPreCheckoutModalStatus) {
2389
+ inlineInstance.closePreCheckoutModal(pendingPreCheckoutModalStatus);
2390
+ pendingPreCheckoutModalStatus = null;
2391
+ } else {
2392
+ animatePreCheckoutModalIn(inlineInstance.preCheckoutModal);
2393
+ }
2394
+ } else {
2355
2395
  transaction.onCancel();
2356
2396
  }
2357
2397
  };
2358
2398
 
2359
- applePaySession.onpaymentauthorized = function (_ref3) {
2360
- var payment = _ref3.payment;
2399
+ applePaySession.onpaymentauthorized = function (_ref4) {
2400
+ var payment = _ref4.payment;
2361
2401
  var charge = chargeApplePay({
2362
2402
  transactionId: transaction.id,
2363
2403
  payment: payment
@@ -2373,6 +2413,7 @@ var Inline = /*#__PURE__*/function () {
2373
2413
  } else {
2374
2414
  applePaySession.completePayment(applePaySession.STATUS_FAILURE);
2375
2415
  transaction.onSetupError(response);
2416
+ pendingPreCheckoutModalStatus = 'failed';
2376
2417
 
2377
2418
  _this5.sendAnalyticsEventToCheckout(analyticsActions.TRACK_PAYMENT_ERROR, {
2378
2419
  channel: 'apple_pay',
@@ -2380,23 +2421,25 @@ var Inline = /*#__PURE__*/function () {
2380
2421
  });
2381
2422
  }
2382
2423
 
2383
- inlineInstance.closePreCheckoutModal(response.status);
2424
+ if (response.status === 'success') {
2425
+ inlineInstance.closePreCheckoutModal(response.status);
2426
+ }
2384
2427
  }).catch(function (error) {
2385
2428
  applePaySession.completePayment(applePaySession.STATUS_FAILURE);
2386
2429
  transaction.onSetupError(error);
2430
+ pendingPreCheckoutModalStatus = 'failed';
2387
2431
 
2388
2432
  _this5.sendAnalyticsEventToCheckout(analyticsActions.TRACK_PAYMENT_ERROR, {
2389
2433
  channel: 'apple_pay',
2390
2434
  message: error && error.message || 'Error occurred'
2391
2435
  });
2392
-
2393
- inlineInstance.closePreCheckoutModal('failed');
2394
2436
  });
2395
2437
  };
2396
2438
 
2397
2439
  applePaySession.begin();
2398
2440
  } catch (error) {
2399
2441
  transaction.onSetupError(error);
2442
+ inlineInstance.closePreCheckoutModal('failed');
2400
2443
  }
2401
2444
  } else {
2402
2445
  handleError('Could not initiate apple pay transaction');