swell-js 3.20.2 → 3.21.0

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.
@@ -10,7 +10,7 @@ import { m as methods$6 } from './subscriptions-8044a530.js';
10
10
  import { d as defaultMethods, n as trimEnd, K as utils, l as trimStart, k as trimBoth, j as toSnake, o as stringifyQuery, E as base64Encode, t as toCamel, e as setOptions } from './index-512fc30d.js';
11
11
  import { m as methods$7 } from './content-0afdcb05.js';
12
12
  import { m as methods$8 } from './settings-937b96f1.js';
13
- import { P as PaymentController } from './index-47c8a8e3.js';
13
+ import { P as PaymentController } from './index-11e12022.js';
14
14
  import { m as methods$9 } from './locale-4391bcf3.js';
15
15
  import { m as methods$a } from './currency-842f76f2.js';
16
16
 
@@ -34,7 +34,7 @@ const options = {
34
34
  };
35
35
 
36
36
  const api = {
37
- version: '3.20.2',
37
+ version: '3.21.0',
38
38
  options,
39
39
  request,
40
40
 
package/dist/api.js CHANGED
@@ -1,4 +1,4 @@
1
- export { a as default } from './api-3759aa14.js';
1
+ export { a as default } from './api-3ec9898d.js';
2
2
  import './card-91071403.js';
3
3
  import './index-512fc30d.js';
4
4
  import 'qs';
@@ -14,6 +14,6 @@ import './categories-8f6a4584.js';
14
14
  import './subscriptions-8044a530.js';
15
15
  import './content-0afdcb05.js';
16
16
  import './settings-937b96f1.js';
17
- import './index-47c8a8e3.js';
17
+ import './index-11e12022.js';
18
18
  import './locale-4391bcf3.js';
19
19
  import './currency-842f76f2.js';
@@ -14,6 +14,7 @@ const SCRIPT_HANDLERS = {
14
14
  'braintree-web-paypal-checkout': loadBraintreePaypalCheckout,
15
15
  'braintree-google-payment': loadBraintreeGoogle,
16
16
  'braintree-apple-payment': loadBraintreeApple,
17
+ 'amazon-checkout': loadAmazonCheckout,
17
18
  };
18
19
 
19
20
  async function loadStripe() {
@@ -125,6 +126,19 @@ async function loadBraintreeApple() {
125
126
  }
126
127
  }
127
128
 
129
+ async function loadAmazonCheckout() {
130
+ if (!window.amazon) {
131
+ await loadScript(
132
+ 'amazon-checkout',
133
+ 'https://static-na.payments-amazon.com/checkout.js',
134
+ );
135
+ }
136
+
137
+ if (!window.amazon) {
138
+ console.error('Warning: Amazon Checkout was not loaded');
139
+ }
140
+ }
141
+
128
142
  async function loadScripts(scripts) {
129
143
  if (!scripts) {
130
144
  return;
@@ -624,13 +638,27 @@ class UnableAuthenticatePaymentMethodError extends Error {
624
638
  }
625
639
  }
626
640
 
627
- class LibraryNotLoaded extends Error {
641
+ class LibraryNotLoadedError extends Error {
628
642
  constructor(library) {
629
643
  const message = `${library} was not loaded`;
630
644
  super(message);
631
645
  }
632
646
  }
633
647
 
648
+ class MethodPropertyMissingError extends Error {
649
+ constructor(method, property) {
650
+ const message = `${method} ${property} is missing`;
651
+ super(message);
652
+ }
653
+ }
654
+
655
+ class DomElementNotFoundError extends Error {
656
+ constructor(elementId) {
657
+ const message = `DOM element with '${elementId}' ID not found`;
658
+ super(message);
659
+ }
660
+ }
661
+
634
662
  class StripeCardPayment extends Payment {
635
663
  constructor(request, options, params, methods) {
636
664
  super(request, options, params, methods.card);
@@ -647,7 +675,7 @@ class StripeCardPayment extends Payment {
647
675
  }
648
676
 
649
677
  if (!StripeCardPayment.stripe) {
650
- throw new LibraryNotLoaded('Stripe');
678
+ throw new LibraryNotLoadedError('Stripe');
651
679
  }
652
680
  }
653
681
 
@@ -813,7 +841,7 @@ class StripeIDealPayment extends Payment {
813
841
  }
814
842
 
815
843
  if (!StripeIDealPayment.stripe) {
816
- throw new LibraryNotLoaded('Stripe');
844
+ throw new LibraryNotLoadedError('Stripe');
817
845
  }
818
846
  }
819
847
 
@@ -926,7 +954,7 @@ class StripeBancontactPayment extends Payment {
926
954
  }
927
955
 
928
956
  if (!StripeBancontactPayment.stripe) {
929
- throw new LibraryNotLoaded('Stripe');
957
+ throw new LibraryNotLoadedError('Stripe');
930
958
  }
931
959
  }
932
960
 
@@ -983,7 +1011,7 @@ class StripeKlarnaPayment extends Payment {
983
1011
  }
984
1012
 
985
1013
  if (!StripeKlarnaPayment.stripe) {
986
- throw new LibraryNotLoaded('Stripe');
1014
+ throw new LibraryNotLoadedError('Stripe');
987
1015
  }
988
1016
  }
989
1017
 
@@ -1048,7 +1076,7 @@ class StripeGooglePayment extends Payment {
1048
1076
 
1049
1077
  get google() {
1050
1078
  if (!window.google) {
1051
- throw new LibraryNotLoaded('Google');
1079
+ throw new LibraryNotLoadedError('Google');
1052
1080
  }
1053
1081
 
1054
1082
  return window.google;
@@ -1063,7 +1091,7 @@ class StripeGooglePayment extends Payment {
1063
1091
  }
1064
1092
 
1065
1093
  if (!StripeGooglePayment.googleClient) {
1066
- throw new LibraryNotLoaded('Google client');
1094
+ throw new LibraryNotLoadedError('Google client');
1067
1095
  }
1068
1096
  }
1069
1097
 
@@ -1175,7 +1203,7 @@ class StripeGooglePayment extends Payment {
1175
1203
  const container = document.getElementById(elementId);
1176
1204
 
1177
1205
  if (!container) {
1178
- throw new Error(`DOM element with '${elementId}' ID not found`);
1206
+ throw new DomElementNotFoundError(elementId);
1179
1207
  }
1180
1208
 
1181
1209
  if (classes.base) {
@@ -1281,7 +1309,7 @@ class StripeApplePayment extends Payment {
1281
1309
  }
1282
1310
 
1283
1311
  if (!StripeApplePayment.stripe) {
1284
- throw new LibraryNotLoaded('Stripe');
1312
+ throw new LibraryNotLoadedError('Stripe');
1285
1313
  }
1286
1314
  }
1287
1315
 
@@ -1357,7 +1385,7 @@ class StripeApplePayment extends Payment {
1357
1385
  const container = document.getElementById(elementId);
1358
1386
 
1359
1387
  if (!container) {
1360
- throw new Error(`DOM element with '${elementId}' ID not found`);
1388
+ throw new DomElementNotFoundError(elementId);
1361
1389
  }
1362
1390
 
1363
1391
  const button = this.stripe.elements().create('paymentRequestButton', {
@@ -1550,7 +1578,7 @@ class BraintreePaypalPayment extends Payment {
1550
1578
 
1551
1579
  get paypal() {
1552
1580
  if (!window.paypal) {
1553
- throw new LibraryNotLoaded('PayPal');
1581
+ throw new LibraryNotLoadedError('PayPal');
1554
1582
  }
1555
1583
 
1556
1584
  return window.paypal;
@@ -1558,7 +1586,7 @@ class BraintreePaypalPayment extends Payment {
1558
1586
 
1559
1587
  get braintree() {
1560
1588
  if (!window.braintree) {
1561
- throw new LibraryNotLoaded('Braintree');
1589
+ throw new LibraryNotLoadedError('Braintree');
1562
1590
  }
1563
1591
 
1564
1592
  return window.braintree;
@@ -1566,7 +1594,7 @@ class BraintreePaypalPayment extends Payment {
1566
1594
 
1567
1595
  get braintreePaypalCheckout() {
1568
1596
  if (!this.braintree.paypalCheckout) {
1569
- throw new LibraryNotLoaded('Braintree PayPal Checkout');
1597
+ throw new LibraryNotLoadedError('Braintree PayPal Checkout');
1570
1598
  }
1571
1599
 
1572
1600
  return this.braintree.paypalCheckout;
@@ -1654,7 +1682,7 @@ class BraintreeGooglePayment extends Payment {
1654
1682
 
1655
1683
  get braintree() {
1656
1684
  if (!window.braintree) {
1657
- throw new LibraryNotLoaded('Braintree');
1685
+ throw new LibraryNotLoadedError('Braintree');
1658
1686
  }
1659
1687
 
1660
1688
  return window.braintree;
@@ -1662,7 +1690,7 @@ class BraintreeGooglePayment extends Payment {
1662
1690
 
1663
1691
  get google() {
1664
1692
  if (!window.google) {
1665
- throw new LibraryNotLoaded('Google');
1693
+ throw new LibraryNotLoadedError('Google');
1666
1694
  }
1667
1695
 
1668
1696
  return window.google;
@@ -1677,7 +1705,7 @@ class BraintreeGooglePayment extends Payment {
1677
1705
  }
1678
1706
 
1679
1707
  if (!BraintreeGooglePayment.googleClient) {
1680
- throw new LibraryNotLoaded('Google client');
1708
+ throw new LibraryNotLoadedError('Google client');
1681
1709
  }
1682
1710
  }
1683
1711
 
@@ -1793,7 +1821,7 @@ class BraintreeGooglePayment extends Payment {
1793
1821
  const container = document.getElementById(elementId);
1794
1822
 
1795
1823
  if (!container) {
1796
- throw new Error(`DOM element with '${elementId}' ID not found`);
1824
+ throw new DomElementNotFoundError(elementId);
1797
1825
  }
1798
1826
 
1799
1827
  if (classes.base) {
@@ -1890,7 +1918,7 @@ class BraintreeApplePayment extends Payment {
1890
1918
 
1891
1919
  get braintree() {
1892
1920
  if (!window.braintree) {
1893
- throw new LibraryNotLoaded('Braintree');
1921
+ throw new LibraryNotLoadedError('Braintree');
1894
1922
  }
1895
1923
 
1896
1924
  return window.braintree;
@@ -1898,7 +1926,7 @@ class BraintreeApplePayment extends Payment {
1898
1926
 
1899
1927
  get ApplePaySession() {
1900
1928
  if (!window.ApplePaySession) {
1901
- throw new LibraryNotLoaded('Apple');
1929
+ throw new LibraryNotLoadedError('Apple');
1902
1930
  }
1903
1931
 
1904
1932
  return window.ApplePaySession;
@@ -1930,7 +1958,7 @@ class BraintreeApplePayment extends Payment {
1930
1958
  const container = document.getElementById(elementId);
1931
1959
 
1932
1960
  if (!container) {
1933
- throw new Error(`DOM element with '${elementId}' ID not found`);
1961
+ throw new DomElementNotFoundError(elementId);
1934
1962
  }
1935
1963
 
1936
1964
  if (classes.base) {
@@ -2395,7 +2423,7 @@ class PaypalDirectPayment extends Payment {
2395
2423
 
2396
2424
  get paypal() {
2397
2425
  if (!window.paypal) {
2398
- throw new LibraryNotLoaded('PayPal');
2426
+ throw new LibraryNotLoadedError('PayPal');
2399
2427
  }
2400
2428
 
2401
2429
  return window.paypal;
@@ -2476,6 +2504,187 @@ class PaypalDirectPayment extends Payment {
2476
2504
  }
2477
2505
  }
2478
2506
 
2507
+ class AmazonDirectPayment extends Payment {
2508
+ constructor(request, options, params, methods) {
2509
+ super(request, options, params, methods.amazon);
2510
+ }
2511
+
2512
+ get scripts() {
2513
+ return ['amazon-checkout'];
2514
+ }
2515
+
2516
+ get amazon() {
2517
+ if (!window.amazon) {
2518
+ throw new LibraryNotLoadedError('Amazon');
2519
+ }
2520
+
2521
+ return window.amazon;
2522
+ }
2523
+
2524
+ get merchantId() {
2525
+ const merchantId = this.method.merchant_id;
2526
+
2527
+ if (!merchantId) {
2528
+ throw new MethodPropertyMissingError('Amazon', 'merchant_id');
2529
+ }
2530
+
2531
+ return merchantId;
2532
+ }
2533
+
2534
+ get publicKeyId() {
2535
+ const publicKeyId = this.method.public_key_id;
2536
+
2537
+ if (!publicKeyId) {
2538
+ throw new MethodPropertyMissingError('Amazon', 'public_key_id');
2539
+ }
2540
+
2541
+ return publicKeyId;
2542
+ }
2543
+
2544
+ get returnUrl() {
2545
+ return `${
2546
+ window.location.origin + window.location.pathname
2547
+ }?gateway=amazon`;
2548
+ }
2549
+
2550
+ async createElements() {
2551
+ const cart = await this.getCart();
2552
+ const returnUrl = this.returnUrl;
2553
+ const isSubscription = Boolean(cart.subscription_delivery);
2554
+ const session = await this.authorizeGateway({
2555
+ gateway: 'amazon',
2556
+ params: {
2557
+ chargePermissionType: isSubscription ? 'Recurring' : 'OneTime',
2558
+ ...(isSubscription
2559
+ ? {
2560
+ recurringMetadata: {
2561
+ frequency: {
2562
+ unit: 'Variable',
2563
+ value: '0',
2564
+ },
2565
+ },
2566
+ }
2567
+ : {}),
2568
+ webCheckoutDetails: {
2569
+ checkoutReviewReturnUrl: `${returnUrl}&redirect_status=succeeded`,
2570
+ checkoutCancelUrl: `${returnUrl}&redirect_status=canceled`,
2571
+ },
2572
+ },
2573
+ });
2574
+
2575
+ this._renderButton(cart, session);
2576
+ }
2577
+
2578
+ async tokenize() {
2579
+ const cart = await this.getCart();
2580
+ const returnUrl = this.returnUrl;
2581
+ const checkoutSessionId = get(cart, 'billing.amazon.checkout_session_id');
2582
+
2583
+ if (!checkoutSessionId) {
2584
+ throw new Error(
2585
+ 'Missing Amazon Pay checkout session ID (billing.amazon.checkout_session_id)',
2586
+ );
2587
+ }
2588
+
2589
+ const intent = await this.createIntent({
2590
+ gateway: 'amazon',
2591
+ intent: {
2592
+ checkoutSessionId,
2593
+ webCheckoutDetails: {
2594
+ checkoutResultReturnUrl: `${returnUrl}&confirm=true&redirect_status=succeeded`,
2595
+ checkoutCancelUrl: `${returnUrl}&redirect_status=canceled`,
2596
+ },
2597
+ paymentDetails:
2598
+ cart.capture_total > 0
2599
+ ? {
2600
+ paymentIntent: 'Authorize',
2601
+ canHandlePendingAuthorization: true,
2602
+ chargeAmount: {
2603
+ amount: cart.capture_total,
2604
+ currencyCode: cart.currency,
2605
+ },
2606
+ }
2607
+ : {
2608
+ // Just confirm payment to save payment details when capture total amount is 0.
2609
+ // e.g. trial subscription, 100% discount or items.price = 0
2610
+ paymentIntent: 'Confirm',
2611
+ },
2612
+ },
2613
+ });
2614
+
2615
+ return window.location.replace(intent.redirect_url);
2616
+ }
2617
+
2618
+ async handleRedirect(queryParams) {
2619
+ const { redirect_status } = queryParams;
2620
+
2621
+ switch (redirect_status) {
2622
+ case 'succeeded':
2623
+ return this._handleSuccessfulRedirect(queryParams);
2624
+ case 'canceled':
2625
+ throw new UnableAuthenticatePaymentMethodError();
2626
+ default:
2627
+ throw new Error(`Unknown redirect status: ${redirect_status}`);
2628
+ }
2629
+ }
2630
+
2631
+ _renderButton(cart, session) {
2632
+ const amazon = this.amazon;
2633
+ const merchantId = this.merchantId;
2634
+ const publicKeyId = this.publicKeyId;
2635
+ const { payload: payloadJSON, signature } = session;
2636
+ const {
2637
+ elementId = 'amazonpay-button',
2638
+ locale = 'en_US',
2639
+ placement = 'Checkout',
2640
+ style: { color = 'Gold' } = {},
2641
+ require: { shipping: requireShipping } = {},
2642
+ classes = {},
2643
+ } = this.params;
2644
+
2645
+ const container = document.getElementById(elementId);
2646
+
2647
+ if (!container) {
2648
+ throw new DomElementNotFoundError(elementId);
2649
+ }
2650
+
2651
+ amazon.Pay.renderButton(`#${elementId}`, {
2652
+ ledgerCurrency: cart.currency,
2653
+ checkoutLanguage: locale,
2654
+ productType: Boolean(requireShipping) ? 'PayAndShip' : 'PayOnly',
2655
+ buttonColor: color,
2656
+ placement,
2657
+ merchantId,
2658
+ publicKeyId,
2659
+ createCheckoutSessionConfig: {
2660
+ payloadJSON,
2661
+ signature,
2662
+ },
2663
+ });
2664
+
2665
+ if (classes.base) {
2666
+ container.classList.add(classes.base);
2667
+ }
2668
+ }
2669
+
2670
+ async _handleSuccessfulRedirect(queryParams) {
2671
+ const { confirm, amazonCheckoutSessionId } = queryParams;
2672
+
2673
+ if (!confirm) {
2674
+ await this.updateCart({
2675
+ billing: {
2676
+ method: 'amazon',
2677
+ amazon: {
2678
+ checkout_session_id: amazonCheckoutSessionId,
2679
+ },
2680
+ },
2681
+ });
2682
+ }
2683
+
2684
+ this.onSuccess();
2685
+ }
2686
+ }
2687
+
2479
2688
  class PaymentController {
2480
2689
  constructor(request, options) {
2481
2690
  this.request = request;
@@ -2517,14 +2726,18 @@ class PaymentController {
2517
2726
  }
2518
2727
 
2519
2728
  async handleRedirect(params = this.params) {
2729
+ const queryParams = getLocationParams(window.location);
2730
+
2731
+ if (!queryParams || !queryParams.gateway) {
2732
+ return;
2733
+ }
2734
+
2520
2735
  this.params = params;
2521
2736
 
2522
2737
  if (!params) {
2523
2738
  throw new Error('Redirect parameters are not provided');
2524
2739
  }
2525
2740
 
2526
- const queryParams = getLocationParams(window.location);
2527
-
2528
2741
  removeUrlParams();
2529
2742
  this._performPaymentAction('handleRedirect', queryParams);
2530
2743
  }
@@ -2690,6 +2903,8 @@ class PaymentController {
2690
2903
  return this._getGooglePaymentClass(gateway);
2691
2904
  case 'apple':
2692
2905
  return this._getApplePaymentClass(gateway);
2906
+ case 'amazon':
2907
+ return this._getAmazonPaymentClass(gateway);
2693
2908
  default:
2694
2909
  return null;
2695
2910
  }
@@ -2772,6 +2987,13 @@ class PaymentController {
2772
2987
  return null;
2773
2988
  }
2774
2989
  }
2990
+
2991
+ _getAmazonPaymentClass(gateway) {
2992
+ switch (gateway) {
2993
+ default:
2994
+ return AmazonDirectPayment;
2995
+ }
2996
+ }
2775
2997
  }
2776
2998
 
2777
2999
  export { PaymentController as P };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { a as default } from './api-3759aa14.js';
1
+ export { a as default } from './api-3ec9898d.js';
2
2
  import './card-91071403.js';
3
3
  import './index-512fc30d.js';
4
4
  import 'qs';
@@ -14,6 +14,6 @@ import './categories-8f6a4584.js';
14
14
  import './subscriptions-8044a530.js';
15
15
  import './content-0afdcb05.js';
16
16
  import './settings-937b96f1.js';
17
- import './index-47c8a8e3.js';
17
+ import './index-11e12022.js';
18
18
  import './locale-4391bcf3.js';
19
19
  import './currency-842f76f2.js';
package/dist/payment.js CHANGED
@@ -1,4 +1,4 @@
1
- export { P as default } from './index-47c8a8e3.js';
1
+ export { P as default } from './index-11e12022.js';
2
2
  import './cart-ff3e3ef6.js';
3
3
  import 'qs';
4
4
  import './index-512fc30d.js';