@merchi/cart 1.4.69 → 1.4.70

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,78 @@ 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 completeZeroBalanceCheckout(_x8, _x9) {
167
+ return _completeZeroBalanceCheckout.apply(this, arguments);
168
+ }
169
+ function _completeZeroBalanceCheckout() {
170
+ _completeZeroBalanceCheckout = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(apiUrl, cart) {
171
+ var url, query, response, data;
172
+ return _regenerator().w(function (_context5) {
173
+ while (1) switch (_context5.n) {
174
+ case 0:
175
+ url = "".concat(apiUrl.replace(/\/$/, ''), "/payments/stripe/cart/").concat(cart.id, "/attempts/");
176
+ query = new URLSearchParams({
177
+ cart_token: cart.token
178
+ }).toString();
179
+ _context5.n = 1;
180
+ return fetch("".concat(url, "?").concat(query), {
181
+ method: 'POST',
182
+ mode: 'cors',
183
+ headers: {
184
+ 'Content-Type': 'application/json'
185
+ },
186
+ body: JSON.stringify({
187
+ requestKey: "zero-balance:".concat(cart.id, ":").concat(Date.now())
188
+ })
189
+ });
190
+ case 1:
191
+ response = _context5.v;
192
+ _context5.n = 2;
193
+ return response.json();
194
+ case 2:
195
+ data = _context5.v;
196
+ if (response.ok) {
197
+ _context5.n = 3;
198
+ break;
199
+ }
200
+ throw new Error(data.message || 'Unable to complete this order.');
201
+ case 3:
202
+ if (data.invoice) {
203
+ _context5.n = 4;
204
+ break;
205
+ }
206
+ throw new Error('Unable to complete this order.');
207
+ case 4:
208
+ return _context5.a(2, data.invoice);
209
+ }
210
+ }, _callee5);
211
+ }));
212
+ return _completeZeroBalanceCheckout.apply(this, arguments);
213
+ }
214
+ function checkPaymentIntnetIsComplete(_x0, _x1) {
166
215
  return _checkPaymentIntnetIsComplete.apply(this, arguments);
167
216
  }
168
217
  function _checkPaymentIntnetIsComplete() {
169
- _checkPaymentIntnetIsComplete = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(apiUrl, cart) {
218
+ _checkPaymentIntnetIsComplete = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(apiUrl, cart) {
170
219
  var url, fetchOptions, queryString, response, i;
171
- return _regenerator().w(function (_context5) {
172
- while (1) switch (_context5.n) {
220
+ return _regenerator().w(function (_context6) {
221
+ while (1) switch (_context6.n) {
173
222
  case 0:
174
223
  url = "stripe/payment_intent/cart/complete/".concat(cart.id, "/");
175
224
  fetchOptions = {
@@ -179,112 +228,148 @@ function _checkPaymentIntnetIsComplete() {
179
228
  queryString = new URLSearchParams({
180
229
  cart_token: cart.token
181
230
  }).toString();
182
- _context5.n = 1;
231
+ _context6.n = 1;
183
232
  return fetch("".concat(apiUrl).concat(url, "?").concat(queryString), fetchOptions);
184
233
  case 1:
185
- response = _context5.v;
234
+ response = _context6.v;
186
235
  if (response.ok) {
187
- _context5.n = 2;
236
+ _context6.n = 2;
188
237
  break;
189
238
  }
190
239
  throw new Error("Failed to check if payment intent is complete.");
191
240
  case 2:
192
- _context5.n = 3;
241
+ _context6.n = 3;
193
242
  return response.json();
194
243
  case 3:
195
- i = _context5.v;
196
- return _context5.a(2, i.invoice);
244
+ i = _context6.v;
245
+ return _context6.a(2, i.invoice);
197
246
  }
198
- }, _callee5);
247
+ }, _callee6);
199
248
  }));
200
249
  return _checkPaymentIntnetIsComplete.apply(this, arguments);
201
250
  }
202
- export function stripeCardFormSubmit(_x0, _x1, _x10, _x11) {
251
+ export function stripeCardFormSubmit(_x10, _x11, _x12, _x13) {
203
252
  return _stripeCardFormSubmit.apply(this, arguments);
204
253
  }
205
254
  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) {
255
+ _stripeCardFormSubmit = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(apiUrl, stripe, card, cart) {
256
+ var _cart$totalCost, intent, stripePayment, _t2;
257
+ return _regenerator().w(function (_context7) {
258
+ while (1) switch (_context7.p = _context7.n) {
210
259
  case 0:
211
- _context6.p = 0;
212
- _context6.n = 1;
213
- return createPaymentIntent(apiUrl, cart);
260
+ _context7.p = 0;
261
+ if (!(((_cart$totalCost = cart.totalCost) !== null && _cart$totalCost !== void 0 ? _cart$totalCost : 0) <= 0)) {
262
+ _context7.n = 2;
263
+ break;
264
+ }
265
+ _context7.n = 1;
266
+ return completeZeroBalanceCheckout(apiUrl, cart);
214
267
  case 1:
215
- intent = _context6.v;
216
- _context6.n = 2;
268
+ return _context7.a(2, _context7.v);
269
+ case 2:
270
+ _context7.n = 3;
271
+ return createPaymentIntent(apiUrl, cart);
272
+ case 3:
273
+ intent = _context7.v;
274
+ if (!intent.zeroBalance) {
275
+ _context7.n = 5;
276
+ break;
277
+ }
278
+ _context7.n = 4;
279
+ return checkPaymentIntnetIsComplete(apiUrl, cart);
280
+ case 4:
281
+ return _context7.a(2, _context7.v);
282
+ case 5:
283
+ _context7.n = 6;
217
284
  return stripe.confirmCardPayment(intent.stripeClientSecret, {
218
285
  payment_method: {
219
286
  card: card
220
287
  }
221
288
  });
222
- case 2:
223
- stripePayment = _context6.v;
289
+ case 6:
290
+ stripePayment = _context7.v;
224
291
  if (!stripePayment.error) {
225
- _context6.n = 3;
292
+ _context7.n = 7;
226
293
  break;
227
294
  }
228
295
  throw new Error(stripePayment.error.message || 'Stripe connect unknown error');
229
- case 3:
230
- _context6.n = 4;
296
+ case 7:
297
+ _context7.n = 8;
231
298
  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;
299
+ case 8:
300
+ return _context7.a(2, _context7.v);
301
+ case 9:
302
+ _context7.p = 9;
303
+ _t2 = _context7.v;
237
304
  throw _t2;
238
- case 6:
239
- return _context6.a(2);
305
+ case 10:
306
+ return _context7.a(2);
240
307
  }
241
- }, _callee6, null, [[0, 5]]);
308
+ }, _callee7, null, [[0, 9]]);
242
309
  }));
243
310
  return _stripeCardFormSubmit.apply(this, arguments);
244
311
  }
245
- export function stripePaymentButtonSubmit(_x12, _x13, _x14, _x15) {
312
+ export function stripePaymentButtonSubmit(_x14, _x15, _x16, _x17) {
246
313
  return _stripePaymentButtonSubmit.apply(this, arguments);
247
314
  }
248
315
  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) {
316
+ _stripePaymentButtonSubmit = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(apiUrl, stripe, cart, paymentMethodEvent) {
317
+ var complete, paymentMethod, _cart$totalCost2, intent, stripePayment, _t3;
318
+ return _regenerator().w(function (_context8) {
319
+ while (1) switch (_context8.p = _context8.n) {
253
320
  case 0:
254
321
  complete = paymentMethodEvent.complete, paymentMethod = paymentMethodEvent.paymentMethod;
255
- _context7.p = 1;
256
- _context7.n = 2;
257
- return createPaymentIntent(apiUrl, cart);
322
+ _context8.p = 1;
323
+ if (!(((_cart$totalCost2 = cart.totalCost) !== null && _cart$totalCost2 !== void 0 ? _cart$totalCost2 : 0) <= 0)) {
324
+ _context8.n = 3;
325
+ break;
326
+ }
327
+ _context8.n = 2;
328
+ return completeZeroBalanceCheckout(apiUrl, cart);
258
329
  case 2:
259
- intent = _context7.v;
260
- _context7.n = 3;
330
+ return _context8.a(2, _context8.v);
331
+ case 3:
332
+ _context8.n = 4;
333
+ return createPaymentIntent(apiUrl, cart);
334
+ case 4:
335
+ intent = _context8.v;
336
+ if (!intent.zeroBalance) {
337
+ _context8.n = 6;
338
+ break;
339
+ }
340
+ _context8.n = 5;
341
+ return checkPaymentIntnetIsComplete(apiUrl, cart);
342
+ case 5:
343
+ return _context8.a(2, _context8.v);
344
+ case 6:
345
+ _context8.n = 7;
261
346
  return stripe.confirmCardPayment(intent.stripeClientSecret, {
262
347
  payment_method: paymentMethod.id
263
348
  }, {
264
349
  handleActions: false
265
350
  });
266
- case 3:
267
- stripePayment = _context7.v;
351
+ case 7:
352
+ stripePayment = _context8.v;
268
353
  if (!stripePayment.error) {
269
- _context7.n = 4;
354
+ _context8.n = 8;
270
355
  break;
271
356
  }
272
357
  throw new Error(stripePayment.error.message || 'Stripe unknown error');
273
- case 4:
358
+ case 8:
274
359
  complete('success');
275
- _context7.n = 5;
360
+ _context8.n = 9;
276
361
  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;
362
+ case 9:
363
+ return _context8.a(2, _context8.v);
364
+ case 10:
365
+ _context8.p = 10;
366
+ _t3 = _context8.v;
282
367
  complete('fail');
283
368
  throw _t3;
284
- case 7:
285
- return _context7.a(2);
369
+ case 11:
370
+ return _context8.a(2);
286
371
  }
287
- }, _callee7, null, [[1, 6]]);
372
+ }, _callee8, null, [[1, 10]]);
288
373
  }));
289
374
  return _stripePaymentButtonSubmit.apply(this, arguments);
290
375
  }
@@ -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,78 @@ 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 completeZeroBalanceCheckout(_x8, _x9) {
177
+ return _completeZeroBalanceCheckout.apply(this, arguments);
178
+ }
179
+ function _completeZeroBalanceCheckout() {
180
+ _completeZeroBalanceCheckout = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(apiUrl, cart) {
181
+ var url, query, response, data;
182
+ return _regenerator().w(function (_context5) {
183
+ while (1) switch (_context5.n) {
184
+ case 0:
185
+ url = "".concat(apiUrl.replace(/\/$/, ''), "/payments/stripe/cart/").concat(cart.id, "/attempts/");
186
+ query = new URLSearchParams({
187
+ cart_token: cart.token
188
+ }).toString();
189
+ _context5.n = 1;
190
+ return fetch("".concat(url, "?").concat(query), {
191
+ method: 'POST',
192
+ mode: 'cors',
193
+ headers: {
194
+ 'Content-Type': 'application/json'
195
+ },
196
+ body: JSON.stringify({
197
+ requestKey: "zero-balance:".concat(cart.id, ":").concat(Date.now())
198
+ })
199
+ });
200
+ case 1:
201
+ response = _context5.v;
202
+ _context5.n = 2;
203
+ return response.json();
204
+ case 2:
205
+ data = _context5.v;
206
+ if (response.ok) {
207
+ _context5.n = 3;
208
+ break;
209
+ }
210
+ throw new Error(data.message || 'Unable to complete this order.');
211
+ case 3:
212
+ if (data.invoice) {
213
+ _context5.n = 4;
214
+ break;
215
+ }
216
+ throw new Error('Unable to complete this order.');
217
+ case 4:
218
+ return _context5.a(2, data.invoice);
219
+ }
220
+ }, _callee5);
221
+ }));
222
+ return _completeZeroBalanceCheckout.apply(this, arguments);
223
+ }
224
+ function checkPaymentIntnetIsComplete(_x0, _x1) {
176
225
  return _checkPaymentIntnetIsComplete.apply(this, arguments);
177
226
  }
178
227
  function _checkPaymentIntnetIsComplete() {
179
- _checkPaymentIntnetIsComplete = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(apiUrl, cart) {
228
+ _checkPaymentIntnetIsComplete = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(apiUrl, cart) {
180
229
  var url, fetchOptions, queryString, response, i;
181
- return _regenerator().w(function (_context5) {
182
- while (1) switch (_context5.n) {
230
+ return _regenerator().w(function (_context6) {
231
+ while (1) switch (_context6.n) {
183
232
  case 0:
184
233
  url = "stripe/payment_intent/cart/complete/".concat(cart.id, "/");
185
234
  fetchOptions = {
@@ -189,112 +238,148 @@ function _checkPaymentIntnetIsComplete() {
189
238
  queryString = new URLSearchParams({
190
239
  cart_token: cart.token
191
240
  }).toString();
192
- _context5.n = 1;
241
+ _context6.n = 1;
193
242
  return fetch("".concat(apiUrl).concat(url, "?").concat(queryString), fetchOptions);
194
243
  case 1:
195
- response = _context5.v;
244
+ response = _context6.v;
196
245
  if (response.ok) {
197
- _context5.n = 2;
246
+ _context6.n = 2;
198
247
  break;
199
248
  }
200
249
  throw new Error("Failed to check if payment intent is complete.");
201
250
  case 2:
202
- _context5.n = 3;
251
+ _context6.n = 3;
203
252
  return response.json();
204
253
  case 3:
205
- i = _context5.v;
206
- return _context5.a(2, i.invoice);
254
+ i = _context6.v;
255
+ return _context6.a(2, i.invoice);
207
256
  }
208
- }, _callee5);
257
+ }, _callee6);
209
258
  }));
210
259
  return _checkPaymentIntnetIsComplete.apply(this, arguments);
211
260
  }
212
- function stripeCardFormSubmit(_x0, _x1, _x10, _x11) {
261
+ function stripeCardFormSubmit(_x10, _x11, _x12, _x13) {
213
262
  return _stripeCardFormSubmit.apply(this, arguments);
214
263
  }
215
264
  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) {
265
+ _stripeCardFormSubmit = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(apiUrl, stripe, card, cart) {
266
+ var _cart$totalCost, intent, stripePayment, _t2;
267
+ return _regenerator().w(function (_context7) {
268
+ while (1) switch (_context7.p = _context7.n) {
220
269
  case 0:
221
- _context6.p = 0;
222
- _context6.n = 1;
223
- return createPaymentIntent(apiUrl, cart);
270
+ _context7.p = 0;
271
+ if (!(((_cart$totalCost = cart.totalCost) !== null && _cart$totalCost !== void 0 ? _cart$totalCost : 0) <= 0)) {
272
+ _context7.n = 2;
273
+ break;
274
+ }
275
+ _context7.n = 1;
276
+ return completeZeroBalanceCheckout(apiUrl, cart);
224
277
  case 1:
225
- intent = _context6.v;
226
- _context6.n = 2;
278
+ return _context7.a(2, _context7.v);
279
+ case 2:
280
+ _context7.n = 3;
281
+ return createPaymentIntent(apiUrl, cart);
282
+ case 3:
283
+ intent = _context7.v;
284
+ if (!intent.zeroBalance) {
285
+ _context7.n = 5;
286
+ break;
287
+ }
288
+ _context7.n = 4;
289
+ return checkPaymentIntnetIsComplete(apiUrl, cart);
290
+ case 4:
291
+ return _context7.a(2, _context7.v);
292
+ case 5:
293
+ _context7.n = 6;
227
294
  return stripe.confirmCardPayment(intent.stripeClientSecret, {
228
295
  payment_method: {
229
296
  card: card
230
297
  }
231
298
  });
232
- case 2:
233
- stripePayment = _context6.v;
299
+ case 6:
300
+ stripePayment = _context7.v;
234
301
  if (!stripePayment.error) {
235
- _context6.n = 3;
302
+ _context7.n = 7;
236
303
  break;
237
304
  }
238
305
  throw new Error(stripePayment.error.message || 'Stripe connect unknown error');
239
- case 3:
240
- _context6.n = 4;
306
+ case 7:
307
+ _context7.n = 8;
241
308
  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;
309
+ case 8:
310
+ return _context7.a(2, _context7.v);
311
+ case 9:
312
+ _context7.p = 9;
313
+ _t2 = _context7.v;
247
314
  throw _t2;
248
- case 6:
249
- return _context6.a(2);
315
+ case 10:
316
+ return _context7.a(2);
250
317
  }
251
- }, _callee6, null, [[0, 5]]);
318
+ }, _callee7, null, [[0, 9]]);
252
319
  }));
253
320
  return _stripeCardFormSubmit.apply(this, arguments);
254
321
  }
255
- function stripePaymentButtonSubmit(_x12, _x13, _x14, _x15) {
322
+ function stripePaymentButtonSubmit(_x14, _x15, _x16, _x17) {
256
323
  return _stripePaymentButtonSubmit.apply(this, arguments);
257
324
  }
258
325
  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) {
326
+ _stripePaymentButtonSubmit = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(apiUrl, stripe, cart, paymentMethodEvent) {
327
+ var complete, paymentMethod, _cart$totalCost2, intent, stripePayment, _t3;
328
+ return _regenerator().w(function (_context8) {
329
+ while (1) switch (_context8.p = _context8.n) {
263
330
  case 0:
264
331
  complete = paymentMethodEvent.complete, paymentMethod = paymentMethodEvent.paymentMethod;
265
- _context7.p = 1;
266
- _context7.n = 2;
267
- return createPaymentIntent(apiUrl, cart);
332
+ _context8.p = 1;
333
+ if (!(((_cart$totalCost2 = cart.totalCost) !== null && _cart$totalCost2 !== void 0 ? _cart$totalCost2 : 0) <= 0)) {
334
+ _context8.n = 3;
335
+ break;
336
+ }
337
+ _context8.n = 2;
338
+ return completeZeroBalanceCheckout(apiUrl, cart);
268
339
  case 2:
269
- intent = _context7.v;
270
- _context7.n = 3;
340
+ return _context8.a(2, _context8.v);
341
+ case 3:
342
+ _context8.n = 4;
343
+ return createPaymentIntent(apiUrl, cart);
344
+ case 4:
345
+ intent = _context8.v;
346
+ if (!intent.zeroBalance) {
347
+ _context8.n = 6;
348
+ break;
349
+ }
350
+ _context8.n = 5;
351
+ return checkPaymentIntnetIsComplete(apiUrl, cart);
352
+ case 5:
353
+ return _context8.a(2, _context8.v);
354
+ case 6:
355
+ _context8.n = 7;
271
356
  return stripe.confirmCardPayment(intent.stripeClientSecret, {
272
357
  payment_method: paymentMethod.id
273
358
  }, {
274
359
  handleActions: false
275
360
  });
276
- case 3:
277
- stripePayment = _context7.v;
361
+ case 7:
362
+ stripePayment = _context8.v;
278
363
  if (!stripePayment.error) {
279
- _context7.n = 4;
364
+ _context8.n = 8;
280
365
  break;
281
366
  }
282
367
  throw new Error(stripePayment.error.message || 'Stripe unknown error');
283
- case 4:
368
+ case 8:
284
369
  complete('success');
285
- _context7.n = 5;
370
+ _context8.n = 9;
286
371
  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;
372
+ case 9:
373
+ return _context8.a(2, _context8.v);
374
+ case 10:
375
+ _context8.p = 10;
376
+ _t3 = _context8.v;
292
377
  complete('fail');
293
378
  throw _t3;
294
- case 7:
295
- return _context7.a(2);
379
+ case 11:
380
+ return _context8.a(2);
296
381
  }
297
- }, _callee7, null, [[1, 6]]);
382
+ }, _callee8, null, [[1, 10]]);
298
383
  }));
299
384
  return _stripePaymentButtonSubmit.apply(this, arguments);
300
385
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@merchi/cart",
3
- "version": "1.4.69",
3
+ "version": "1.4.70",
4
4
  "description": "Merchi's cart",
5
5
  "source": "src/index.ts",
6
6
  "main": "lib/index.js",
@@ -65,12 +65,34 @@ 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 completeZeroBalanceCheckout(apiUrl: string, cart: any) {
78
+ const url = `${apiUrl.replace(/\/$/, '')}/payments/stripe/cart/${cart.id}/attempts/`;
79
+ const query = new URLSearchParams({ cart_token: cart.token }).toString();
80
+ const response = await fetch(`${url}?${query}`, {
81
+ method: 'POST',
82
+ mode: 'cors',
83
+ headers: { 'Content-Type': 'application/json' },
84
+ body: JSON.stringify({
85
+ requestKey: `zero-balance:${cart.id}:${Date.now()}`,
86
+ }),
87
+ });
88
+ const data = await response.json();
89
+ if (!response.ok) {
90
+ throw new Error(data.message || 'Unable to complete this order.');
91
+ }
92
+ if (!data.invoice) {
93
+ throw new Error('Unable to complete this order.');
94
+ }
95
+ return data.invoice;
74
96
  }
75
97
 
76
98
  async function checkPaymentIntnetIsComplete(apiUrl: string, cart: any) {
@@ -92,7 +114,13 @@ async function checkPaymentIntnetIsComplete(apiUrl: string, cart: any) {
92
114
 
93
115
  export async function stripeCardFormSubmit(apiUrl: string, stripe: any, card: any, cart: any) {
94
116
  try {
117
+ if ((cart.totalCost ?? 0) <= 0) {
118
+ return await completeZeroBalanceCheckout(apiUrl, cart);
119
+ }
95
120
  const intent = await createPaymentIntent(apiUrl, cart);
121
+ if (intent.zeroBalance) {
122
+ return await checkPaymentIntnetIsComplete(apiUrl, cart);
123
+ }
96
124
  const stripePayment = await stripe.confirmCardPayment(intent.stripeClientSecret, { payment_method: { card } });
97
125
 
98
126
  if (stripePayment.error) {
@@ -114,7 +142,13 @@ export async function stripePaymentButtonSubmit(
114
142
  const { complete, paymentMethod } = paymentMethodEvent;
115
143
 
116
144
  try {
145
+ if ((cart.totalCost ?? 0) <= 0) {
146
+ return await completeZeroBalanceCheckout(apiUrl, cart);
147
+ }
117
148
  const intent = await createPaymentIntent(apiUrl, cart);
149
+ if (intent.zeroBalance) {
150
+ return await checkPaymentIntnetIsComplete(apiUrl, cart);
151
+ }
118
152
  const stripePayment = await stripe.confirmCardPayment(
119
153
  intent.stripeClientSecret,
120
154
  { payment_method: paymentMethod.id },