@merchi/cart 1.4.69 → 1.4.71

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.
@@ -127,7 +127,7 @@ function createPaymentIntent(_x5, _x6, _x7) {
127
127
  }
128
128
  function _createPaymentIntent() {
129
129
  _createPaymentIntent = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(apiUrl, cart, paymentMethodType) {
130
- var id, token, fetchOptions, queryParams, queryString, uri, url, response;
130
+ var id, token, fetchOptions, queryParams, queryString, uri, url, response, data;
131
131
  return _regenerator().w(function (_context4) {
132
132
  while (1) switch (_context4.n) {
133
133
  case 0:
@@ -147,29 +147,118 @@ function _createPaymentIntent() {
147
147
  return fetch(url, fetchOptions);
148
148
  case 1:
149
149
  response = _context4.v;
150
+ _context4.n = 2;
151
+ return response.json();
152
+ case 2:
153
+ data = _context4.v;
150
154
  if (response.ok) {
151
- _context4.n = 2;
155
+ _context4.n = 3;
152
156
  break;
153
157
  }
154
- throw new Error("Failed to create payment intent.");
155
- case 2:
156
- _context4.n = 3;
157
- return response.json();
158
+ throw new Error(data.message || "Failed to create payment intent.");
158
159
  case 3:
159
- return _context4.a(2, _context4.v);
160
+ return _context4.a(2, data);
160
161
  }
161
162
  }, _callee4);
162
163
  }));
163
164
  return _createPaymentIntent.apply(this, arguments);
164
165
  }
165
- function checkPaymentIntnetIsComplete(_x8, _x9) {
166
+ function completeZeroBalanceCheckoutLegacy(_x8, _x9) {
167
+ return _completeZeroBalanceCheckoutLegacy.apply(this, arguments);
168
+ }
169
+ function _completeZeroBalanceCheckoutLegacy() {
170
+ _completeZeroBalanceCheckoutLegacy = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(apiUrl, cart) {
171
+ var intent;
172
+ return _regenerator().w(function (_context5) {
173
+ while (1) switch (_context5.n) {
174
+ case 0:
175
+ _context5.n = 1;
176
+ return createPaymentIntent(apiUrl, cart);
177
+ case 1:
178
+ intent = _context5.v;
179
+ if (intent.zeroBalance) {
180
+ _context5.n = 2;
181
+ break;
182
+ }
183
+ throw new Error('Unable to complete this order.');
184
+ case 2:
185
+ _context5.n = 3;
186
+ return checkPaymentIntnetIsComplete(apiUrl, cart);
187
+ case 3:
188
+ return _context5.a(2, _context5.v);
189
+ }
190
+ }, _callee5);
191
+ }));
192
+ return _completeZeroBalanceCheckoutLegacy.apply(this, arguments);
193
+ }
194
+ function completeZeroBalanceCheckout(_x0, _x1) {
195
+ return _completeZeroBalanceCheckout.apply(this, arguments);
196
+ }
197
+ function _completeZeroBalanceCheckout() {
198
+ _completeZeroBalanceCheckout = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(apiUrl, cart) {
199
+ var url, query, response, data, message;
200
+ return _regenerator().w(function (_context6) {
201
+ while (1) switch (_context6.n) {
202
+ case 0:
203
+ url = "".concat(apiUrl.replace(/\/$/, ''), "/payments/stripe/cart/").concat(cart.id, "/attempts/");
204
+ query = new URLSearchParams({
205
+ cart_token: cart.token
206
+ }).toString();
207
+ _context6.n = 1;
208
+ return fetch("".concat(url, "?").concat(query), {
209
+ method: 'POST',
210
+ mode: 'cors',
211
+ headers: {
212
+ 'Content-Type': 'application/json'
213
+ },
214
+ body: JSON.stringify({
215
+ requestKey: "zero-balance:".concat(cart.id, ":").concat(Date.now())
216
+ })
217
+ });
218
+ case 1:
219
+ response = _context6.v;
220
+ _context6.n = 2;
221
+ return response.json();
222
+ case 2:
223
+ data = _context6.v;
224
+ if (!(response.ok && data.invoice)) {
225
+ _context6.n = 3;
226
+ break;
227
+ }
228
+ return _context6.a(2, data.invoice);
229
+ case 3:
230
+ message = data.message || '';
231
+ if (!(response.status === 409 && message.toLowerCase().includes('new payment flow is disabled'))) {
232
+ _context6.n = 5;
233
+ break;
234
+ }
235
+ _context6.n = 4;
236
+ return completeZeroBalanceCheckoutLegacy(apiUrl, cart);
237
+ case 4:
238
+ return _context6.a(2, _context6.v);
239
+ case 5:
240
+ if (response.ok) {
241
+ _context6.n = 6;
242
+ break;
243
+ }
244
+ throw new Error(message || 'Unable to complete this order.');
245
+ case 6:
246
+ throw new Error('Unable to complete this order.');
247
+ case 7:
248
+ return _context6.a(2);
249
+ }
250
+ }, _callee6);
251
+ }));
252
+ return _completeZeroBalanceCheckout.apply(this, arguments);
253
+ }
254
+ function checkPaymentIntnetIsComplete(_x10, _x11) {
166
255
  return _checkPaymentIntnetIsComplete.apply(this, arguments);
167
256
  }
168
257
  function _checkPaymentIntnetIsComplete() {
169
- _checkPaymentIntnetIsComplete = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(apiUrl, cart) {
258
+ _checkPaymentIntnetIsComplete = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(apiUrl, cart) {
170
259
  var url, fetchOptions, queryString, response, i;
171
- return _regenerator().w(function (_context5) {
172
- while (1) switch (_context5.n) {
260
+ return _regenerator().w(function (_context7) {
261
+ while (1) switch (_context7.n) {
173
262
  case 0:
174
263
  url = "stripe/payment_intent/cart/complete/".concat(cart.id, "/");
175
264
  fetchOptions = {
@@ -179,112 +268,148 @@ function _checkPaymentIntnetIsComplete() {
179
268
  queryString = new URLSearchParams({
180
269
  cart_token: cart.token
181
270
  }).toString();
182
- _context5.n = 1;
271
+ _context7.n = 1;
183
272
  return fetch("".concat(apiUrl).concat(url, "?").concat(queryString), fetchOptions);
184
273
  case 1:
185
- response = _context5.v;
274
+ response = _context7.v;
186
275
  if (response.ok) {
187
- _context5.n = 2;
276
+ _context7.n = 2;
188
277
  break;
189
278
  }
190
279
  throw new Error("Failed to check if payment intent is complete.");
191
280
  case 2:
192
- _context5.n = 3;
281
+ _context7.n = 3;
193
282
  return response.json();
194
283
  case 3:
195
- i = _context5.v;
196
- return _context5.a(2, i.invoice);
284
+ i = _context7.v;
285
+ return _context7.a(2, i.invoice);
197
286
  }
198
- }, _callee5);
287
+ }, _callee7);
199
288
  }));
200
289
  return _checkPaymentIntnetIsComplete.apply(this, arguments);
201
290
  }
202
- export function stripeCardFormSubmit(_x0, _x1, _x10, _x11) {
291
+ export function stripeCardFormSubmit(_x12, _x13, _x14, _x15) {
203
292
  return _stripeCardFormSubmit.apply(this, arguments);
204
293
  }
205
294
  function _stripeCardFormSubmit() {
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) {
295
+ _stripeCardFormSubmit = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(apiUrl, stripe, card, cart) {
296
+ var _cart$totalCost, intent, stripePayment, _t2;
297
+ return _regenerator().w(function (_context8) {
298
+ while (1) switch (_context8.p = _context8.n) {
210
299
  case 0:
211
- _context6.p = 0;
212
- _context6.n = 1;
213
- return createPaymentIntent(apiUrl, cart);
300
+ _context8.p = 0;
301
+ if (!(((_cart$totalCost = cart.totalCost) !== null && _cart$totalCost !== void 0 ? _cart$totalCost : 0) <= 0)) {
302
+ _context8.n = 2;
303
+ break;
304
+ }
305
+ _context8.n = 1;
306
+ return completeZeroBalanceCheckout(apiUrl, cart);
214
307
  case 1:
215
- intent = _context6.v;
216
- _context6.n = 2;
308
+ return _context8.a(2, _context8.v);
309
+ case 2:
310
+ _context8.n = 3;
311
+ return createPaymentIntent(apiUrl, cart);
312
+ case 3:
313
+ intent = _context8.v;
314
+ if (!intent.zeroBalance) {
315
+ _context8.n = 5;
316
+ break;
317
+ }
318
+ _context8.n = 4;
319
+ return checkPaymentIntnetIsComplete(apiUrl, cart);
320
+ case 4:
321
+ return _context8.a(2, _context8.v);
322
+ case 5:
323
+ _context8.n = 6;
217
324
  return stripe.confirmCardPayment(intent.stripeClientSecret, {
218
325
  payment_method: {
219
326
  card: card
220
327
  }
221
328
  });
222
- case 2:
223
- stripePayment = _context6.v;
329
+ case 6:
330
+ stripePayment = _context8.v;
224
331
  if (!stripePayment.error) {
225
- _context6.n = 3;
332
+ _context8.n = 7;
226
333
  break;
227
334
  }
228
335
  throw new Error(stripePayment.error.message || 'Stripe connect unknown error');
229
- case 3:
230
- _context6.n = 4;
336
+ case 7:
337
+ _context8.n = 8;
231
338
  return checkPaymentIntnetIsComplete(apiUrl, cart);
232
- case 4:
233
- return _context6.a(2, _context6.v);
234
- case 5:
235
- _context6.p = 5;
236
- _t2 = _context6.v;
339
+ case 8:
340
+ return _context8.a(2, _context8.v);
341
+ case 9:
342
+ _context8.p = 9;
343
+ _t2 = _context8.v;
237
344
  throw _t2;
238
- case 6:
239
- return _context6.a(2);
345
+ case 10:
346
+ return _context8.a(2);
240
347
  }
241
- }, _callee6, null, [[0, 5]]);
348
+ }, _callee8, null, [[0, 9]]);
242
349
  }));
243
350
  return _stripeCardFormSubmit.apply(this, arguments);
244
351
  }
245
- export function stripePaymentButtonSubmit(_x12, _x13, _x14, _x15) {
352
+ export function stripePaymentButtonSubmit(_x16, _x17, _x18, _x19) {
246
353
  return _stripePaymentButtonSubmit.apply(this, arguments);
247
354
  }
248
355
  function _stripePaymentButtonSubmit() {
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) {
356
+ _stripePaymentButtonSubmit = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9(apiUrl, stripe, cart, paymentMethodEvent) {
357
+ var complete, paymentMethod, _cart$totalCost2, intent, stripePayment, _t3;
358
+ return _regenerator().w(function (_context9) {
359
+ while (1) switch (_context9.p = _context9.n) {
253
360
  case 0:
254
361
  complete = paymentMethodEvent.complete, paymentMethod = paymentMethodEvent.paymentMethod;
255
- _context7.p = 1;
256
- _context7.n = 2;
257
- return createPaymentIntent(apiUrl, cart);
362
+ _context9.p = 1;
363
+ if (!(((_cart$totalCost2 = cart.totalCost) !== null && _cart$totalCost2 !== void 0 ? _cart$totalCost2 : 0) <= 0)) {
364
+ _context9.n = 3;
365
+ break;
366
+ }
367
+ _context9.n = 2;
368
+ return completeZeroBalanceCheckout(apiUrl, cart);
258
369
  case 2:
259
- intent = _context7.v;
260
- _context7.n = 3;
370
+ return _context9.a(2, _context9.v);
371
+ case 3:
372
+ _context9.n = 4;
373
+ return createPaymentIntent(apiUrl, cart);
374
+ case 4:
375
+ intent = _context9.v;
376
+ if (!intent.zeroBalance) {
377
+ _context9.n = 6;
378
+ break;
379
+ }
380
+ _context9.n = 5;
381
+ return checkPaymentIntnetIsComplete(apiUrl, cart);
382
+ case 5:
383
+ return _context9.a(2, _context9.v);
384
+ case 6:
385
+ _context9.n = 7;
261
386
  return stripe.confirmCardPayment(intent.stripeClientSecret, {
262
387
  payment_method: paymentMethod.id
263
388
  }, {
264
389
  handleActions: false
265
390
  });
266
- case 3:
267
- stripePayment = _context7.v;
391
+ case 7:
392
+ stripePayment = _context9.v;
268
393
  if (!stripePayment.error) {
269
- _context7.n = 4;
394
+ _context9.n = 8;
270
395
  break;
271
396
  }
272
397
  throw new Error(stripePayment.error.message || 'Stripe unknown error');
273
- case 4:
398
+ case 8:
274
399
  complete('success');
275
- _context7.n = 5;
400
+ _context9.n = 9;
276
401
  return checkPaymentIntnetIsComplete(apiUrl, cart);
277
- case 5:
278
- return _context7.a(2, _context7.v);
279
- case 6:
280
- _context7.p = 6;
281
- _t3 = _context7.v;
402
+ case 9:
403
+ return _context9.a(2, _context9.v);
404
+ case 10:
405
+ _context9.p = 10;
406
+ _t3 = _context9.v;
282
407
  complete('fail');
283
408
  throw _t3;
284
- case 7:
285
- return _context7.a(2);
409
+ case 11:
410
+ return _context9.a(2);
286
411
  }
287
- }, _callee7, null, [[1, 6]]);
412
+ }, _callee9, null, [[1, 10]]);
288
413
  }));
289
414
  return _stripePaymentButtonSubmit.apply(this, arguments);
290
415
  }
@@ -137,7 +137,7 @@ function createPaymentIntent(_x5, _x6, _x7) {
137
137
  }
138
138
  function _createPaymentIntent() {
139
139
  _createPaymentIntent = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(apiUrl, cart, paymentMethodType) {
140
- var id, token, fetchOptions, queryParams, queryString, uri, url, response;
140
+ var id, token, fetchOptions, queryParams, queryString, uri, url, response, data;
141
141
  return _regenerator().w(function (_context4) {
142
142
  while (1) switch (_context4.n) {
143
143
  case 0:
@@ -157,29 +157,118 @@ function _createPaymentIntent() {
157
157
  return fetch(url, fetchOptions);
158
158
  case 1:
159
159
  response = _context4.v;
160
+ _context4.n = 2;
161
+ return response.json();
162
+ case 2:
163
+ data = _context4.v;
160
164
  if (response.ok) {
161
- _context4.n = 2;
165
+ _context4.n = 3;
162
166
  break;
163
167
  }
164
- throw new Error("Failed to create payment intent.");
165
- case 2:
166
- _context4.n = 3;
167
- return response.json();
168
+ throw new Error(data.message || "Failed to create payment intent.");
168
169
  case 3:
169
- return _context4.a(2, _context4.v);
170
+ return _context4.a(2, data);
170
171
  }
171
172
  }, _callee4);
172
173
  }));
173
174
  return _createPaymentIntent.apply(this, arguments);
174
175
  }
175
- function checkPaymentIntnetIsComplete(_x8, _x9) {
176
+ function completeZeroBalanceCheckoutLegacy(_x8, _x9) {
177
+ return _completeZeroBalanceCheckoutLegacy.apply(this, arguments);
178
+ }
179
+ function _completeZeroBalanceCheckoutLegacy() {
180
+ _completeZeroBalanceCheckoutLegacy = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(apiUrl, cart) {
181
+ var intent;
182
+ return _regenerator().w(function (_context5) {
183
+ while (1) switch (_context5.n) {
184
+ case 0:
185
+ _context5.n = 1;
186
+ return createPaymentIntent(apiUrl, cart);
187
+ case 1:
188
+ intent = _context5.v;
189
+ if (intent.zeroBalance) {
190
+ _context5.n = 2;
191
+ break;
192
+ }
193
+ throw new Error('Unable to complete this order.');
194
+ case 2:
195
+ _context5.n = 3;
196
+ return checkPaymentIntnetIsComplete(apiUrl, cart);
197
+ case 3:
198
+ return _context5.a(2, _context5.v);
199
+ }
200
+ }, _callee5);
201
+ }));
202
+ return _completeZeroBalanceCheckoutLegacy.apply(this, arguments);
203
+ }
204
+ function completeZeroBalanceCheckout(_x0, _x1) {
205
+ return _completeZeroBalanceCheckout.apply(this, arguments);
206
+ }
207
+ function _completeZeroBalanceCheckout() {
208
+ _completeZeroBalanceCheckout = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(apiUrl, cart) {
209
+ var url, query, response, data, message;
210
+ return _regenerator().w(function (_context6) {
211
+ while (1) switch (_context6.n) {
212
+ case 0:
213
+ url = "".concat(apiUrl.replace(/\/$/, ''), "/payments/stripe/cart/").concat(cart.id, "/attempts/");
214
+ query = new URLSearchParams({
215
+ cart_token: cart.token
216
+ }).toString();
217
+ _context6.n = 1;
218
+ return fetch("".concat(url, "?").concat(query), {
219
+ method: 'POST',
220
+ mode: 'cors',
221
+ headers: {
222
+ 'Content-Type': 'application/json'
223
+ },
224
+ body: JSON.stringify({
225
+ requestKey: "zero-balance:".concat(cart.id, ":").concat(Date.now())
226
+ })
227
+ });
228
+ case 1:
229
+ response = _context6.v;
230
+ _context6.n = 2;
231
+ return response.json();
232
+ case 2:
233
+ data = _context6.v;
234
+ if (!(response.ok && data.invoice)) {
235
+ _context6.n = 3;
236
+ break;
237
+ }
238
+ return _context6.a(2, data.invoice);
239
+ case 3:
240
+ message = data.message || '';
241
+ if (!(response.status === 409 && message.toLowerCase().includes('new payment flow is disabled'))) {
242
+ _context6.n = 5;
243
+ break;
244
+ }
245
+ _context6.n = 4;
246
+ return completeZeroBalanceCheckoutLegacy(apiUrl, cart);
247
+ case 4:
248
+ return _context6.a(2, _context6.v);
249
+ case 5:
250
+ if (response.ok) {
251
+ _context6.n = 6;
252
+ break;
253
+ }
254
+ throw new Error(message || 'Unable to complete this order.');
255
+ case 6:
256
+ throw new Error('Unable to complete this order.');
257
+ case 7:
258
+ return _context6.a(2);
259
+ }
260
+ }, _callee6);
261
+ }));
262
+ return _completeZeroBalanceCheckout.apply(this, arguments);
263
+ }
264
+ function checkPaymentIntnetIsComplete(_x10, _x11) {
176
265
  return _checkPaymentIntnetIsComplete.apply(this, arguments);
177
266
  }
178
267
  function _checkPaymentIntnetIsComplete() {
179
- _checkPaymentIntnetIsComplete = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(apiUrl, cart) {
268
+ _checkPaymentIntnetIsComplete = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(apiUrl, cart) {
180
269
  var url, fetchOptions, queryString, response, i;
181
- return _regenerator().w(function (_context5) {
182
- while (1) switch (_context5.n) {
270
+ return _regenerator().w(function (_context7) {
271
+ while (1) switch (_context7.n) {
183
272
  case 0:
184
273
  url = "stripe/payment_intent/cart/complete/".concat(cart.id, "/");
185
274
  fetchOptions = {
@@ -189,112 +278,148 @@ function _checkPaymentIntnetIsComplete() {
189
278
  queryString = new URLSearchParams({
190
279
  cart_token: cart.token
191
280
  }).toString();
192
- _context5.n = 1;
281
+ _context7.n = 1;
193
282
  return fetch("".concat(apiUrl).concat(url, "?").concat(queryString), fetchOptions);
194
283
  case 1:
195
- response = _context5.v;
284
+ response = _context7.v;
196
285
  if (response.ok) {
197
- _context5.n = 2;
286
+ _context7.n = 2;
198
287
  break;
199
288
  }
200
289
  throw new Error("Failed to check if payment intent is complete.");
201
290
  case 2:
202
- _context5.n = 3;
291
+ _context7.n = 3;
203
292
  return response.json();
204
293
  case 3:
205
- i = _context5.v;
206
- return _context5.a(2, i.invoice);
294
+ i = _context7.v;
295
+ return _context7.a(2, i.invoice);
207
296
  }
208
- }, _callee5);
297
+ }, _callee7);
209
298
  }));
210
299
  return _checkPaymentIntnetIsComplete.apply(this, arguments);
211
300
  }
212
- function stripeCardFormSubmit(_x0, _x1, _x10, _x11) {
301
+ function stripeCardFormSubmit(_x12, _x13, _x14, _x15) {
213
302
  return _stripeCardFormSubmit.apply(this, arguments);
214
303
  }
215
304
  function _stripeCardFormSubmit() {
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) {
305
+ _stripeCardFormSubmit = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(apiUrl, stripe, card, cart) {
306
+ var _cart$totalCost, intent, stripePayment, _t2;
307
+ return _regenerator().w(function (_context8) {
308
+ while (1) switch (_context8.p = _context8.n) {
220
309
  case 0:
221
- _context6.p = 0;
222
- _context6.n = 1;
223
- return createPaymentIntent(apiUrl, cart);
310
+ _context8.p = 0;
311
+ if (!(((_cart$totalCost = cart.totalCost) !== null && _cart$totalCost !== void 0 ? _cart$totalCost : 0) <= 0)) {
312
+ _context8.n = 2;
313
+ break;
314
+ }
315
+ _context8.n = 1;
316
+ return completeZeroBalanceCheckout(apiUrl, cart);
224
317
  case 1:
225
- intent = _context6.v;
226
- _context6.n = 2;
318
+ return _context8.a(2, _context8.v);
319
+ case 2:
320
+ _context8.n = 3;
321
+ return createPaymentIntent(apiUrl, cart);
322
+ case 3:
323
+ intent = _context8.v;
324
+ if (!intent.zeroBalance) {
325
+ _context8.n = 5;
326
+ break;
327
+ }
328
+ _context8.n = 4;
329
+ return checkPaymentIntnetIsComplete(apiUrl, cart);
330
+ case 4:
331
+ return _context8.a(2, _context8.v);
332
+ case 5:
333
+ _context8.n = 6;
227
334
  return stripe.confirmCardPayment(intent.stripeClientSecret, {
228
335
  payment_method: {
229
336
  card: card
230
337
  }
231
338
  });
232
- case 2:
233
- stripePayment = _context6.v;
339
+ case 6:
340
+ stripePayment = _context8.v;
234
341
  if (!stripePayment.error) {
235
- _context6.n = 3;
342
+ _context8.n = 7;
236
343
  break;
237
344
  }
238
345
  throw new Error(stripePayment.error.message || 'Stripe connect unknown error');
239
- case 3:
240
- _context6.n = 4;
346
+ case 7:
347
+ _context8.n = 8;
241
348
  return checkPaymentIntnetIsComplete(apiUrl, cart);
242
- case 4:
243
- return _context6.a(2, _context6.v);
244
- case 5:
245
- _context6.p = 5;
246
- _t2 = _context6.v;
349
+ case 8:
350
+ return _context8.a(2, _context8.v);
351
+ case 9:
352
+ _context8.p = 9;
353
+ _t2 = _context8.v;
247
354
  throw _t2;
248
- case 6:
249
- return _context6.a(2);
355
+ case 10:
356
+ return _context8.a(2);
250
357
  }
251
- }, _callee6, null, [[0, 5]]);
358
+ }, _callee8, null, [[0, 9]]);
252
359
  }));
253
360
  return _stripeCardFormSubmit.apply(this, arguments);
254
361
  }
255
- function stripePaymentButtonSubmit(_x12, _x13, _x14, _x15) {
362
+ function stripePaymentButtonSubmit(_x16, _x17, _x18, _x19) {
256
363
  return _stripePaymentButtonSubmit.apply(this, arguments);
257
364
  }
258
365
  function _stripePaymentButtonSubmit() {
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) {
366
+ _stripePaymentButtonSubmit = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9(apiUrl, stripe, cart, paymentMethodEvent) {
367
+ var complete, paymentMethod, _cart$totalCost2, intent, stripePayment, _t3;
368
+ return _regenerator().w(function (_context9) {
369
+ while (1) switch (_context9.p = _context9.n) {
263
370
  case 0:
264
371
  complete = paymentMethodEvent.complete, paymentMethod = paymentMethodEvent.paymentMethod;
265
- _context7.p = 1;
266
- _context7.n = 2;
267
- return createPaymentIntent(apiUrl, cart);
372
+ _context9.p = 1;
373
+ if (!(((_cart$totalCost2 = cart.totalCost) !== null && _cart$totalCost2 !== void 0 ? _cart$totalCost2 : 0) <= 0)) {
374
+ _context9.n = 3;
375
+ break;
376
+ }
377
+ _context9.n = 2;
378
+ return completeZeroBalanceCheckout(apiUrl, cart);
268
379
  case 2:
269
- intent = _context7.v;
270
- _context7.n = 3;
380
+ return _context9.a(2, _context9.v);
381
+ case 3:
382
+ _context9.n = 4;
383
+ return createPaymentIntent(apiUrl, cart);
384
+ case 4:
385
+ intent = _context9.v;
386
+ if (!intent.zeroBalance) {
387
+ _context9.n = 6;
388
+ break;
389
+ }
390
+ _context9.n = 5;
391
+ return checkPaymentIntnetIsComplete(apiUrl, cart);
392
+ case 5:
393
+ return _context9.a(2, _context9.v);
394
+ case 6:
395
+ _context9.n = 7;
271
396
  return stripe.confirmCardPayment(intent.stripeClientSecret, {
272
397
  payment_method: paymentMethod.id
273
398
  }, {
274
399
  handleActions: false
275
400
  });
276
- case 3:
277
- stripePayment = _context7.v;
401
+ case 7:
402
+ stripePayment = _context9.v;
278
403
  if (!stripePayment.error) {
279
- _context7.n = 4;
404
+ _context9.n = 8;
280
405
  break;
281
406
  }
282
407
  throw new Error(stripePayment.error.message || 'Stripe unknown error');
283
- case 4:
408
+ case 8:
284
409
  complete('success');
285
- _context7.n = 5;
410
+ _context9.n = 9;
286
411
  return checkPaymentIntnetIsComplete(apiUrl, cart);
287
- case 5:
288
- return _context7.a(2, _context7.v);
289
- case 6:
290
- _context7.p = 6;
291
- _t3 = _context7.v;
412
+ case 9:
413
+ return _context9.a(2, _context9.v);
414
+ case 10:
415
+ _context9.p = 10;
416
+ _t3 = _context9.v;
292
417
  complete('fail');
293
418
  throw _t3;
294
- case 7:
295
- return _context7.a(2);
419
+ case 11:
420
+ return _context9.a(2);
296
421
  }
297
- }, _callee7, null, [[1, 6]]);
422
+ }, _callee9, null, [[1, 10]]);
298
423
  }));
299
424
  return _stripePaymentButtonSubmit.apply(this, arguments);
300
425
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@merchi/cart",
3
- "version": "1.4.69",
3
+ "version": "1.4.71",
4
4
  "description": "Merchi's cart",
5
5
  "source": "src/index.ts",
6
6
  "main": "lib/index.js",
@@ -94,7 +94,7 @@
94
94
  "@fortawesome/free-solid-svg-icons": "6.4.2",
95
95
  "@fortawesome/react-fontawesome": "^0.2.0",
96
96
  "@merchi/error-catch": "^0.1.11",
97
- "@merchi/invoice": "^1.3.16",
97
+ "@merchi/invoice": "^1.3.17",
98
98
  "@merchi/product-form": "^1.19.58",
99
99
  "@merchi/product-form-sdk": "^1.0.38",
100
100
  "@merchi/sdk": "^1.42.27",
@@ -65,12 +65,49 @@ async function createPaymentIntent(apiUrl: string, cart: any, paymentMethodType?
65
65
  const url = `${apiUrl}${uri}?${queryString}`;
66
66
 
67
67
  const response = await fetch(url, fetchOptions);
68
+ const data = await response.json();
68
69
 
69
70
  if (!response.ok) {
70
- throw new Error("Failed to create payment intent.");
71
+ throw new Error(data.message || "Failed to create payment intent.");
71
72
  }
72
73
 
73
- return await response.json();
74
+ return data;
75
+ }
76
+
77
+ async function completeZeroBalanceCheckoutLegacy(apiUrl: string, cart: any) {
78
+ const intent = await createPaymentIntent(apiUrl, cart);
79
+ if (!intent.zeroBalance) {
80
+ throw new Error('Unable to complete this order.');
81
+ }
82
+ return await checkPaymentIntnetIsComplete(apiUrl, cart);
83
+ }
84
+
85
+ async function completeZeroBalanceCheckout(apiUrl: string, cart: any) {
86
+ const url = `${apiUrl.replace(/\/$/, '')}/payments/stripe/cart/${cart.id}/attempts/`;
87
+ const query = new URLSearchParams({ cart_token: cart.token }).toString();
88
+ const response = await fetch(`${url}?${query}`, {
89
+ method: 'POST',
90
+ mode: 'cors',
91
+ headers: { 'Content-Type': 'application/json' },
92
+ body: JSON.stringify({
93
+ requestKey: `zero-balance:${cart.id}:${Date.now()}`,
94
+ }),
95
+ });
96
+ const data = await response.json();
97
+ if (response.ok && data.invoice) {
98
+ return data.invoice;
99
+ }
100
+ const message = data.message || '';
101
+ if (
102
+ response.status === 409
103
+ && message.toLowerCase().includes('new payment flow is disabled')
104
+ ) {
105
+ return await completeZeroBalanceCheckoutLegacy(apiUrl, cart);
106
+ }
107
+ if (!response.ok) {
108
+ throw new Error(message || 'Unable to complete this order.');
109
+ }
110
+ throw new Error('Unable to complete this order.');
74
111
  }
75
112
 
76
113
  async function checkPaymentIntnetIsComplete(apiUrl: string, cart: any) {
@@ -92,7 +129,13 @@ async function checkPaymentIntnetIsComplete(apiUrl: string, cart: any) {
92
129
 
93
130
  export async function stripeCardFormSubmit(apiUrl: string, stripe: any, card: any, cart: any) {
94
131
  try {
132
+ if ((cart.totalCost ?? 0) <= 0) {
133
+ return await completeZeroBalanceCheckout(apiUrl, cart);
134
+ }
95
135
  const intent = await createPaymentIntent(apiUrl, cart);
136
+ if (intent.zeroBalance) {
137
+ return await checkPaymentIntnetIsComplete(apiUrl, cart);
138
+ }
96
139
  const stripePayment = await stripe.confirmCardPayment(intent.stripeClientSecret, { payment_method: { card } });
97
140
 
98
141
  if (stripePayment.error) {
@@ -114,7 +157,13 @@ export async function stripePaymentButtonSubmit(
114
157
  const { complete, paymentMethod } = paymentMethodEvent;
115
158
 
116
159
  try {
160
+ if ((cart.totalCost ?? 0) <= 0) {
161
+ return await completeZeroBalanceCheckout(apiUrl, cart);
162
+ }
117
163
  const intent = await createPaymentIntent(apiUrl, cart);
164
+ if (intent.zeroBalance) {
165
+ return await checkPaymentIntnetIsComplete(apiUrl, cart);
166
+ }
118
167
  const stripePayment = await stripe.confirmCardPayment(
119
168
  intent.stripeClientSecret,
120
169
  { payment_method: paymentMethod.id },