@merchi/cart 1.4.4 → 1.4.5

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.
@@ -32,6 +32,7 @@ import { makeJob } from './utilities/job';
32
32
  import { makeProduct } from './utilities/product';
33
33
  import { getSavedCheckoutCustomer, saveCheckoutAddress } from './utilities/local_storage';
34
34
  import { makeUser } from './utilities/user';
35
+ import { persistTestCheckoutFlag, readTestCheckoutFlag } from './utilities/test_checkout';
35
36
  import { jsx as _jsx } from "react/jsx-runtime";
36
37
  var CartContext = /*#__PURE__*/createContext({
37
38
  cart: {},
@@ -497,47 +498,65 @@ var CartProvider = function CartProvider(_ref) {
497
498
  }
498
499
  function _createCartAndCookie() {
499
500
  _createCartAndCookie = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(callback) {
500
- var _cart, cartJson, _t3;
501
+ var isTest, _cart, created, cartJson, _t3;
501
502
  return _regenerator().w(function (_context2) {
502
503
  while (1) switch (_context2.p = _context2.n) {
503
504
  case 0:
504
505
  setFetchingCart(true);
505
506
  _context2.p = 1;
507
+ isTest = readTestCheckoutFlag();
506
508
  _cart = makeCart({
507
509
  domain: {
508
510
  id: domainId
509
- }
511
+ },
512
+ isTest: isTest
510
513
  }, true);
514
+ if (!isTest) {
515
+ _context2.n = 3;
516
+ break;
517
+ }
511
518
  _context2.n = 2;
519
+ return merchi.authenticatedFetch('/carts/', {
520
+ method: 'POST',
521
+ body: _cart.toFormData(),
522
+ query: [['embed', JSON.stringify(cartEmbed)], ['skip_rights', 'y'], ['is_test', 'true']]
523
+ });
524
+ case 2:
525
+ created = _context2.v;
526
+ _cart.fromJson(created.cart);
527
+ _context2.n = 4;
528
+ break;
529
+ case 3:
530
+ _context2.n = 4;
512
531
  return _cart.create({
513
532
  embed: cartEmbed
514
533
  });
515
- case 2:
516
- _context2.n = 3;
534
+ case 4:
535
+ _context2.n = 5;
517
536
  return _cart.toJson();
518
- case 3:
537
+ case 5:
519
538
  cartJson = _context2.v;
520
- if (domainId) setCartCookie(Number(domainId), cartJson, undefined);
521
- _context2.n = 4;
539
+ if (domainId) setCartCookie(Number(domainId), cartJson, undefined, isTest);
540
+ _context2.n = 6;
522
541
  return attachSavedClientIfNeeded(cartJson);
523
- case 4:
542
+ case 6:
524
543
  cartJson = _context2.v;
525
544
  setCart(_objectSpread({}, cartJson));
526
545
  if (callback) callback();
527
- _context2.n = 6;
546
+ _context2.n = 8;
528
547
  break;
529
- case 5:
530
- _context2.p = 5;
548
+ case 7:
549
+ _context2.p = 7;
531
550
  _t3 = _context2.v;
532
551
  alertError(_t3.errorMessage || _t3.message || 'Unable to fetch cart.');
533
- case 6:
534
- _context2.p = 6;
552
+ case 8:
553
+ _context2.p = 8;
535
554
  setFetchingCart(false);
536
- return _context2.f(6);
537
- case 7:
555
+ return _context2.f(8);
556
+ case 9:
538
557
  return _context2.a(2);
539
558
  }
540
- }, _callee2, null, [[1, 5, 6, 7]]);
559
+ }, _callee2, null, [[1, 7, 8, 9]]);
541
560
  }));
542
561
  return _createCartAndCookie.apply(this, arguments);
543
562
  }
@@ -546,7 +565,7 @@ var CartProvider = function CartProvider(_ref) {
546
565
  }
547
566
  function _getCart() {
548
567
  _getCart = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(cartIdAndToken) {
549
- var id, _cart2, cartJson, _t4;
568
+ var id, _cart2, cartJson, isTest, _t4;
550
569
  return _regenerator().w(function (_context3) {
551
570
  while (1) switch (_context3.p = _context3.n) {
552
571
  case 0:
@@ -564,36 +583,46 @@ var CartProvider = function CartProvider(_ref) {
564
583
  return _cart2.toJson();
565
584
  case 3:
566
585
  cartJson = _context3.v;
567
- if (!stripeIsValidAndActive(cartJson)) {
586
+ isTest = readTestCheckoutFlag();
587
+ if (!(Boolean(cartJson.isTest) !== isTest)) {
568
588
  _context3.n = 5;
569
589
  break;
570
590
  }
571
591
  _context3.n = 4;
572
- return attachSavedClientIfNeeded(cartJson);
592
+ return createCartAndCookie();
573
593
  case 4:
594
+ return _context3.a(2);
595
+ case 5:
596
+ if (!(stripeIsValidAndActive(cartJson) || isTest)) {
597
+ _context3.n = 7;
598
+ break;
599
+ }
600
+ _context3.n = 6;
601
+ return attachSavedClientIfNeeded(cartJson);
602
+ case 6:
574
603
  cartJson = _context3.v;
575
604
  setCart(_objectSpread({}, cartJson));
576
- _context3.n = 6;
605
+ _context3.n = 8;
577
606
  break;
578
- case 5:
607
+ case 7:
579
608
  console.error("MErhci cart error: Stripe payment " + "options have not been correctly set up. " + "Check the company profile payment options tab " + "to set and edit stripe payment options.");
580
609
  setCartSettingsInvalid(true);
581
- case 6:
582
- _context3.n = 8;
610
+ case 8:
611
+ _context3.n = 10;
583
612
  break;
584
- case 7:
585
- _context3.p = 7;
613
+ case 9:
614
+ _context3.p = 9;
586
615
  _t4 = _context3.v;
587
- _context3.n = 8;
616
+ _context3.n = 10;
588
617
  return createCartAndCookie();
589
- case 8:
590
- _context3.p = 8;
618
+ case 10:
619
+ _context3.p = 10;
591
620
  setFetchingCart(false);
592
- return _context3.f(8);
593
- case 9:
621
+ return _context3.f(10);
622
+ case 11:
594
623
  return _context3.a(2);
595
624
  }
596
- }, _callee3, null, [[1, 7, 8, 9]]);
625
+ }, _callee3, null, [[1, 9, 10, 11]]);
597
626
  }));
598
627
  return _getCart.apply(this, arguments);
599
628
  }
@@ -956,6 +985,7 @@ var CartProvider = function CartProvider(_ref) {
956
985
  window.createCartAndCookie = createCartAndCookie;
957
986
  }
958
987
  useEffect(function () {
988
+ persistTestCheckoutFlag();
959
989
  if (domainId) actionGetMerchiCart();
960
990
  if (domainId && includeTheme) actionFetchTheme();
961
991
  }, [domainId]);
@@ -12,7 +12,7 @@ import * as React from 'react';
12
12
  import { useEffect, useState } from 'react';
13
13
  import { ButtonPay } from '../buttons';
14
14
  import FormStripeCardFields from './StripeCardFields';
15
- import { stripeCardFormSubmit, stripeInitPromise, stripePaymentButtonSubmit } from './actions';
15
+ import { fetchTestPublishableKey, stripeCardFormSubmit, stripeInitPromise, stripePaymentButtonSubmit } from './actions';
16
16
  import { companyStripePubKeyOrTestPubKey } from './utils';
17
17
  import { useCartContext } from '../CartProvider';
18
18
  import { tabIdPaymentSuccess } from '../utilities/tabs';
@@ -47,6 +47,7 @@ function StripeCardForm() {
47
47
  error = _useState2[0],
48
48
  setError = _useState2[1];
49
49
  var company = domain.company;
50
+ var isTestCheckout = Boolean(cart.isTest);
50
51
  var hasCompanyPubKey = Boolean(company.isStripeValid && companyStripePubKeyOrTestPubKey(company));
51
52
  var companyPubKey = hasCompanyPubKey ? companyStripePubKeyOrTestPubKey(company) : '';
52
53
  var canUseConnect = !!company.stripeAccountId;
@@ -133,15 +134,21 @@ function StripeCardForm() {
133
134
  }));
134
135
  return _doStripePaymentRequestForButton.apply(this, arguments);
135
136
  }
136
- var _useState7 = useState(companyPubKey),
137
+ var _useState7 = useState(isTestCheckout ? '' : companyPubKey),
137
138
  _useState8 = _slicedToArray(_useState7, 2),
138
139
  stripePublicKey = _useState8[0],
139
140
  setStripePublicKey = _useState8[1];
140
141
  useEffect(function () {
142
+ if (isTestCheckout) {
143
+ fetchTestPublishableKey(url).then(setStripePublicKey)["catch"](function () {
144
+ return setStripePublicKey('');
145
+ });
146
+ return;
147
+ }
141
148
  if (!stripePublicKey && canUseConnect) stripeInitPromise(url).then(setStripePublicKey);
142
- }, [stripePublicKey, canUseConnect]);
149
+ }, [stripePublicKey, canUseConnect, isTestCheckout, url]);
143
150
  return /*#__PURE__*/_jsxs(_Fragment, {
144
- children: [company.isTesting && badgeTestMode, /*#__PURE__*/_jsx(FormStripeCardFields, {
151
+ children: [!isTestCheckout && company.isTesting && badgeTestMode, /*#__PURE__*/_jsx(FormStripeCardFields, {
145
152
  doStripePayment: doStripePayment,
146
153
  doStripePaymentRequestForButton: doStripePaymentRequestForButton,
147
154
  cart: cart,
@@ -1,33 +1,139 @@
1
+ function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
2
+ function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); }
3
+ function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
4
+ function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
5
+ function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
6
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
7
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
8
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
9
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
10
+ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
1
11
  import * as React from 'react';
12
+ import { useEffect, useState } from 'react';
2
13
  import { StripePaymentForm, StripePaymentGate } from '@merchi/invoice';
3
14
  import LegacyStripeCardForm from './LegacyStripeCardForm';
15
+ import { completeTestPayment, fetchTestPublishableKey } from './actions';
4
16
  import { useCartContext } from '../CartProvider';
5
17
  import { tabIdPaymentSuccess } from '../utilities/tabs';
6
- import { jsx as _jsx } from "react/jsx-runtime";
18
+ import { TEST_CARD_NUMBER } from '../utilities/test_checkout';
19
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
20
+ var testCheckoutHint = /*#__PURE__*/_jsxs("div", {
21
+ style: {
22
+ marginBottom: '12px'
23
+ },
24
+ children: [/*#__PURE__*/_jsx("div", {
25
+ style: {
26
+ color: 'red',
27
+ fontWeight: 600
28
+ },
29
+ children: "Test checkout"
30
+ }), /*#__PURE__*/_jsxs("p", {
31
+ style: {
32
+ margin: '4px 0 0',
33
+ fontSize: '13px'
34
+ },
35
+ children: ["No real charge. Use card ", TEST_CARD_NUMBER, ", any future expiry, any CVC, any ZIP."]
36
+ })]
37
+ });
7
38
  export default function StripeCardForm() {
8
39
  var _useCartContext = useCartContext(),
9
40
  alertError = _useCartContext.alertError,
10
41
  apiUrl = _useCartContext.apiUrl,
11
42
  cart = _useCartContext.cart,
43
+ classNameBtnPay = _useCartContext.classNameBtnPay,
12
44
  setActiveTabIndex = _useCartContext.setActiveTabIndex,
13
45
  setInvoiceJson = _useCartContext.setInvoiceJson;
14
- return /*#__PURE__*/_jsx(StripePaymentGate, {
15
- engine: cart.stripePaymentEngine,
16
- apiUrl: apiUrl || 'https://api.merchi.co/v6/',
17
- resource: "cart",
18
- resourceId: cart.id,
19
- resourceToken: cart.token || cart.cartToken,
20
- legacy: /*#__PURE__*/_jsx(LegacyStripeCardForm, {}),
21
- children: /*#__PURE__*/_jsx(StripePaymentForm, {
22
- apiUrl: apiUrl || 'https://api.merchi.co/v6/',
46
+ var isTestCheckout = Boolean(cart.isTest);
47
+ var url = apiUrl || 'https://api.merchi.co/v6/';
48
+ var _useState = useState(false),
49
+ _useState2 = _slicedToArray(_useState, 2),
50
+ testKeyMissing = _useState2[0],
51
+ setTestKeyMissing = _useState2[1];
52
+ var _useState3 = useState(false),
53
+ _useState4 = _slicedToArray(_useState3, 2),
54
+ loadingTestPayment = _useState4[0],
55
+ setLoadingTestPayment = _useState4[1];
56
+ useEffect(function () {
57
+ if (!isTestCheckout) {
58
+ setTestKeyMissing(false);
59
+ return;
60
+ }
61
+ fetchTestPublishableKey(url).then(function () {
62
+ return setTestKeyMissing(false);
63
+ })["catch"](function () {
64
+ return setTestKeyMissing(true);
65
+ });
66
+ }, [isTestCheckout, url]);
67
+ function doCompleteTestPayment() {
68
+ return _doCompleteTestPayment.apply(this, arguments);
69
+ }
70
+ function _doCompleteTestPayment() {
71
+ _doCompleteTestPayment = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
72
+ var invoice, _t;
73
+ return _regenerator().w(function (_context) {
74
+ while (1) switch (_context.p = _context.n) {
75
+ case 0:
76
+ setLoadingTestPayment(true);
77
+ _context.p = 1;
78
+ _context.n = 2;
79
+ return completeTestPayment(url, cart);
80
+ case 2:
81
+ invoice = _context.v;
82
+ setInvoiceJson(invoice);
83
+ setActiveTabIndex(tabIdPaymentSuccess);
84
+ _context.n = 4;
85
+ break;
86
+ case 3:
87
+ _context.p = 3;
88
+ _t = _context.v;
89
+ alertError(_t.message);
90
+ case 4:
91
+ _context.p = 4;
92
+ setLoadingTestPayment(false);
93
+ return _context.f(4);
94
+ case 5:
95
+ return _context.a(2);
96
+ }
97
+ }, _callee, null, [[1, 3, 4, 5]]);
98
+ }));
99
+ return _doCompleteTestPayment.apply(this, arguments);
100
+ }
101
+ if (isTestCheckout && testKeyMissing) {
102
+ return /*#__PURE__*/_jsxs(_Fragment, {
103
+ children: [testCheckoutHint, /*#__PURE__*/_jsx("p", {
104
+ style: {
105
+ fontSize: '13px',
106
+ marginBottom: '12px'
107
+ },
108
+ children: "Stripe test keys are not configured. Complete this as a test order without a card."
109
+ }), /*#__PURE__*/_jsx("button", {
110
+ type: "button",
111
+ className: classNameBtnPay,
112
+ disabled: loadingTestPayment,
113
+ onClick: doCompleteTestPayment,
114
+ children: loadingTestPayment ? 'Completing…' : 'Complete test order'
115
+ })]
116
+ });
117
+ }
118
+ return /*#__PURE__*/_jsxs(_Fragment, {
119
+ children: [isTestCheckout && testCheckoutHint, /*#__PURE__*/_jsx(StripePaymentGate, {
120
+ engine: isTestCheckout ? 'legacy' : cart.stripePaymentEngine,
121
+ apiUrl: url,
23
122
  resource: "cart",
24
123
  resourceId: cart.id,
25
124
  resourceToken: cart.token || cart.cartToken,
26
- onError: alertError,
27
- onSuccess: function onSuccess(invoice) {
28
- setInvoiceJson(invoice);
29
- setActiveTabIndex(tabIdPaymentSuccess);
30
- }
31
- })
125
+ legacy: /*#__PURE__*/_jsx(LegacyStripeCardForm, {}),
126
+ children: /*#__PURE__*/_jsx(StripePaymentForm, {
127
+ apiUrl: url,
128
+ resource: "cart",
129
+ resourceId: cart.id,
130
+ resourceToken: cart.token || cart.cartToken,
131
+ onError: alertError,
132
+ onSuccess: function onSuccess(invoice) {
133
+ setInvoiceJson(invoice);
134
+ setActiveTabIndex(tabIdPaymentSuccess);
135
+ }
136
+ })
137
+ })]
32
138
  });
33
139
  }