@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/esm/stripe/actions.js
CHANGED
|
@@ -24,14 +24,112 @@ function _stripeInitPromise() {
|
|
|
24
24
|
}));
|
|
25
25
|
return _stripeInitPromise.apply(this, arguments);
|
|
26
26
|
}
|
|
27
|
-
function
|
|
27
|
+
export function fetchTestPublishableKey(_x2) {
|
|
28
|
+
return _fetchTestPublishableKey.apply(this, arguments);
|
|
29
|
+
}
|
|
30
|
+
function _fetchTestPublishableKey() {
|
|
31
|
+
_fetchTestPublishableKey = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(apiUrl) {
|
|
32
|
+
var response, data;
|
|
33
|
+
return _regenerator().w(function (_context2) {
|
|
34
|
+
while (1) switch (_context2.n) {
|
|
35
|
+
case 0:
|
|
36
|
+
_context2.n = 1;
|
|
37
|
+
return fetch("".concat(apiUrl, "stripe/test_publishable_key/"), {
|
|
38
|
+
method: 'GET',
|
|
39
|
+
mode: 'cors'
|
|
40
|
+
});
|
|
41
|
+
case 1:
|
|
42
|
+
response = _context2.v;
|
|
43
|
+
if (!(response && typeof response.json === 'function')) {
|
|
44
|
+
_context2.n = 5;
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
if (response.ok) {
|
|
48
|
+
_context2.n = 2;
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
throw new Error('Stripe test keys are not configured');
|
|
52
|
+
case 2:
|
|
53
|
+
_context2.n = 3;
|
|
54
|
+
return response.json();
|
|
55
|
+
case 3:
|
|
56
|
+
data = _context2.v;
|
|
57
|
+
if (!(!data || !data.key)) {
|
|
58
|
+
_context2.n = 4;
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
throw new Error('Stripe test keys are not configured');
|
|
62
|
+
case 4:
|
|
63
|
+
return _context2.a(2, data.key);
|
|
64
|
+
case 5:
|
|
65
|
+
if (!(!response || !response.key)) {
|
|
66
|
+
_context2.n = 6;
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
throw new Error('Stripe test keys are not configured');
|
|
70
|
+
case 6:
|
|
71
|
+
return _context2.a(2, response.key);
|
|
72
|
+
}
|
|
73
|
+
}, _callee2);
|
|
74
|
+
}));
|
|
75
|
+
return _fetchTestPublishableKey.apply(this, arguments);
|
|
76
|
+
}
|
|
77
|
+
export function completeTestPayment(_x3, _x4) {
|
|
78
|
+
return _completeTestPayment.apply(this, arguments);
|
|
79
|
+
}
|
|
80
|
+
function _completeTestPayment() {
|
|
81
|
+
_completeTestPayment = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(apiUrl, cart) {
|
|
82
|
+
var query, response, message, _data, data, _t;
|
|
83
|
+
return _regenerator().w(function (_context3) {
|
|
84
|
+
while (1) switch (_context3.p = _context3.n) {
|
|
85
|
+
case 0:
|
|
86
|
+
query = new URLSearchParams({
|
|
87
|
+
cart_token: cart.token
|
|
88
|
+
}).toString();
|
|
89
|
+
_context3.n = 1;
|
|
90
|
+
return fetch("".concat(apiUrl, "carts/").concat(cart.id, "/complete_test_payment/?").concat(query), {
|
|
91
|
+
method: 'POST',
|
|
92
|
+
mode: 'cors'
|
|
93
|
+
});
|
|
94
|
+
case 1:
|
|
95
|
+
response = _context3.v;
|
|
96
|
+
if (response.ok) {
|
|
97
|
+
_context3.n = 6;
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
message = 'Unable to complete the test order.';
|
|
101
|
+
_context3.p = 2;
|
|
102
|
+
_context3.n = 3;
|
|
103
|
+
return response.json();
|
|
104
|
+
case 3:
|
|
105
|
+
_data = _context3.v;
|
|
106
|
+
message = _data.message || _data.errorMessage || message;
|
|
107
|
+
_context3.n = 5;
|
|
108
|
+
break;
|
|
109
|
+
case 4:
|
|
110
|
+
_context3.p = 4;
|
|
111
|
+
_t = _context3.v;
|
|
112
|
+
case 5:
|
|
113
|
+
throw new Error(message);
|
|
114
|
+
case 6:
|
|
115
|
+
_context3.n = 7;
|
|
116
|
+
return response.json();
|
|
117
|
+
case 7:
|
|
118
|
+
data = _context3.v;
|
|
119
|
+
return _context3.a(2, data.invoice);
|
|
120
|
+
}
|
|
121
|
+
}, _callee3, null, [[2, 4]]);
|
|
122
|
+
}));
|
|
123
|
+
return _completeTestPayment.apply(this, arguments);
|
|
124
|
+
}
|
|
125
|
+
function createPaymentIntent(_x5, _x6, _x7) {
|
|
28
126
|
return _createPaymentIntent.apply(this, arguments);
|
|
29
127
|
}
|
|
30
128
|
function _createPaymentIntent() {
|
|
31
|
-
_createPaymentIntent = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function
|
|
129
|
+
_createPaymentIntent = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(apiUrl, cart, paymentMethodType) {
|
|
32
130
|
var id, token, fetchOptions, queryParams, queryString, uri, url, response;
|
|
33
|
-
return _regenerator().w(function (
|
|
34
|
-
while (1) switch (
|
|
131
|
+
return _regenerator().w(function (_context4) {
|
|
132
|
+
while (1) switch (_context4.n) {
|
|
35
133
|
case 0:
|
|
36
134
|
id = cart.id, token = cart.token;
|
|
37
135
|
fetchOptions = {
|
|
@@ -45,33 +143,33 @@ function _createPaymentIntent() {
|
|
|
45
143
|
queryString = new URLSearchParams(queryParams).toString();
|
|
46
144
|
uri = "stripe/payment_intent/cart/".concat(id, "/");
|
|
47
145
|
url = "".concat(apiUrl).concat(uri, "?").concat(queryString);
|
|
48
|
-
|
|
146
|
+
_context4.n = 1;
|
|
49
147
|
return fetch(url, fetchOptions);
|
|
50
148
|
case 1:
|
|
51
|
-
response =
|
|
149
|
+
response = _context4.v;
|
|
52
150
|
if (response.ok) {
|
|
53
|
-
|
|
151
|
+
_context4.n = 2;
|
|
54
152
|
break;
|
|
55
153
|
}
|
|
56
154
|
throw new Error("Failed to create payment intent.");
|
|
57
155
|
case 2:
|
|
58
|
-
|
|
156
|
+
_context4.n = 3;
|
|
59
157
|
return response.json();
|
|
60
158
|
case 3:
|
|
61
|
-
return
|
|
159
|
+
return _context4.a(2, _context4.v);
|
|
62
160
|
}
|
|
63
|
-
},
|
|
161
|
+
}, _callee4);
|
|
64
162
|
}));
|
|
65
163
|
return _createPaymentIntent.apply(this, arguments);
|
|
66
164
|
}
|
|
67
|
-
function checkPaymentIntnetIsComplete(
|
|
165
|
+
function checkPaymentIntnetIsComplete(_x8, _x9) {
|
|
68
166
|
return _checkPaymentIntnetIsComplete.apply(this, arguments);
|
|
69
167
|
}
|
|
70
168
|
function _checkPaymentIntnetIsComplete() {
|
|
71
|
-
_checkPaymentIntnetIsComplete = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function
|
|
169
|
+
_checkPaymentIntnetIsComplete = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(apiUrl, cart) {
|
|
72
170
|
var url, fetchOptions, queryString, response, i;
|
|
73
|
-
return _regenerator().w(function (
|
|
74
|
-
while (1) switch (
|
|
171
|
+
return _regenerator().w(function (_context5) {
|
|
172
|
+
while (1) switch (_context5.n) {
|
|
75
173
|
case 0:
|
|
76
174
|
url = "stripe/payment_intent/cart/complete/".concat(cart.id, "/");
|
|
77
175
|
fetchOptions = {
|
|
@@ -81,112 +179,112 @@ function _checkPaymentIntnetIsComplete() {
|
|
|
81
179
|
queryString = new URLSearchParams({
|
|
82
180
|
cart_token: cart.token
|
|
83
181
|
}).toString();
|
|
84
|
-
|
|
182
|
+
_context5.n = 1;
|
|
85
183
|
return fetch("".concat(apiUrl).concat(url, "?").concat(queryString), fetchOptions);
|
|
86
184
|
case 1:
|
|
87
|
-
response =
|
|
185
|
+
response = _context5.v;
|
|
88
186
|
if (response.ok) {
|
|
89
|
-
|
|
187
|
+
_context5.n = 2;
|
|
90
188
|
break;
|
|
91
189
|
}
|
|
92
190
|
throw new Error("Failed to check if payment intent is complete.");
|
|
93
191
|
case 2:
|
|
94
|
-
|
|
192
|
+
_context5.n = 3;
|
|
95
193
|
return response.json();
|
|
96
194
|
case 3:
|
|
97
|
-
i =
|
|
98
|
-
return
|
|
195
|
+
i = _context5.v;
|
|
196
|
+
return _context5.a(2, i.invoice);
|
|
99
197
|
}
|
|
100
|
-
},
|
|
198
|
+
}, _callee5);
|
|
101
199
|
}));
|
|
102
200
|
return _checkPaymentIntnetIsComplete.apply(this, arguments);
|
|
103
201
|
}
|
|
104
|
-
export function stripeCardFormSubmit(
|
|
202
|
+
export function stripeCardFormSubmit(_x0, _x1, _x10, _x11) {
|
|
105
203
|
return _stripeCardFormSubmit.apply(this, arguments);
|
|
106
204
|
}
|
|
107
205
|
function _stripeCardFormSubmit() {
|
|
108
|
-
_stripeCardFormSubmit = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function
|
|
109
|
-
var intent, stripePayment,
|
|
110
|
-
return _regenerator().w(function (
|
|
111
|
-
while (1) switch (
|
|
206
|
+
_stripeCardFormSubmit = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(apiUrl, stripe, card, cart) {
|
|
207
|
+
var intent, stripePayment, _t2;
|
|
208
|
+
return _regenerator().w(function (_context6) {
|
|
209
|
+
while (1) switch (_context6.p = _context6.n) {
|
|
112
210
|
case 0:
|
|
113
|
-
|
|
114
|
-
|
|
211
|
+
_context6.p = 0;
|
|
212
|
+
_context6.n = 1;
|
|
115
213
|
return createPaymentIntent(apiUrl, cart);
|
|
116
214
|
case 1:
|
|
117
|
-
intent =
|
|
118
|
-
|
|
215
|
+
intent = _context6.v;
|
|
216
|
+
_context6.n = 2;
|
|
119
217
|
return stripe.confirmCardPayment(intent.stripeClientSecret, {
|
|
120
218
|
payment_method: {
|
|
121
219
|
card: card
|
|
122
220
|
}
|
|
123
221
|
});
|
|
124
222
|
case 2:
|
|
125
|
-
stripePayment =
|
|
223
|
+
stripePayment = _context6.v;
|
|
126
224
|
if (!stripePayment.error) {
|
|
127
|
-
|
|
225
|
+
_context6.n = 3;
|
|
128
226
|
break;
|
|
129
227
|
}
|
|
130
228
|
throw new Error(stripePayment.error.message || 'Stripe connect unknown error');
|
|
131
229
|
case 3:
|
|
132
|
-
|
|
230
|
+
_context6.n = 4;
|
|
133
231
|
return checkPaymentIntnetIsComplete(apiUrl, cart);
|
|
134
232
|
case 4:
|
|
135
|
-
return
|
|
233
|
+
return _context6.a(2, _context6.v);
|
|
136
234
|
case 5:
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
throw
|
|
235
|
+
_context6.p = 5;
|
|
236
|
+
_t2 = _context6.v;
|
|
237
|
+
throw _t2;
|
|
140
238
|
case 6:
|
|
141
|
-
return
|
|
239
|
+
return _context6.a(2);
|
|
142
240
|
}
|
|
143
|
-
},
|
|
241
|
+
}, _callee6, null, [[0, 5]]);
|
|
144
242
|
}));
|
|
145
243
|
return _stripeCardFormSubmit.apply(this, arguments);
|
|
146
244
|
}
|
|
147
|
-
export function stripePaymentButtonSubmit(
|
|
245
|
+
export function stripePaymentButtonSubmit(_x12, _x13, _x14, _x15) {
|
|
148
246
|
return _stripePaymentButtonSubmit.apply(this, arguments);
|
|
149
247
|
}
|
|
150
248
|
function _stripePaymentButtonSubmit() {
|
|
151
|
-
_stripePaymentButtonSubmit = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function
|
|
152
|
-
var complete, paymentMethod, intent, stripePayment,
|
|
153
|
-
return _regenerator().w(function (
|
|
154
|
-
while (1) switch (
|
|
249
|
+
_stripePaymentButtonSubmit = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(apiUrl, stripe, cart, paymentMethodEvent) {
|
|
250
|
+
var complete, paymentMethod, intent, stripePayment, _t3;
|
|
251
|
+
return _regenerator().w(function (_context7) {
|
|
252
|
+
while (1) switch (_context7.p = _context7.n) {
|
|
155
253
|
case 0:
|
|
156
254
|
complete = paymentMethodEvent.complete, paymentMethod = paymentMethodEvent.paymentMethod;
|
|
157
|
-
|
|
158
|
-
|
|
255
|
+
_context7.p = 1;
|
|
256
|
+
_context7.n = 2;
|
|
159
257
|
return createPaymentIntent(apiUrl, cart);
|
|
160
258
|
case 2:
|
|
161
|
-
intent =
|
|
162
|
-
|
|
259
|
+
intent = _context7.v;
|
|
260
|
+
_context7.n = 3;
|
|
163
261
|
return stripe.confirmCardPayment(intent.stripeClientSecret, {
|
|
164
262
|
payment_method: paymentMethod.id
|
|
165
263
|
}, {
|
|
166
264
|
handleActions: false
|
|
167
265
|
});
|
|
168
266
|
case 3:
|
|
169
|
-
stripePayment =
|
|
267
|
+
stripePayment = _context7.v;
|
|
170
268
|
if (!stripePayment.error) {
|
|
171
|
-
|
|
269
|
+
_context7.n = 4;
|
|
172
270
|
break;
|
|
173
271
|
}
|
|
174
272
|
throw new Error(stripePayment.error.message || 'Stripe unknown error');
|
|
175
273
|
case 4:
|
|
176
274
|
complete('success');
|
|
177
|
-
|
|
275
|
+
_context7.n = 5;
|
|
178
276
|
return checkPaymentIntnetIsComplete(apiUrl, cart);
|
|
179
277
|
case 5:
|
|
180
|
-
return
|
|
278
|
+
return _context7.a(2, _context7.v);
|
|
181
279
|
case 6:
|
|
182
|
-
|
|
183
|
-
|
|
280
|
+
_context7.p = 6;
|
|
281
|
+
_t3 = _context7.v;
|
|
184
282
|
complete('fail');
|
|
185
|
-
throw
|
|
283
|
+
throw _t3;
|
|
186
284
|
case 7:
|
|
187
|
-
return
|
|
285
|
+
return _context7.a(2);
|
|
188
286
|
}
|
|
189
|
-
},
|
|
287
|
+
}, _callee7, null, [[1, 6]]);
|
|
190
288
|
}));
|
|
191
289
|
return _stripePaymentButtonSubmit.apply(this, arguments);
|
|
192
290
|
}
|
|
@@ -0,0 +1,120 @@
|
|
|
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
|
+
import { completeTestPayment, fetchTestPublishableKey } from './actions';
|
|
6
|
+
describe('test checkout stripe actions', function () {
|
|
7
|
+
var originalFetch = global.fetch;
|
|
8
|
+
afterEach(function () {
|
|
9
|
+
global.fetch = originalFetch;
|
|
10
|
+
});
|
|
11
|
+
it('loads the master test publishable key', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
|
|
12
|
+
return _regenerator().w(function (_context2) {
|
|
13
|
+
while (1) switch (_context2.n) {
|
|
14
|
+
case 0:
|
|
15
|
+
global.fetch = jest.fn().mockResolvedValue({
|
|
16
|
+
ok: true,
|
|
17
|
+
json: function () {
|
|
18
|
+
var _json = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
|
|
19
|
+
return _regenerator().w(function (_context) {
|
|
20
|
+
while (1) switch (_context.n) {
|
|
21
|
+
case 0:
|
|
22
|
+
return _context.a(2, {
|
|
23
|
+
key: 'pk_test_master'
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}, _callee);
|
|
27
|
+
}));
|
|
28
|
+
function json() {
|
|
29
|
+
return _json.apply(this, arguments);
|
|
30
|
+
}
|
|
31
|
+
return json;
|
|
32
|
+
}()
|
|
33
|
+
});
|
|
34
|
+
_context2.n = 1;
|
|
35
|
+
return expect(fetchTestPublishableKey('https://api.test/v6/')).resolves.toBe('pk_test_master');
|
|
36
|
+
case 1:
|
|
37
|
+
expect(global.fetch).toHaveBeenCalledWith('https://api.test/v6/stripe/test_publishable_key/', {
|
|
38
|
+
method: 'GET',
|
|
39
|
+
mode: 'cors'
|
|
40
|
+
});
|
|
41
|
+
case 2:
|
|
42
|
+
return _context2.a(2);
|
|
43
|
+
}
|
|
44
|
+
}, _callee2);
|
|
45
|
+
})));
|
|
46
|
+
it('fails over when the test-key request is rejected', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4() {
|
|
47
|
+
return _regenerator().w(function (_context4) {
|
|
48
|
+
while (1) switch (_context4.n) {
|
|
49
|
+
case 0:
|
|
50
|
+
global.fetch = jest.fn().mockResolvedValue({
|
|
51
|
+
ok: false,
|
|
52
|
+
json: function () {
|
|
53
|
+
var _json2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() {
|
|
54
|
+
return _regenerator().w(function (_context3) {
|
|
55
|
+
while (1) switch (_context3.n) {
|
|
56
|
+
case 0:
|
|
57
|
+
return _context3.a(2, {
|
|
58
|
+
message: 'missing'
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}, _callee3);
|
|
62
|
+
}));
|
|
63
|
+
function json() {
|
|
64
|
+
return _json2.apply(this, arguments);
|
|
65
|
+
}
|
|
66
|
+
return json;
|
|
67
|
+
}()
|
|
68
|
+
});
|
|
69
|
+
_context4.n = 1;
|
|
70
|
+
return expect(fetchTestPublishableKey('https://api.test/v6/')).rejects.toThrow('Stripe test keys are not configured');
|
|
71
|
+
case 1:
|
|
72
|
+
return _context4.a(2);
|
|
73
|
+
}
|
|
74
|
+
}, _callee4);
|
|
75
|
+
})));
|
|
76
|
+
it('completes a no-Stripe test payment with the cart token', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6() {
|
|
77
|
+
return _regenerator().w(function (_context6) {
|
|
78
|
+
while (1) switch (_context6.n) {
|
|
79
|
+
case 0:
|
|
80
|
+
global.fetch = jest.fn().mockResolvedValue({
|
|
81
|
+
ok: true,
|
|
82
|
+
json: function () {
|
|
83
|
+
var _json3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5() {
|
|
84
|
+
return _regenerator().w(function (_context5) {
|
|
85
|
+
while (1) switch (_context5.n) {
|
|
86
|
+
case 0:
|
|
87
|
+
return _context5.a(2, {
|
|
88
|
+
invoice: {
|
|
89
|
+
id: 9,
|
|
90
|
+
isTest: true
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}, _callee5);
|
|
95
|
+
}));
|
|
96
|
+
function json() {
|
|
97
|
+
return _json3.apply(this, arguments);
|
|
98
|
+
}
|
|
99
|
+
return json;
|
|
100
|
+
}()
|
|
101
|
+
});
|
|
102
|
+
_context6.n = 1;
|
|
103
|
+
return expect(completeTestPayment('https://api.test/v6/', {
|
|
104
|
+
id: 4,
|
|
105
|
+
token: 'tok'
|
|
106
|
+
})).resolves.toEqual({
|
|
107
|
+
id: 9,
|
|
108
|
+
isTest: true
|
|
109
|
+
});
|
|
110
|
+
case 1:
|
|
111
|
+
expect(global.fetch).toHaveBeenCalledWith('https://api.test/v6/carts/4/complete_test_payment/?cart_token=tok', {
|
|
112
|
+
method: 'POST',
|
|
113
|
+
mode: 'cors'
|
|
114
|
+
});
|
|
115
|
+
case 2:
|
|
116
|
+
return _context6.a(2);
|
|
117
|
+
}
|
|
118
|
+
}, _callee6);
|
|
119
|
+
})));
|
|
120
|
+
});
|
package/esm/utilities/cookie.js
CHANGED
|
@@ -3,6 +3,7 @@ function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try {
|
|
|
3
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
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
5
|
import { isBrowser } from "browser-or-node";
|
|
6
|
+
import { cartCookieName, readTestCheckoutFlag } from "./test_checkout";
|
|
6
7
|
export function getCookie(_x, _x2) {
|
|
7
8
|
return _getCookie.apply(this, arguments);
|
|
8
9
|
}
|
|
@@ -52,17 +53,18 @@ function _getCookie() {
|
|
|
52
53
|
}));
|
|
53
54
|
return _getCookie.apply(this, arguments);
|
|
54
55
|
}
|
|
55
|
-
export function getCartCookie(_x3) {
|
|
56
|
+
export function getCartCookie(_x3, _x4) {
|
|
56
57
|
return _getCartCookie.apply(this, arguments);
|
|
57
58
|
}
|
|
58
59
|
function _getCartCookie() {
|
|
59
|
-
_getCartCookie = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(domainId) {
|
|
60
|
-
var idAndToken;
|
|
60
|
+
_getCartCookie = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(domainId, isTest) {
|
|
61
|
+
var test, idAndToken;
|
|
61
62
|
return _regenerator().w(function (_context3) {
|
|
62
63
|
while (1) switch (_context3.n) {
|
|
63
64
|
case 0:
|
|
65
|
+
test = isTest !== null && isTest !== void 0 ? isTest : readTestCheckoutFlag();
|
|
64
66
|
_context3.n = 1;
|
|
65
|
-
return getCookie(
|
|
67
|
+
return getCookie(cartCookieName(domainId, test), null);
|
|
66
68
|
case 1:
|
|
67
69
|
idAndToken = _context3.v;
|
|
68
70
|
return _context3.a(2, idAndToken ? idAndToken.split(',') : null);
|
|
@@ -71,11 +73,11 @@ function _getCartCookie() {
|
|
|
71
73
|
}));
|
|
72
74
|
return _getCartCookie.apply(this, arguments);
|
|
73
75
|
}
|
|
74
|
-
export function getCartCookieToken(
|
|
76
|
+
export function getCartCookieToken(_x5, _x6) {
|
|
75
77
|
return _getCartCookieToken.apply(this, arguments);
|
|
76
78
|
}
|
|
77
79
|
function _getCartCookieToken() {
|
|
78
|
-
_getCartCookieToken = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(domainId) {
|
|
80
|
+
_getCartCookieToken = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(domainId, isTest) {
|
|
79
81
|
var cartIdAndToken, _t;
|
|
80
82
|
return _regenerator().w(function (_context4) {
|
|
81
83
|
while (1) switch (_context4.n) {
|
|
@@ -85,7 +87,7 @@ function _getCartCookieToken() {
|
|
|
85
87
|
break;
|
|
86
88
|
}
|
|
87
89
|
_context4.n = 1;
|
|
88
|
-
return getCartCookie(Number(domainId));
|
|
90
|
+
return getCartCookie(Number(domainId), isTest);
|
|
89
91
|
case 1:
|
|
90
92
|
_t = _context4.v;
|
|
91
93
|
_context4.n = 3;
|
|
@@ -100,7 +102,7 @@ function _getCartCookieToken() {
|
|
|
100
102
|
}));
|
|
101
103
|
return _getCartCookieToken.apply(this, arguments);
|
|
102
104
|
}
|
|
103
|
-
function setSessionCookie(
|
|
105
|
+
function setSessionCookie(_x7, _x8, _x9) {
|
|
104
106
|
return _setSessionCookie.apply(this, arguments);
|
|
105
107
|
}
|
|
106
108
|
function _setSessionCookie() {
|
|
@@ -137,20 +139,21 @@ function _setSessionCookie() {
|
|
|
137
139
|
return _setSessionCookie.apply(this, arguments);
|
|
138
140
|
}
|
|
139
141
|
export var setCartCookie = /*#__PURE__*/function () {
|
|
140
|
-
var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(storeId, cart, domain) {
|
|
141
|
-
var cookieValue;
|
|
142
|
+
var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(storeId, cart, domain, isTest) {
|
|
143
|
+
var test, cookieValue;
|
|
142
144
|
return _regenerator().w(function (_context) {
|
|
143
145
|
while (1) switch (_context.n) {
|
|
144
146
|
case 0:
|
|
147
|
+
test = isTest !== null && isTest !== void 0 ? isTest : cart ? Boolean(cart.isTest) : readTestCheckoutFlag();
|
|
145
148
|
cookieValue = cart ? cart.id + ',' + cart.token : '';
|
|
146
149
|
_context.n = 1;
|
|
147
|
-
return setSessionCookie(
|
|
150
|
+
return setSessionCookie(cartCookieName(storeId, test), cookieValue, domain);
|
|
148
151
|
case 1:
|
|
149
152
|
return _context.a(2);
|
|
150
153
|
}
|
|
151
154
|
}, _callee);
|
|
152
155
|
}));
|
|
153
|
-
return function setCartCookie(
|
|
156
|
+
return function setCartCookie(_x0, _x1, _x10, _x11) {
|
|
154
157
|
return _ref.apply(this, arguments);
|
|
155
158
|
};
|
|
156
159
|
}();
|
package/esm/utilities/index.js
CHANGED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export var TEST_CHECKOUT_QUERY = 'merchi_test_checkout';
|
|
2
|
+
export var TEST_CHECKOUT_STORAGE_KEY = 'merchi_test_checkout';
|
|
3
|
+
export var TEST_CARD_NUMBER = '4242 4242 4242 4242';
|
|
4
|
+
export function readTestCheckoutFlag() {
|
|
5
|
+
if (typeof window === 'undefined') {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
try {
|
|
9
|
+
var params = new URLSearchParams(window.location.search);
|
|
10
|
+
if (params.get(TEST_CHECKOUT_QUERY) === '1') {
|
|
11
|
+
window.localStorage.setItem(TEST_CHECKOUT_STORAGE_KEY, '1');
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
return window.localStorage.getItem(TEST_CHECKOUT_STORAGE_KEY) === '1';
|
|
15
|
+
} catch (_unused) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function persistTestCheckoutFlag() {
|
|
20
|
+
return readTestCheckoutFlag();
|
|
21
|
+
}
|
|
22
|
+
export function cartCookieName(domainId) {
|
|
23
|
+
var isTest = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
24
|
+
return isTest ? "cart-".concat(domainId, "-test") : "cart-".concat(domainId);
|
|
25
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { TEST_CARD_NUMBER, TEST_CHECKOUT_QUERY, TEST_CHECKOUT_STORAGE_KEY, cartCookieName, readTestCheckoutFlag } from './test_checkout';
|
|
2
|
+
describe('test checkout helpers', function () {
|
|
3
|
+
var originalLocation = window.location;
|
|
4
|
+
beforeEach(function () {
|
|
5
|
+
window.localStorage.clear();
|
|
6
|
+
delete window.location;
|
|
7
|
+
window.location = {
|
|
8
|
+
search: ''
|
|
9
|
+
};
|
|
10
|
+
});
|
|
11
|
+
afterEach(function () {
|
|
12
|
+
window.location = originalLocation;
|
|
13
|
+
window.localStorage.clear();
|
|
14
|
+
});
|
|
15
|
+
it('builds a separate cookie name for test carts', function () {
|
|
16
|
+
expect(cartCookieName(12)).toBe('cart-12');
|
|
17
|
+
expect(cartCookieName(12, true)).toBe('cart-12-test');
|
|
18
|
+
});
|
|
19
|
+
it('persists the query flag and exposes the test card number', function () {
|
|
20
|
+
window.location = {
|
|
21
|
+
search: "?".concat(TEST_CHECKOUT_QUERY, "=1")
|
|
22
|
+
};
|
|
23
|
+
expect(readTestCheckoutFlag()).toBe(true);
|
|
24
|
+
expect(window.localStorage.getItem(TEST_CHECKOUT_STORAGE_KEY)).toBe('1');
|
|
25
|
+
expect(TEST_CARD_NUMBER).toBe('4242 4242 4242 4242');
|
|
26
|
+
});
|
|
27
|
+
it('reads the stored flag when the query is absent', function () {
|
|
28
|
+
window.localStorage.setItem(TEST_CHECKOUT_STORAGE_KEY, '1');
|
|
29
|
+
expect(readTestCheckoutFlag()).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
});
|