swell-js 3.12.1 → 3.12.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/dist/api.js CHANGED
@@ -246,7 +246,7 @@ function _request() {
246
246
 
247
247
  if (typeof window !== 'undefined') {
248
248
  window.swell = {
249
- version: '3.12.1'
249
+ version: '3.12.2'
250
250
  };
251
251
  }
252
252
 
package/dist/payment.js CHANGED
@@ -12,6 +12,12 @@ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (O
12
12
 
13
13
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
14
14
 
15
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
16
+
17
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
18
+
19
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
20
+
15
21
  var get = require('lodash/get');
16
22
 
17
23
  var toLower = require('lodash/toLower');
@@ -652,7 +658,7 @@ function payPalButton(_x16, _x17, _x18, _x19) {
652
658
 
653
659
  function _payPalButton() {
654
660
  _payPalButton = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee11(request, cart, payMethods, params) {
655
- var paypal, _params$paypal, locale, style, elementId, onError, onSuccess;
661
+ var paypal, _params$paypal, locale, style, elementId, onError, onSuccess, _getTotalsDueRemainin, totalDue;
656
662
 
657
663
  return _regenerator["default"].wrap(function _callee11$(_context11) {
658
664
  while (1) {
@@ -678,6 +684,16 @@ function _payPalButton() {
678
684
  return isFunction(successHandler) && successHandler();
679
685
  };
680
686
 
687
+ _getTotalsDueRemainin = getTotalsDueRemaining(cart), totalDue = _getTotalsDueRemainin.totalDue;
688
+
689
+ if (totalDue > 0) {
690
+ _context11.next = 9;
691
+ break;
692
+ }
693
+
694
+ throw new Error('Invalid PayPal button amount. Value should be greater than zero.');
695
+
696
+ case 9:
681
697
  paypal.Buttons({
682
698
  locale: locale || 'en_US',
683
699
  style: style || {
@@ -693,7 +709,7 @@ function _payPalButton() {
693
709
  intent: 'AUTHORIZE',
694
710
  purchase_units: [{
695
711
  amount: {
696
- value: cart.grand_total,
712
+ value: +totalDue.toFixed(2),
697
713
  currency_code: cart.currency
698
714
  }
699
715
  }]
@@ -728,7 +744,7 @@ function _payPalButton() {
728
744
  }
729
745
  }, onError).render(elementId || '#paypal-button');
730
746
 
731
- case 7:
747
+ case 10:
732
748
  case "end":
733
749
  return _context11.stop();
734
750
  }
@@ -1477,6 +1493,59 @@ function _handlePaysafecardRedirectAction() {
1477
1493
  return _handlePaysafecardRedirectAction.apply(this, arguments);
1478
1494
  }
1479
1495
 
1496
+ function getTotalsDueRemaining(cart) {
1497
+ var grand_total = cart.grand_total,
1498
+ account = cart.account,
1499
+ account_credit_amount = cart.account_credit_amount,
1500
+ giftcards = cart.giftcards;
1501
+ var totalDue = grand_total;
1502
+ var totalRemaining = 0;
1503
+ var totalRemainingGiftcard = 0;
1504
+ var totalRemainingAccount = 0;
1505
+
1506
+ if (giftcards && giftcards.length > 0) {
1507
+ var _iterator = _createForOfIteratorHelper(giftcards),
1508
+ _step;
1509
+
1510
+ try {
1511
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
1512
+ var gc = _step.value;
1513
+ totalDue -= gc.amount;
1514
+ }
1515
+ } catch (err) {
1516
+ _iterator.e(err);
1517
+ } finally {
1518
+ _iterator.f();
1519
+ }
1520
+
1521
+ if (totalDue < 0) {
1522
+ totalRemainingGiftcard = -totalDue;
1523
+ }
1524
+ }
1525
+
1526
+ var accountCreditAmount = typeof account_credit_amount === 'number' ? account_credit_amount : account && account.balance;
1527
+
1528
+ if (accountCreditAmount > 0) {
1529
+ totalDue -= accountCreditAmount;
1530
+
1531
+ if (totalDue < 0) {
1532
+ totalRemainingAccount = -totalDue - totalRemainingGiftcard;
1533
+ }
1534
+ }
1535
+
1536
+ if (totalDue < 0) {
1537
+ totalRemaining = -totalDue;
1538
+ totalDue = 0;
1539
+ }
1540
+
1541
+ return {
1542
+ totalDue: totalDue,
1543
+ totalRemaining: totalRemaining,
1544
+ totalRemainingGiftcard: totalRemainingGiftcard,
1545
+ totalRemainingAccount: totalRemainingAccount
1546
+ };
1547
+ }
1548
+
1480
1549
  module.exports = {
1481
1550
  methods: methods
1482
1551
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swell-js",
3
- "version": "3.12.1",
3
+ "version": "3.12.2",
4
4
  "description": "Swell JS library for client-side stores",
5
5
  "repository": {
6
6
  "type": "git",
package/src/payment.js CHANGED
@@ -269,6 +269,12 @@ async function payPalButton(request, cart, payMethods, params) {
269
269
  return isFunction(successHandler) && successHandler();
270
270
  };
271
271
 
272
+ const { totalDue } = getTotalsDueRemaining(cart);
273
+
274
+ if (!(totalDue > 0)) {
275
+ throw new Error('Invalid PayPal button amount. Value should be greater than zero.');
276
+ }
277
+
272
278
  paypal
273
279
  .Buttons(
274
280
  {
@@ -287,7 +293,7 @@ async function payPalButton(request, cart, payMethods, params) {
287
293
  purchase_units: [
288
294
  {
289
295
  amount: {
290
- value: cart.grand_total,
296
+ value: +totalDue.toFixed(2),
291
297
  currency_code: cart.currency,
292
298
  },
293
299
  },
@@ -695,6 +701,42 @@ async function handlePaysafecardRedirectAction(request, cart) {
695
701
  }
696
702
  }
697
703
 
704
+ function getTotalsDueRemaining(cart) {
705
+ const { grand_total, account, account_credit_amount, giftcards } = cart;
706
+
707
+ let totalDue = grand_total;
708
+ let totalRemaining = 0;
709
+ let totalRemainingGiftcard = 0;
710
+ let totalRemainingAccount = 0;
711
+
712
+ if (giftcards && giftcards.length > 0) {
713
+ for (let gc of giftcards) {
714
+ totalDue -= gc.amount;
715
+ }
716
+ if (totalDue < 0) {
717
+ totalRemainingGiftcard = -totalDue;
718
+ }
719
+ }
720
+
721
+ const accountCreditAmount =
722
+ typeof account_credit_amount === 'number'
723
+ ? account_credit_amount
724
+ : account && account.balance;
725
+ if (accountCreditAmount > 0) {
726
+ totalDue -= accountCreditAmount;
727
+ if (totalDue < 0) {
728
+ totalRemainingAccount = -totalDue - totalRemainingGiftcard;
729
+ }
730
+ }
731
+
732
+ if (totalDue < 0) {
733
+ totalRemaining = -totalDue;
734
+ totalDue = 0;
735
+ }
736
+
737
+ return { totalDue, totalRemaining, totalRemainingGiftcard, totalRemainingAccount };
738
+ }
739
+
698
740
  module.exports = {
699
741
  methods,
700
742
  };