swell-js 3.16.0 → 3.17.3

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/LICENSE CHANGED
@@ -1,4 +1,6 @@
1
- Copyright 2019 Schema Technologies, LLC
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Swell Commerce Corp.
2
4
 
3
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
6
 
@@ -2,15 +2,15 @@ import { c as cardApi } from './card-2f5f9866.js';
2
2
  import { g as getCookie, s as setCookie } from './cookie-a39e556c.js';
3
3
  import { f as trimEnd, e as trimStart, c as trimBoth, b as toSnake, h as stringifyQuery, n as base64Encode, t as toCamel, s as setOptions } from './index-bce8d606.js';
4
4
  import { c as cacheApi } from './cache-54abc38e.js';
5
- import { m as methods } from './cart-bdec9379.js';
5
+ import { m as methods } from './cart-21650912.js';
6
6
  import { m as methods$1 } from './account-328cc590.js';
7
- import { m as methods$2 } from './products-a6a43be8.js';
7
+ import { m as methods$2 } from './products-baf505e7.js';
8
8
  import { m as methods$3 } from './categories-e52bebe3.js';
9
9
  import { m as methods$4 } from './attributes-db35134e.js';
10
- import { m as methods$5 } from './subscriptions-eecdd4ff.js';
10
+ import { m as methods$5 } from './subscriptions-0e44e99f.js';
11
11
  import { m as methods$6 } from './content-eb42ecce.js';
12
12
  import { m as methods$7 } from './settings-b1a4a4af.js';
13
- import { m as methods$8 } from './payment-fc4a80bf.js';
13
+ import { m as methods$8 } from './payment-fd4a2cbe.js';
14
14
  import { m as methods$9 } from './locale-9de8dba1.js';
15
15
  import { m as methods$a } from './currency-209bb202.js';
16
16
 
package/dist/api.js CHANGED
@@ -1,4 +1,4 @@
1
- export { a as default } from './api-be1f70ea.js';
1
+ export { a as default } from './api-d1c4929d.js';
2
2
  import './card-2f5f9866.js';
3
3
  import './index-bce8d606.js';
4
4
  import 'qs';
@@ -16,15 +16,15 @@ import 'deepmerge';
16
16
  import 'object-keys-normalizer';
17
17
  import './cookie-a39e556c.js';
18
18
  import './cache-54abc38e.js';
19
- import './cart-bdec9379.js';
20
- import './products-a6a43be8.js';
19
+ import './cart-21650912.js';
20
+ import './products-baf505e7.js';
21
21
  import './attributes-db35134e.js';
22
22
  import './account-328cc590.js';
23
23
  import './categories-e52bebe3.js';
24
- import './subscriptions-eecdd4ff.js';
24
+ import './subscriptions-0e44e99f.js';
25
25
  import './content-eb42ecce.js';
26
26
  import './settings-b1a4a4af.js';
27
- import './payment-fc4a80bf.js';
27
+ import './payment-fd4a2cbe.js';
28
28
  import 'lodash/toLower';
29
29
  import 'lodash/reduce';
30
30
  import 'lodash/isEmpty';
@@ -11,7 +11,7 @@ import cloneDeep from 'lodash/cloneDeep';
11
11
  import 'lodash/isEqual';
12
12
  import 'deepmerge';
13
13
  import 'object-keys-normalizer';
14
- import { c as cleanProductOptions } from './products-a6a43be8.js';
14
+ import { c as cleanProductOptions } from './products-baf505e7.js';
15
15
 
16
16
  function methods(request, options) {
17
17
  return {
package/dist/cart.js CHANGED
@@ -1,4 +1,4 @@
1
- export { m as default } from './cart-bdec9379.js';
1
+ export { m as default } from './cart-21650912.js';
2
2
  import 'qs';
3
3
  import 'lodash/set';
4
4
  import 'lodash/get';
@@ -12,7 +12,7 @@ import 'lodash/cloneDeep';
12
12
  import 'lodash/isEqual';
13
13
  import 'deepmerge';
14
14
  import 'object-keys-normalizer';
15
- import './products-a6a43be8.js';
15
+ import './products-baf505e7.js';
16
16
  import './index-bce8d606.js';
17
17
  import './cache-54abc38e.js';
18
18
  import './attributes-db35134e.js';
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export { q as utils } from './index-bce8d606.js';
2
- export { a as default } from './api-be1f70ea.js';
2
+ export { a as default } from './api-d1c4929d.js';
3
3
  import 'qs';
4
4
  import 'lodash/set';
5
5
  import 'lodash/get';
@@ -16,15 +16,15 @@ import 'object-keys-normalizer';
16
16
  import './card-2f5f9866.js';
17
17
  import './cookie-a39e556c.js';
18
18
  import './cache-54abc38e.js';
19
- import './cart-bdec9379.js';
20
- import './products-a6a43be8.js';
19
+ import './cart-21650912.js';
20
+ import './products-baf505e7.js';
21
21
  import './attributes-db35134e.js';
22
22
  import './account-328cc590.js';
23
23
  import './categories-e52bebe3.js';
24
- import './subscriptions-eecdd4ff.js';
24
+ import './subscriptions-0e44e99f.js';
25
25
  import './content-eb42ecce.js';
26
26
  import './settings-b1a4a4af.js';
27
- import './payment-fc4a80bf.js';
27
+ import './payment-fd4a2cbe.js';
28
28
  import 'lodash/toLower';
29
29
  import 'lodash/reduce';
30
30
  import 'lodash/isEmpty';
@@ -1,6 +1,6 @@
1
1
  import get from 'lodash/get';
2
2
  import toLower from 'lodash/toLower';
3
- import { m as methods$1 } from './cart-bdec9379.js';
3
+ import { m as methods$1 } from './cart-21650912.js';
4
4
  import { m as methods$2 } from './settings-b1a4a4af.js';
5
5
  import { b as toSnake, v as vaultRequest, j as isFunction, o as getLocationParams, p as removeUrlParams } from './index-bce8d606.js';
6
6
  import reduce from 'lodash/reduce';
@@ -465,12 +465,12 @@ function methods(request, opts) {
465
465
 
466
466
  async createElements(elementParams) {
467
467
  this.params = elementParams || {};
468
- const cart = toSnake(await methods$1.methods(request, options).get());
468
+ const cart = toSnake(await methods$1(request, options).get());
469
469
  if (!cart) {
470
470
  throw new Error('Cart not found');
471
471
  }
472
472
  const payMethods = toSnake(
473
- await methods$2.methods(request, options).payments(),
473
+ await methods$2(request, options).payments(),
474
474
  );
475
475
  if (payMethods.error) {
476
476
  throw new Error(payMethods.error);
@@ -479,12 +479,12 @@ function methods(request, opts) {
479
479
  },
480
480
 
481
481
  async tokenize(params) {
482
- const cart = toSnake(await methods$1.methods(request, options).get());
482
+ const cart = toSnake(await methods$1(request, options).get());
483
483
  if (!cart) {
484
484
  throw new Error('Cart not found');
485
485
  }
486
486
  const payMethods = toSnake(
487
- await methods$2.methods(request, options).payments(),
487
+ await methods$2(request, options).payments(),
488
488
  );
489
489
  if (payMethods.error) {
490
490
  throw new Error(payMethods.error);
@@ -498,7 +498,7 @@ function methods(request, opts) {
498
498
  },
499
499
 
500
500
  async handleRedirect(params) {
501
- const cart = toSnake(await methods$1.methods(request, options).get());
501
+ const cart = toSnake(await methods$1(request, options).get());
502
502
  if (!cart) {
503
503
  throw new Error('Cart not found');
504
504
  }
@@ -511,7 +511,7 @@ function methods(request, opts) {
511
511
  throw new Error('Payment not found');
512
512
  }
513
513
  const payMethods = toSnake(
514
- await methods$2.methods(request, options).payments(),
514
+ await methods$2(request, options).payments(),
515
515
  );
516
516
  if (payMethods.error) {
517
517
  throw new Error(payMethods.error);
@@ -699,9 +699,30 @@ async function stripeElements(request, payMethods, params) {
699
699
  }
700
700
  }
701
701
 
702
+ /**
703
+ * Update cart email with paypal's when no email is present
704
+ */
705
+ async function shouldUsePayPalEmail(guest, request, options) {
706
+ // Only check if the email should be updated when the user is not logged in (guest user)
707
+ if (!guest) return false;
708
+
709
+ // Refetch to avoid stale data from the cart
710
+ const updatedCart = await methods$1(request, options).get();
711
+ const currentEmail = get(updatedCart, 'account.email');
712
+
713
+ // If no email is present, use paypal's email
714
+ if (!currentEmail) {
715
+ return true;
716
+ } else {
717
+ return false;
718
+ }
719
+ }
720
+
702
721
  async function payPalButton(request, cart, payMethods, params) {
703
722
  const paypal = window.paypal;
704
723
  const { paypal: { locale, style, elementId } = {} } = params;
724
+ const { capture_total, currency, guest } = cart;
725
+
705
726
  const onError = (error) => {
706
727
  const errorHandler = get(params, 'paypal.onError');
707
728
  if (isFunction(errorHandler)) {
@@ -714,9 +735,7 @@ async function payPalButton(request, cart, payMethods, params) {
714
735
  return isFunction(successHandler) && successHandler();
715
736
  };
716
737
 
717
- const { totalDue } = getTotalsDueRemaining(cart);
718
-
719
- if (!(totalDue > 0)) {
738
+ if (!(capture_total > 0)) {
720
739
  throw new Error(
721
740
  'Invalid PayPal button amount. Value should be greater than zero.',
722
741
  );
@@ -740,8 +759,8 @@ async function payPalButton(request, cart, payMethods, params) {
740
759
  purchase_units: [
741
760
  {
742
761
  amount: {
743
- value: +totalDue.toFixed(2),
744
- currency_code: cart.currency,
762
+ value: +capture_total.toFixed(2),
763
+ currency_code: currency,
745
764
  },
746
765
  },
747
766
  ],
@@ -749,13 +768,19 @@ async function payPalButton(request, cart, payMethods, params) {
749
768
  onApprove: (data, actions) =>
750
769
  actions.order
751
770
  .get()
752
- .then((order) => {
771
+ .then(async (order) => {
753
772
  const orderId = order.id;
754
773
  const payer = order.payer;
755
774
  const shipping = get(order, 'purchase_units[0].shipping');
756
775
 
757
- return methods$1.methods(request).update({
758
- ...(!cart.account_logged_in && {
776
+ const usePayPalEmail = await shouldUsePayPalEmail(
777
+ guest,
778
+ request,
779
+ options,
780
+ );
781
+
782
+ return methods$1(request).update({
783
+ ...(usePayPalEmail && {
759
784
  account: {
760
785
  email: payer.email_address,
761
786
  },
@@ -815,9 +840,9 @@ async function braintreePayPalButton(request, cart, payMethods, params) {
815
840
  paypalCheckoutInstance
816
841
  .tokenizePayment(data)
817
842
  .then(({ nonce }) =>
818
- methods$1
819
- .methods(request, options)
820
- .update({ billing: { paypal: { nonce } } }),
843
+ methods$1(request, options).update({
844
+ billing: { paypal: { nonce } },
845
+ }),
821
846
  )
822
847
  .then(
823
848
  () =>
@@ -846,7 +871,7 @@ async function braintreePayPalButton(request, cart, payMethods, params) {
846
871
  }
847
872
 
848
873
  async function paymentTokenize(request, params, payMethods, cart) {
849
- const { totalDue } = getTotalsDueRemaining(cart);
874
+ const { capture_total, auth_total } = cart;
850
875
  const onError = (error) => {
851
876
  const errorHandler =
852
877
  get(params, 'card.onError') ||
@@ -885,11 +910,10 @@ async function paymentTokenize(request, params, payMethods, cart) {
885
910
 
886
911
  if (paymentMethod.error) {
887
912
  return onError(paymentMethod.error);
888
- } else if (totalDue < 1) {
913
+ } else if (capture_total < 1) {
889
914
  // should save payment method data when payment amount is less than 1
890
915
  // https://stripe.com/docs/currencies#minimum-and-maximum-charge-amounts
891
- return methods$1
892
- .methods(request, options)
916
+ return methods$1(request, options)
893
917
  .update({
894
918
  billing: {
895
919
  method: 'card',
@@ -903,7 +927,7 @@ async function paymentTokenize(request, params, payMethods, cart) {
903
927
  const currency = toLower(get(cart, 'currency', 'usd'));
904
928
  const amount = stripeAmountByCurrency(
905
929
  currency,
906
- cart.trial ? cart.trial_initial_capture_total + cart.trial_auth_total : totalDue,
930
+ capture_total + auth_total,
907
931
  );
908
932
  const stripeCustomer = get(cart, 'account.stripe_customer');
909
933
  const intent = toSnake(
@@ -928,8 +952,7 @@ async function paymentTokenize(request, params, payMethods, cart) {
928
952
  );
929
953
  return error
930
954
  ? onError(error)
931
- : await methods$1
932
- .methods(request, options)
955
+ : await methods$1(request, options)
933
956
  .update({
934
957
  billing: {
935
958
  method: 'card',
@@ -937,8 +960,8 @@ async function paymentTokenize(request, params, payMethods, cart) {
937
960
  intent: {
938
961
  stripe: {
939
962
  id: paymentIntent.id,
940
- ...(cart.trial && {
941
- auth_amount: cart.trial_auth_total,
963
+ ...(!!auth_total && {
964
+ auth_amount: auth_total,
942
965
  }),
943
966
  },
944
967
  },
@@ -958,7 +981,7 @@ async function paymentTokenize(request, params, payMethods, cart) {
958
981
  return onError(intent.error);
959
982
  }
960
983
 
961
- await methods$1.methods(request, options).update({
984
+ await methods$1(request, options).update({
962
985
  billing: {
963
986
  method: 'card',
964
987
  intent: {
@@ -989,7 +1012,7 @@ async function paymentTokenize(request, params, payMethods, cart) {
989
1012
  }
990
1013
 
991
1014
  const currency = toLower(get(cart, 'currency', 'eur'));
992
- const amount = stripeAmountByCurrency(currency, totalDue);
1015
+ const amount = stripeAmountByCurrency(currency, capture_total);
993
1016
  const intent = toSnake(
994
1017
  await methods(request)
995
1018
  .createIntent({
@@ -1008,8 +1031,7 @@ async function paymentTokenize(request, params, payMethods, cart) {
1008
1031
  );
1009
1032
 
1010
1033
  if (intent) {
1011
- await methods$1
1012
- .methods(request, options)
1034
+ await methods$1(request, options)
1013
1035
  .update({
1014
1036
  billing: {
1015
1037
  method: 'ideal',
@@ -1041,9 +1063,7 @@ async function paymentTokenize(request, params, payMethods, cart) {
1041
1063
  }
1042
1064
  const { publishable_key } = payMethods.card;
1043
1065
  const stripe = window.Stripe(publishable_key);
1044
- const settings = toSnake(
1045
- await methods$2.methods(request, options).get(),
1046
- );
1066
+ const settings = toSnake(await methods$2(request, options).get());
1047
1067
 
1048
1068
  const { error, source } = await createKlarnaSource(stripe, {
1049
1069
  ...cart,
@@ -1052,8 +1072,7 @@ async function paymentTokenize(request, params, payMethods, cart) {
1052
1072
 
1053
1073
  return error
1054
1074
  ? onError(error)
1055
- : methods$1
1056
- .methods(request, options)
1075
+ : methods$1(request, options)
1057
1076
  .update({
1058
1077
  billing: {
1059
1078
  method: 'klarna',
@@ -1074,8 +1093,7 @@ async function paymentTokenize(request, params, payMethods, cart) {
1074
1093
 
1075
1094
  return error
1076
1095
  ? onError(error)
1077
- : methods$1
1078
- .methods(request, options)
1096
+ : methods$1(request, options)
1079
1097
  .update({
1080
1098
  billing: {
1081
1099
  method: 'bancontact',
@@ -1093,7 +1111,7 @@ async function paymentTokenize(request, params, payMethods, cart) {
1093
1111
  return;
1094
1112
  }
1095
1113
 
1096
- await methods$1.methods(request, options).update({
1114
+ await methods$1(request, options).update({
1097
1115
  billing: {
1098
1116
  method: 'paysafecard',
1099
1117
  intent: {
@@ -1181,7 +1199,7 @@ async function handleQuickpayRedirectAction(
1181
1199
  } else if (card.error) {
1182
1200
  return card;
1183
1201
  } else {
1184
- await methods$1.methods(request, options).update({
1202
+ await methods$1(request, options).update({
1185
1203
  billing: {
1186
1204
  method: 'card',
1187
1205
  card,
@@ -1253,7 +1271,7 @@ async function handleDirectKlarnaRedirectAction(
1253
1271
  };
1254
1272
  }
1255
1273
 
1256
- await methods$1.methods(request, options).update({
1274
+ await methods$1(request, options).update({
1257
1275
  billing: {
1258
1276
  method: 'klarna',
1259
1277
  klarna: {
@@ -1307,45 +1325,4 @@ async function authenticateStripeCard(request, payment, payMethods) {
1307
1325
  : { status: actionResult.status };
1308
1326
  }
1309
1327
 
1310
- function getTotalsDueRemaining(cart) {
1311
- const { grand_total, account, account_credit_amount, giftcards } = cart;
1312
-
1313
- let totalDue = grand_total;
1314
- let totalRemaining = 0;
1315
- let totalRemainingGiftcard = 0;
1316
- let totalRemainingAccount = 0;
1317
-
1318
- if (giftcards && giftcards.length > 0) {
1319
- for (let gc of giftcards) {
1320
- totalDue -= gc.amount;
1321
- }
1322
- if (totalDue < 0) {
1323
- totalRemainingGiftcard = -totalDue;
1324
- }
1325
- }
1326
-
1327
- const accountCreditAmount =
1328
- typeof account_credit_amount === 'number'
1329
- ? account_credit_amount
1330
- : account && account.balance;
1331
- if (accountCreditAmount > 0) {
1332
- totalDue -= accountCreditAmount;
1333
- if (totalDue < 0) {
1334
- totalRemainingAccount = -totalDue - totalRemainingGiftcard;
1335
- }
1336
- }
1337
-
1338
- if (totalDue < 0) {
1339
- totalRemaining = -totalDue;
1340
- totalDue = 0;
1341
- }
1342
-
1343
- return {
1344
- totalDue,
1345
- totalRemaining,
1346
- totalRemainingGiftcard,
1347
- totalRemainingAccount,
1348
- };
1349
- }
1350
-
1351
- export { methods as m };
1328
+ export { methods as m, shouldUsePayPalEmail as s };
package/dist/payment.js CHANGED
@@ -1,7 +1,7 @@
1
- export { m as default } from './payment-fc4a80bf.js';
1
+ export { m as default, s as shouldUsePayPalEmail } from './payment-fd4a2cbe.js';
2
2
  import 'lodash/get';
3
3
  import 'lodash/toLower';
4
- import './cart-bdec9379.js';
4
+ import './cart-21650912.js';
5
5
  import 'qs';
6
6
  import 'lodash/set';
7
7
  import 'lodash/uniq';
@@ -14,7 +14,7 @@ import 'lodash/cloneDeep';
14
14
  import 'lodash/isEqual';
15
15
  import 'deepmerge';
16
16
  import 'object-keys-normalizer';
17
- import './products-a6a43be8.js';
17
+ import './products-baf505e7.js';
18
18
  import './index-bce8d606.js';
19
19
  import './cache-54abc38e.js';
20
20
  import './attributes-db35134e.js';
@@ -235,11 +235,10 @@ function findPurchaseOption(product, purchaseOption) {
235
235
  }
236
236
 
237
237
  async function getFilterableAttributeFilters(request, products, options) {
238
- const { results: filterableAttributes } = await methods$1
239
- .methods(request, OPTIONS)
240
- .list({
241
- filterable: true,
242
- });
238
+ const { results: filterableAttributes } = await methods$1(
239
+ request).list({
240
+ filterable: true,
241
+ });
243
242
 
244
243
  return getFilters(products, { ...options, filterableAttributes });
245
244
  }
package/dist/products.js CHANGED
@@ -1,4 +1,4 @@
1
- export { b as calculateVariation, c as cleanProductOptions, m as default, f as findVariantWithOptions, g as getProductOptionIndex, a as getVariantOptionValueIds } from './products-a6a43be8.js';
1
+ export { b as calculateVariation, c as cleanProductOptions, m as default, f as findVariantWithOptions, g as getProductOptionIndex, a as getVariantOptionValueIds } from './products-baf505e7.js';
2
2
  import './index-bce8d606.js';
3
3
  import 'qs';
4
4
  import 'lodash/set';
@@ -1,4 +1,4 @@
1
- import { c as cleanProductOptions } from './products-a6a43be8.js';
1
+ import { c as cleanProductOptions } from './products-baf505e7.js';
2
2
  import { d as defaultMethods } from './index-bce8d606.js';
3
3
  import { c as cacheApi } from './cache-54abc38e.js';
4
4
 
@@ -1,5 +1,5 @@
1
- export { m as default } from './subscriptions-eecdd4ff.js';
2
- import './products-a6a43be8.js';
1
+ export { m as default } from './subscriptions-0e44e99f.js';
2
+ import './products-baf505e7.js';
3
3
  import './index-bce8d606.js';
4
4
  import 'qs';
5
5
  import 'lodash/set';