@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.
- package/esm/CartProvider.js +61 -31
- package/esm/stripe/LegacyStripeCardForm.js +11 -4
- package/esm/stripe/StripeCardForm.js +122 -16
- package/esm/stripe/actions.js +155 -57
- package/esm/stripe/actions.test.js +120 -0
- package/esm/utilities/cookie.js +15 -12
- package/esm/utilities/index.js +1 -0
- package/esm/utilities/test_checkout.js +25 -0
- package/esm/utilities/test_checkout.test.js +31 -0
- package/lib/CartProvider.js +61 -31
- package/lib/stripe/LegacyStripeCardForm.js +10 -3
- package/lib/stripe/StripeCardForm.js +123 -17
- package/lib/stripe/actions.d.ts +2 -0
- package/lib/stripe/actions.js +157 -57
- package/lib/stripe/actions.test.d.ts +1 -0
- package/lib/utilities/cookie.d.ts +3 -3
- package/lib/utilities/cookie.js +15 -12
- package/lib/utilities/index.d.ts +1 -0
- package/lib/utilities/index.js +11 -0
- package/lib/utilities/test_checkout.d.ts +6 -0
- package/lib/utilities/test_checkout.js +34 -0
- package/lib/utilities/test_checkout.test.d.ts +1 -0
- package/package.json +8 -5
- package/src/CartProvider.tsx +25 -4
- package/src/stripe/LegacyStripeCardForm.tsx +9 -3
- package/src/stripe/StripeCardForm.tsx +92 -8
- package/src/stripe/actions.test.ts +50 -0
- package/src/stripe/actions.ts +41 -0
- package/src/utilities/cookie.ts +9 -6
- package/src/utilities/index.ts +1 -0
- package/src/utilities/test_checkout.test.ts +39 -0
- package/src/utilities/test_checkout.ts +27 -0
package/lib/stripe/actions.js
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.completeTestPayment = completeTestPayment;
|
|
7
|
+
exports.fetchTestPublishableKey = fetchTestPublishableKey;
|
|
6
8
|
exports.stripeCardFormSubmit = stripeCardFormSubmit;
|
|
7
9
|
exports.stripeInitPromise = stripeInitPromise;
|
|
8
10
|
exports.stripePaymentButtonSubmit = stripePaymentButtonSubmit;
|
|
@@ -32,14 +34,112 @@ function _stripeInitPromise() {
|
|
|
32
34
|
}));
|
|
33
35
|
return _stripeInitPromise.apply(this, arguments);
|
|
34
36
|
}
|
|
35
|
-
function
|
|
37
|
+
function fetchTestPublishableKey(_x2) {
|
|
38
|
+
return _fetchTestPublishableKey.apply(this, arguments);
|
|
39
|
+
}
|
|
40
|
+
function _fetchTestPublishableKey() {
|
|
41
|
+
_fetchTestPublishableKey = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(apiUrl) {
|
|
42
|
+
var response, data;
|
|
43
|
+
return _regenerator().w(function (_context2) {
|
|
44
|
+
while (1) switch (_context2.n) {
|
|
45
|
+
case 0:
|
|
46
|
+
_context2.n = 1;
|
|
47
|
+
return fetch("".concat(apiUrl, "stripe/test_publishable_key/"), {
|
|
48
|
+
method: 'GET',
|
|
49
|
+
mode: 'cors'
|
|
50
|
+
});
|
|
51
|
+
case 1:
|
|
52
|
+
response = _context2.v;
|
|
53
|
+
if (!(response && typeof response.json === 'function')) {
|
|
54
|
+
_context2.n = 5;
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
if (response.ok) {
|
|
58
|
+
_context2.n = 2;
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
throw new Error('Stripe test keys are not configured');
|
|
62
|
+
case 2:
|
|
63
|
+
_context2.n = 3;
|
|
64
|
+
return response.json();
|
|
65
|
+
case 3:
|
|
66
|
+
data = _context2.v;
|
|
67
|
+
if (!(!data || !data.key)) {
|
|
68
|
+
_context2.n = 4;
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
throw new Error('Stripe test keys are not configured');
|
|
72
|
+
case 4:
|
|
73
|
+
return _context2.a(2, data.key);
|
|
74
|
+
case 5:
|
|
75
|
+
if (!(!response || !response.key)) {
|
|
76
|
+
_context2.n = 6;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
throw new Error('Stripe test keys are not configured');
|
|
80
|
+
case 6:
|
|
81
|
+
return _context2.a(2, response.key);
|
|
82
|
+
}
|
|
83
|
+
}, _callee2);
|
|
84
|
+
}));
|
|
85
|
+
return _fetchTestPublishableKey.apply(this, arguments);
|
|
86
|
+
}
|
|
87
|
+
function completeTestPayment(_x3, _x4) {
|
|
88
|
+
return _completeTestPayment.apply(this, arguments);
|
|
89
|
+
}
|
|
90
|
+
function _completeTestPayment() {
|
|
91
|
+
_completeTestPayment = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(apiUrl, cart) {
|
|
92
|
+
var query, response, message, _data, data, _t;
|
|
93
|
+
return _regenerator().w(function (_context3) {
|
|
94
|
+
while (1) switch (_context3.p = _context3.n) {
|
|
95
|
+
case 0:
|
|
96
|
+
query = new URLSearchParams({
|
|
97
|
+
cart_token: cart.token
|
|
98
|
+
}).toString();
|
|
99
|
+
_context3.n = 1;
|
|
100
|
+
return fetch("".concat(apiUrl, "carts/").concat(cart.id, "/complete_test_payment/?").concat(query), {
|
|
101
|
+
method: 'POST',
|
|
102
|
+
mode: 'cors'
|
|
103
|
+
});
|
|
104
|
+
case 1:
|
|
105
|
+
response = _context3.v;
|
|
106
|
+
if (response.ok) {
|
|
107
|
+
_context3.n = 6;
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
message = 'Unable to complete the test order.';
|
|
111
|
+
_context3.p = 2;
|
|
112
|
+
_context3.n = 3;
|
|
113
|
+
return response.json();
|
|
114
|
+
case 3:
|
|
115
|
+
_data = _context3.v;
|
|
116
|
+
message = _data.message || _data.errorMessage || message;
|
|
117
|
+
_context3.n = 5;
|
|
118
|
+
break;
|
|
119
|
+
case 4:
|
|
120
|
+
_context3.p = 4;
|
|
121
|
+
_t = _context3.v;
|
|
122
|
+
case 5:
|
|
123
|
+
throw new Error(message);
|
|
124
|
+
case 6:
|
|
125
|
+
_context3.n = 7;
|
|
126
|
+
return response.json();
|
|
127
|
+
case 7:
|
|
128
|
+
data = _context3.v;
|
|
129
|
+
return _context3.a(2, data.invoice);
|
|
130
|
+
}
|
|
131
|
+
}, _callee3, null, [[2, 4]]);
|
|
132
|
+
}));
|
|
133
|
+
return _completeTestPayment.apply(this, arguments);
|
|
134
|
+
}
|
|
135
|
+
function createPaymentIntent(_x5, _x6, _x7) {
|
|
36
136
|
return _createPaymentIntent.apply(this, arguments);
|
|
37
137
|
}
|
|
38
138
|
function _createPaymentIntent() {
|
|
39
|
-
_createPaymentIntent = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function
|
|
139
|
+
_createPaymentIntent = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(apiUrl, cart, paymentMethodType) {
|
|
40
140
|
var id, token, fetchOptions, queryParams, queryString, uri, url, response;
|
|
41
|
-
return _regenerator().w(function (
|
|
42
|
-
while (1) switch (
|
|
141
|
+
return _regenerator().w(function (_context4) {
|
|
142
|
+
while (1) switch (_context4.n) {
|
|
43
143
|
case 0:
|
|
44
144
|
id = cart.id, token = cart.token;
|
|
45
145
|
fetchOptions = {
|
|
@@ -53,33 +153,33 @@ function _createPaymentIntent() {
|
|
|
53
153
|
queryString = new URLSearchParams(queryParams).toString();
|
|
54
154
|
uri = "stripe/payment_intent/cart/".concat(id, "/");
|
|
55
155
|
url = "".concat(apiUrl).concat(uri, "?").concat(queryString);
|
|
56
|
-
|
|
156
|
+
_context4.n = 1;
|
|
57
157
|
return fetch(url, fetchOptions);
|
|
58
158
|
case 1:
|
|
59
|
-
response =
|
|
159
|
+
response = _context4.v;
|
|
60
160
|
if (response.ok) {
|
|
61
|
-
|
|
161
|
+
_context4.n = 2;
|
|
62
162
|
break;
|
|
63
163
|
}
|
|
64
164
|
throw new Error("Failed to create payment intent.");
|
|
65
165
|
case 2:
|
|
66
|
-
|
|
166
|
+
_context4.n = 3;
|
|
67
167
|
return response.json();
|
|
68
168
|
case 3:
|
|
69
|
-
return
|
|
169
|
+
return _context4.a(2, _context4.v);
|
|
70
170
|
}
|
|
71
|
-
},
|
|
171
|
+
}, _callee4);
|
|
72
172
|
}));
|
|
73
173
|
return _createPaymentIntent.apply(this, arguments);
|
|
74
174
|
}
|
|
75
|
-
function checkPaymentIntnetIsComplete(
|
|
175
|
+
function checkPaymentIntnetIsComplete(_x8, _x9) {
|
|
76
176
|
return _checkPaymentIntnetIsComplete.apply(this, arguments);
|
|
77
177
|
}
|
|
78
178
|
function _checkPaymentIntnetIsComplete() {
|
|
79
|
-
_checkPaymentIntnetIsComplete = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function
|
|
179
|
+
_checkPaymentIntnetIsComplete = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(apiUrl, cart) {
|
|
80
180
|
var url, fetchOptions, queryString, response, i;
|
|
81
|
-
return _regenerator().w(function (
|
|
82
|
-
while (1) switch (
|
|
181
|
+
return _regenerator().w(function (_context5) {
|
|
182
|
+
while (1) switch (_context5.n) {
|
|
83
183
|
case 0:
|
|
84
184
|
url = "stripe/payment_intent/cart/complete/".concat(cart.id, "/");
|
|
85
185
|
fetchOptions = {
|
|
@@ -89,112 +189,112 @@ function _checkPaymentIntnetIsComplete() {
|
|
|
89
189
|
queryString = new URLSearchParams({
|
|
90
190
|
cart_token: cart.token
|
|
91
191
|
}).toString();
|
|
92
|
-
|
|
192
|
+
_context5.n = 1;
|
|
93
193
|
return fetch("".concat(apiUrl).concat(url, "?").concat(queryString), fetchOptions);
|
|
94
194
|
case 1:
|
|
95
|
-
response =
|
|
195
|
+
response = _context5.v;
|
|
96
196
|
if (response.ok) {
|
|
97
|
-
|
|
197
|
+
_context5.n = 2;
|
|
98
198
|
break;
|
|
99
199
|
}
|
|
100
200
|
throw new Error("Failed to check if payment intent is complete.");
|
|
101
201
|
case 2:
|
|
102
|
-
|
|
202
|
+
_context5.n = 3;
|
|
103
203
|
return response.json();
|
|
104
204
|
case 3:
|
|
105
|
-
i =
|
|
106
|
-
return
|
|
205
|
+
i = _context5.v;
|
|
206
|
+
return _context5.a(2, i.invoice);
|
|
107
207
|
}
|
|
108
|
-
},
|
|
208
|
+
}, _callee5);
|
|
109
209
|
}));
|
|
110
210
|
return _checkPaymentIntnetIsComplete.apply(this, arguments);
|
|
111
211
|
}
|
|
112
|
-
function stripeCardFormSubmit(
|
|
212
|
+
function stripeCardFormSubmit(_x0, _x1, _x10, _x11) {
|
|
113
213
|
return _stripeCardFormSubmit.apply(this, arguments);
|
|
114
214
|
}
|
|
115
215
|
function _stripeCardFormSubmit() {
|
|
116
|
-
_stripeCardFormSubmit = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function
|
|
117
|
-
var intent, stripePayment,
|
|
118
|
-
return _regenerator().w(function (
|
|
119
|
-
while (1) switch (
|
|
216
|
+
_stripeCardFormSubmit = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(apiUrl, stripe, card, cart) {
|
|
217
|
+
var intent, stripePayment, _t2;
|
|
218
|
+
return _regenerator().w(function (_context6) {
|
|
219
|
+
while (1) switch (_context6.p = _context6.n) {
|
|
120
220
|
case 0:
|
|
121
|
-
|
|
122
|
-
|
|
221
|
+
_context6.p = 0;
|
|
222
|
+
_context6.n = 1;
|
|
123
223
|
return createPaymentIntent(apiUrl, cart);
|
|
124
224
|
case 1:
|
|
125
|
-
intent =
|
|
126
|
-
|
|
225
|
+
intent = _context6.v;
|
|
226
|
+
_context6.n = 2;
|
|
127
227
|
return stripe.confirmCardPayment(intent.stripeClientSecret, {
|
|
128
228
|
payment_method: {
|
|
129
229
|
card: card
|
|
130
230
|
}
|
|
131
231
|
});
|
|
132
232
|
case 2:
|
|
133
|
-
stripePayment =
|
|
233
|
+
stripePayment = _context6.v;
|
|
134
234
|
if (!stripePayment.error) {
|
|
135
|
-
|
|
235
|
+
_context6.n = 3;
|
|
136
236
|
break;
|
|
137
237
|
}
|
|
138
238
|
throw new Error(stripePayment.error.message || 'Stripe connect unknown error');
|
|
139
239
|
case 3:
|
|
140
|
-
|
|
240
|
+
_context6.n = 4;
|
|
141
241
|
return checkPaymentIntnetIsComplete(apiUrl, cart);
|
|
142
242
|
case 4:
|
|
143
|
-
return
|
|
243
|
+
return _context6.a(2, _context6.v);
|
|
144
244
|
case 5:
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
throw
|
|
245
|
+
_context6.p = 5;
|
|
246
|
+
_t2 = _context6.v;
|
|
247
|
+
throw _t2;
|
|
148
248
|
case 6:
|
|
149
|
-
return
|
|
249
|
+
return _context6.a(2);
|
|
150
250
|
}
|
|
151
|
-
},
|
|
251
|
+
}, _callee6, null, [[0, 5]]);
|
|
152
252
|
}));
|
|
153
253
|
return _stripeCardFormSubmit.apply(this, arguments);
|
|
154
254
|
}
|
|
155
|
-
function stripePaymentButtonSubmit(
|
|
255
|
+
function stripePaymentButtonSubmit(_x12, _x13, _x14, _x15) {
|
|
156
256
|
return _stripePaymentButtonSubmit.apply(this, arguments);
|
|
157
257
|
}
|
|
158
258
|
function _stripePaymentButtonSubmit() {
|
|
159
|
-
_stripePaymentButtonSubmit = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function
|
|
160
|
-
var complete, paymentMethod, intent, stripePayment,
|
|
161
|
-
return _regenerator().w(function (
|
|
162
|
-
while (1) switch (
|
|
259
|
+
_stripePaymentButtonSubmit = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(apiUrl, stripe, cart, paymentMethodEvent) {
|
|
260
|
+
var complete, paymentMethod, intent, stripePayment, _t3;
|
|
261
|
+
return _regenerator().w(function (_context7) {
|
|
262
|
+
while (1) switch (_context7.p = _context7.n) {
|
|
163
263
|
case 0:
|
|
164
264
|
complete = paymentMethodEvent.complete, paymentMethod = paymentMethodEvent.paymentMethod;
|
|
165
|
-
|
|
166
|
-
|
|
265
|
+
_context7.p = 1;
|
|
266
|
+
_context7.n = 2;
|
|
167
267
|
return createPaymentIntent(apiUrl, cart);
|
|
168
268
|
case 2:
|
|
169
|
-
intent =
|
|
170
|
-
|
|
269
|
+
intent = _context7.v;
|
|
270
|
+
_context7.n = 3;
|
|
171
271
|
return stripe.confirmCardPayment(intent.stripeClientSecret, {
|
|
172
272
|
payment_method: paymentMethod.id
|
|
173
273
|
}, {
|
|
174
274
|
handleActions: false
|
|
175
275
|
});
|
|
176
276
|
case 3:
|
|
177
|
-
stripePayment =
|
|
277
|
+
stripePayment = _context7.v;
|
|
178
278
|
if (!stripePayment.error) {
|
|
179
|
-
|
|
279
|
+
_context7.n = 4;
|
|
180
280
|
break;
|
|
181
281
|
}
|
|
182
282
|
throw new Error(stripePayment.error.message || 'Stripe unknown error');
|
|
183
283
|
case 4:
|
|
184
284
|
complete('success');
|
|
185
|
-
|
|
285
|
+
_context7.n = 5;
|
|
186
286
|
return checkPaymentIntnetIsComplete(apiUrl, cart);
|
|
187
287
|
case 5:
|
|
188
|
-
return
|
|
288
|
+
return _context7.a(2, _context7.v);
|
|
189
289
|
case 6:
|
|
190
|
-
|
|
191
|
-
|
|
290
|
+
_context7.p = 6;
|
|
291
|
+
_t3 = _context7.v;
|
|
192
292
|
complete('fail');
|
|
193
|
-
throw
|
|
293
|
+
throw _t3;
|
|
194
294
|
case 7:
|
|
195
|
-
return
|
|
295
|
+
return _context7.a(2);
|
|
196
296
|
}
|
|
197
|
-
},
|
|
297
|
+
}, _callee7, null, [[1, 6]]);
|
|
198
298
|
}));
|
|
199
299
|
return _stripePaymentButtonSubmit.apply(this, arguments);
|
|
200
300
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export declare function getCookie(name: string, defaultValue: any): Promise<any>;
|
|
2
|
-
export declare function getCartCookie(domainId: number | string): Promise<string[]>;
|
|
3
|
-
export declare function getCartCookieToken(domainId: number | string): Promise<string>;
|
|
4
|
-
export declare const setCartCookie: (storeId: number, cart: any, domain?: any) => Promise<void>;
|
|
2
|
+
export declare function getCartCookie(domainId: number | string, isTest?: boolean): Promise<string[]>;
|
|
3
|
+
export declare function getCartCookieToken(domainId: number | string, isTest?: boolean): Promise<string>;
|
|
4
|
+
export declare const setCartCookie: (storeId: number, cart: any, domain?: any, isTest?: boolean) => Promise<void>;
|
package/lib/utilities/cookie.js
CHANGED
|
@@ -8,6 +8,7 @@ exports.getCartCookieToken = getCartCookieToken;
|
|
|
8
8
|
exports.getCookie = getCookie;
|
|
9
9
|
exports.setCartCookie = void 0;
|
|
10
10
|
var _browserOrNode = require("browser-or-node");
|
|
11
|
+
var _test_checkout = require("./test_checkout");
|
|
11
12
|
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 }; })(); }
|
|
12
13
|
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); }
|
|
13
14
|
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); }
|
|
@@ -61,17 +62,18 @@ function _getCookie() {
|
|
|
61
62
|
}));
|
|
62
63
|
return _getCookie.apply(this, arguments);
|
|
63
64
|
}
|
|
64
|
-
function getCartCookie(_x3) {
|
|
65
|
+
function getCartCookie(_x3, _x4) {
|
|
65
66
|
return _getCartCookie.apply(this, arguments);
|
|
66
67
|
}
|
|
67
68
|
function _getCartCookie() {
|
|
68
|
-
_getCartCookie = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(domainId) {
|
|
69
|
-
var idAndToken;
|
|
69
|
+
_getCartCookie = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(domainId, isTest) {
|
|
70
|
+
var test, idAndToken;
|
|
70
71
|
return _regenerator().w(function (_context3) {
|
|
71
72
|
while (1) switch (_context3.n) {
|
|
72
73
|
case 0:
|
|
74
|
+
test = isTest !== null && isTest !== void 0 ? isTest : (0, _test_checkout.readTestCheckoutFlag)();
|
|
73
75
|
_context3.n = 1;
|
|
74
|
-
return getCookie(
|
|
76
|
+
return getCookie((0, _test_checkout.cartCookieName)(domainId, test), null);
|
|
75
77
|
case 1:
|
|
76
78
|
idAndToken = _context3.v;
|
|
77
79
|
return _context3.a(2, idAndToken ? idAndToken.split(',') : null);
|
|
@@ -80,11 +82,11 @@ function _getCartCookie() {
|
|
|
80
82
|
}));
|
|
81
83
|
return _getCartCookie.apply(this, arguments);
|
|
82
84
|
}
|
|
83
|
-
function getCartCookieToken(
|
|
85
|
+
function getCartCookieToken(_x5, _x6) {
|
|
84
86
|
return _getCartCookieToken.apply(this, arguments);
|
|
85
87
|
}
|
|
86
88
|
function _getCartCookieToken() {
|
|
87
|
-
_getCartCookieToken = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(domainId) {
|
|
89
|
+
_getCartCookieToken = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(domainId, isTest) {
|
|
88
90
|
var cartIdAndToken, _t;
|
|
89
91
|
return _regenerator().w(function (_context4) {
|
|
90
92
|
while (1) switch (_context4.n) {
|
|
@@ -94,7 +96,7 @@ function _getCartCookieToken() {
|
|
|
94
96
|
break;
|
|
95
97
|
}
|
|
96
98
|
_context4.n = 1;
|
|
97
|
-
return getCartCookie(Number(domainId));
|
|
99
|
+
return getCartCookie(Number(domainId), isTest);
|
|
98
100
|
case 1:
|
|
99
101
|
_t = _context4.v;
|
|
100
102
|
_context4.n = 3;
|
|
@@ -109,7 +111,7 @@ function _getCartCookieToken() {
|
|
|
109
111
|
}));
|
|
110
112
|
return _getCartCookieToken.apply(this, arguments);
|
|
111
113
|
}
|
|
112
|
-
function setSessionCookie(
|
|
114
|
+
function setSessionCookie(_x7, _x8, _x9) {
|
|
113
115
|
return _setSessionCookie.apply(this, arguments);
|
|
114
116
|
}
|
|
115
117
|
function _setSessionCookie() {
|
|
@@ -146,20 +148,21 @@ function _setSessionCookie() {
|
|
|
146
148
|
return _setSessionCookie.apply(this, arguments);
|
|
147
149
|
}
|
|
148
150
|
var setCartCookie = exports.setCartCookie = /*#__PURE__*/function () {
|
|
149
|
-
var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(storeId, cart, domain) {
|
|
150
|
-
var cookieValue;
|
|
151
|
+
var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(storeId, cart, domain, isTest) {
|
|
152
|
+
var test, cookieValue;
|
|
151
153
|
return _regenerator().w(function (_context) {
|
|
152
154
|
while (1) switch (_context.n) {
|
|
153
155
|
case 0:
|
|
156
|
+
test = isTest !== null && isTest !== void 0 ? isTest : cart ? Boolean(cart.isTest) : (0, _test_checkout.readTestCheckoutFlag)();
|
|
154
157
|
cookieValue = cart ? cart.id + ',' + cart.token : '';
|
|
155
158
|
_context.n = 1;
|
|
156
|
-
return setSessionCookie(
|
|
159
|
+
return setSessionCookie((0, _test_checkout.cartCookieName)(storeId, test), cookieValue, domain);
|
|
157
160
|
case 1:
|
|
158
161
|
return _context.a(2);
|
|
159
162
|
}
|
|
160
163
|
}, _callee);
|
|
161
164
|
}));
|
|
162
|
-
return function setCartCookie(
|
|
165
|
+
return function setCartCookie(_x0, _x1, _x10, _x11) {
|
|
163
166
|
return _ref.apply(this, arguments);
|
|
164
167
|
};
|
|
165
168
|
}();
|
package/lib/utilities/index.d.ts
CHANGED
package/lib/utilities/index.js
CHANGED
|
@@ -135,6 +135,17 @@ Object.keys(_tabs).forEach(function (key) {
|
|
|
135
135
|
}
|
|
136
136
|
});
|
|
137
137
|
});
|
|
138
|
+
var _test_checkout = require("./test_checkout");
|
|
139
|
+
Object.keys(_test_checkout).forEach(function (key) {
|
|
140
|
+
if (key === "default" || key === "__esModule") return;
|
|
141
|
+
if (key in exports && exports[key] === _test_checkout[key]) return;
|
|
142
|
+
Object.defineProperty(exports, key, {
|
|
143
|
+
enumerable: true,
|
|
144
|
+
get: function get() {
|
|
145
|
+
return _test_checkout[key];
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
});
|
|
138
149
|
var _user = require("./user");
|
|
139
150
|
Object.keys(_user).forEach(function (key) {
|
|
140
151
|
if (key === "default" || key === "__esModule") return;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare const TEST_CHECKOUT_QUERY = "merchi_test_checkout";
|
|
2
|
+
export declare const TEST_CHECKOUT_STORAGE_KEY = "merchi_test_checkout";
|
|
3
|
+
export declare const TEST_CARD_NUMBER = "4242 4242 4242 4242";
|
|
4
|
+
export declare function readTestCheckoutFlag(): boolean;
|
|
5
|
+
export declare function persistTestCheckoutFlag(): boolean;
|
|
6
|
+
export declare function cartCookieName(domainId: number | string, isTest?: boolean): string;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.TEST_CHECKOUT_STORAGE_KEY = exports.TEST_CHECKOUT_QUERY = exports.TEST_CARD_NUMBER = void 0;
|
|
7
|
+
exports.cartCookieName = cartCookieName;
|
|
8
|
+
exports.persistTestCheckoutFlag = persistTestCheckoutFlag;
|
|
9
|
+
exports.readTestCheckoutFlag = readTestCheckoutFlag;
|
|
10
|
+
var TEST_CHECKOUT_QUERY = exports.TEST_CHECKOUT_QUERY = 'merchi_test_checkout';
|
|
11
|
+
var TEST_CHECKOUT_STORAGE_KEY = exports.TEST_CHECKOUT_STORAGE_KEY = 'merchi_test_checkout';
|
|
12
|
+
var TEST_CARD_NUMBER = exports.TEST_CARD_NUMBER = '4242 4242 4242 4242';
|
|
13
|
+
function readTestCheckoutFlag() {
|
|
14
|
+
if (typeof window === 'undefined') {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
try {
|
|
18
|
+
var params = new URLSearchParams(window.location.search);
|
|
19
|
+
if (params.get(TEST_CHECKOUT_QUERY) === '1') {
|
|
20
|
+
window.localStorage.setItem(TEST_CHECKOUT_STORAGE_KEY, '1');
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
return window.localStorage.getItem(TEST_CHECKOUT_STORAGE_KEY) === '1';
|
|
24
|
+
} catch (_unused) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function persistTestCheckoutFlag() {
|
|
29
|
+
return readTestCheckoutFlag();
|
|
30
|
+
}
|
|
31
|
+
function cartCookieName(domainId) {
|
|
32
|
+
var isTest = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
33
|
+
return isTest ? "cart-".concat(domainId, "-test") : "cart-".concat(domainId);
|
|
34
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@merchi/cart",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.5",
|
|
4
4
|
"description": "Merchi's cart",
|
|
5
5
|
"source": "src/index.ts",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"copy-assets": "copyfiles -u 1 src/assets/**/* esm && copyfiles -u 1 src/assets/**/* lib",
|
|
19
19
|
"compile-sass": "sass src/styles/globals.scss esm/styles/globals.css && sass src/styles/globals.scss lib/styles/globals.css",
|
|
20
20
|
"start": "react-scripts start",
|
|
21
|
-
"test": "
|
|
21
|
+
"test": "jest",
|
|
22
22
|
"eject": "react-scripts eject"
|
|
23
23
|
},
|
|
24
24
|
"repository": {
|
|
@@ -81,9 +81,12 @@
|
|
|
81
81
|
"copyfiles": "^2.4.1",
|
|
82
82
|
"eslint": "8.47.0",
|
|
83
83
|
"eslint-config-next": "13.4.16",
|
|
84
|
+
"jest": "28",
|
|
85
|
+
"jest-environment-jsdom": "28",
|
|
84
86
|
"react": "^18.0.0",
|
|
85
87
|
"react-dom": "^18.0.0",
|
|
86
88
|
"sass": "^1.70.0",
|
|
89
|
+
"ts-jest": "28",
|
|
87
90
|
"typescript": "^5.1.6"
|
|
88
91
|
},
|
|
89
92
|
"dependencies": {
|
|
@@ -95,9 +98,9 @@
|
|
|
95
98
|
"browser-or-node": "^3.0.0-pre.0",
|
|
96
99
|
"js-cookie": "^3.0.5",
|
|
97
100
|
"lodash": "^4.17.21",
|
|
98
|
-
"@merchi/product-form": "^1.19.
|
|
99
|
-
"@merchi/product-form-sdk": "^1.0.
|
|
100
|
-
"@merchi/sdk": "^1.
|
|
101
|
+
"@merchi/product-form": "^1.19.13",
|
|
102
|
+
"@merchi/product-form-sdk": "^1.0.10",
|
|
103
|
+
"@merchi/sdk": "^1.42.0",
|
|
101
104
|
"react-country-region-selector": "^3.6.1",
|
|
102
105
|
"react-hook-form": "7.45.4",
|
|
103
106
|
"react-icons": "^4.10.1",
|
package/src/CartProvider.tsx
CHANGED
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
saveCheckoutAddress,
|
|
21
21
|
} from './utilities/local_storage';
|
|
22
22
|
import { makeUser } from './utilities/user';
|
|
23
|
+
import { persistTestCheckoutFlag, readTestCheckoutFlag } from './utilities/test_checkout';
|
|
23
24
|
|
|
24
25
|
export interface PropsCart {
|
|
25
26
|
cart: any;
|
|
@@ -620,10 +621,24 @@ const CartProvider = ({
|
|
|
620
621
|
async function createCartAndCookie(callback?: () => void) {
|
|
621
622
|
setFetchingCart(true);
|
|
622
623
|
try {
|
|
623
|
-
const
|
|
624
|
-
|
|
624
|
+
const isTest = readTestCheckoutFlag();
|
|
625
|
+
const cart = makeCart({domain: {id: domainId}, isTest}, true);
|
|
626
|
+
if (isTest) {
|
|
627
|
+
const created = await merchi.authenticatedFetch('/carts/', {
|
|
628
|
+
method: 'POST',
|
|
629
|
+
body: cart.toFormData(),
|
|
630
|
+
query: [
|
|
631
|
+
['embed', JSON.stringify(cartEmbed)],
|
|
632
|
+
['skip_rights', 'y'],
|
|
633
|
+
['is_test', 'true'],
|
|
634
|
+
],
|
|
635
|
+
});
|
|
636
|
+
cart.fromJson(created.cart);
|
|
637
|
+
} else {
|
|
638
|
+
await cart.create({embed: cartEmbed});
|
|
639
|
+
}
|
|
625
640
|
let cartJson = await cart.toJson();
|
|
626
|
-
if (domainId) setCartCookie(Number(domainId), cartJson, undefined);
|
|
641
|
+
if (domainId) setCartCookie(Number(domainId), cartJson, undefined, isTest);
|
|
627
642
|
cartJson = await attachSavedClientIfNeeded(cartJson);
|
|
628
643
|
setCart({...cartJson});
|
|
629
644
|
if (callback) callback();
|
|
@@ -643,7 +658,12 @@ const CartProvider = ({
|
|
|
643
658
|
try {
|
|
644
659
|
const cart = await merchi.Cart.get(id, {embed: cartEmbed});
|
|
645
660
|
let cartJson = await cart.toJson();
|
|
646
|
-
|
|
661
|
+
const isTest = readTestCheckoutFlag();
|
|
662
|
+
if (Boolean(cartJson.isTest) !== isTest) {
|
|
663
|
+
await createCartAndCookie();
|
|
664
|
+
return;
|
|
665
|
+
}
|
|
666
|
+
if (stripeIsValidAndActive(cartJson) || isTest) {
|
|
647
667
|
cartJson = await attachSavedClientIfNeeded(cartJson);
|
|
648
668
|
setCart({...cartJson});
|
|
649
669
|
} else {
|
|
@@ -833,6 +853,7 @@ const CartProvider = ({
|
|
|
833
853
|
}
|
|
834
854
|
|
|
835
855
|
useEffect(() => {
|
|
856
|
+
persistTestCheckoutFlag();
|
|
836
857
|
if (domainId) actionGetMerchiCart();
|
|
837
858
|
if (domainId && includeTheme) actionFetchTheme();
|
|
838
859
|
}, [domainId]);
|
|
@@ -3,6 +3,7 @@ import { useEffect, useState } from 'react';
|
|
|
3
3
|
import { ButtonPay } from '../buttons';
|
|
4
4
|
import FormStripeCardFields from './StripeCardFields';
|
|
5
5
|
import {
|
|
6
|
+
fetchTestPublishableKey,
|
|
6
7
|
stripeCardFormSubmit,
|
|
7
8
|
stripeInitPromise,
|
|
8
9
|
stripePaymentButtonSubmit,
|
|
@@ -28,6 +29,7 @@ function StripeCardForm() {
|
|
|
28
29
|
const { domain } = cart;
|
|
29
30
|
const [error, setError] = useState(({} as any));
|
|
30
31
|
const company = domain.company;
|
|
32
|
+
const isTestCheckout = Boolean(cart.isTest);
|
|
31
33
|
const hasCompanyPubKey = Boolean(company.isStripeValid && companyStripePubKeyOrTestPubKey(company));
|
|
32
34
|
const companyPubKey = hasCompanyPubKey ? companyStripePubKeyOrTestPubKey(company) : '';
|
|
33
35
|
const canUseConnect = !!company.stripeAccountId;
|
|
@@ -66,13 +68,17 @@ function StripeCardForm() {
|
|
|
66
68
|
setError({message: e.errorMessage || e.message || 'Unable to process card.'});
|
|
67
69
|
}
|
|
68
70
|
}
|
|
69
|
-
const [stripePublicKey, setStripePublicKey] = useState(companyPubKey);
|
|
71
|
+
const [stripePublicKey, setStripePublicKey] = useState(isTestCheckout ? '' : companyPubKey);
|
|
70
72
|
useEffect(() => {
|
|
73
|
+
if (isTestCheckout) {
|
|
74
|
+
fetchTestPublishableKey(url).then(setStripePublicKey).catch(() => setStripePublicKey(''));
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
71
77
|
if (!stripePublicKey && canUseConnect) stripeInitPromise(url).then(setStripePublicKey)
|
|
72
|
-
}, [stripePublicKey, canUseConnect]);
|
|
78
|
+
}, [stripePublicKey, canUseConnect, isTestCheckout, url]);
|
|
73
79
|
return (
|
|
74
80
|
<>
|
|
75
|
-
{company.isTesting && badgeTestMode}
|
|
81
|
+
{!isTestCheckout && company.isTesting && badgeTestMode}
|
|
76
82
|
<FormStripeCardFields
|
|
77
83
|
doStripePayment={doStripePayment}
|
|
78
84
|
doStripePaymentRequestForButton={doStripePaymentRequestForButton}
|