UNPKG

@shopgate/engage

Version:
8 lines 4.32 kB
import _regeneratorRuntime from"@babel/runtime/regenerator";function asyncGeneratorStep(gen,resolve,reject,_next,_throw,key,arg){try{var info=gen[key](arg);var value=info.value;}catch(error){reject(error);return;}if(info.done){resolve(value);}else{Promise.resolve(value).then(_next,_throw);}}function _asyncToGenerator(fn){return function(){var self=this,args=arguments;return new Promise(function(resolve,reject){var gen=fn.apply(self,args);function _next(value){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"next",value);}function _throw(err){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"throw",err);}_next(undefined);});};}import*as Sentry from'@sentry/browser';import{getNeedsPaymentForOrder,getDefaultPaymentMethod,getPaymentMethods}from"../selectors/payment";import{getCheckoutOrder}from"../selectors/order";import{initializeCheckout}from"./initializeCheckout";import{fetchCheckoutOrder}from"./fetchCheckoutOrder";import{updateCheckoutOrder}from"./updateCheckoutOrder";import{fetchPaymentMethods}from"./fetchPaymentMethods";import{errorCheckout}from"./errorCheckout";/** * Starts entering the checkout process for the customer. * @returns {Function} */export var prepareCheckout=function prepareCheckout(_ref){var _ref$initializeOrder=_ref.initializeOrder,initializeOrder=_ref$initializeOrder===void 0?true:_ref$initializeOrder,_ref$initializePaymen=_ref.initializePayment,initializePayment=_ref$initializePaymen===void 0?true:_ref$initializePaymen;return(/*#__PURE__*/function(){var _ref2=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(dispatch,getState){var _order$paymentTransac,_order$paymentTransac2;var defaultPaymentMethod,needsPayment,order,paymentMethods,paymentAlreadyInitialized;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:if(!initializeOrder){_context.next=9;break;}_context.prev=1;_context.next=4;return dispatch(initializeCheckout());case 4:_context.next=9;break;case 6:_context.prev=6;_context.t0=_context["catch"](1);return _context.abrupt("return",dispatch(errorCheckout('checkout.errors.checkCart','shopgate.checkout.initialize',_context.t0)));case 9:_context.next=11;return Promise.all([dispatch(fetchCheckoutOrder()),dispatch(fetchPaymentMethods())]);case 11:// If payment is needed we create a new payment transaction. needsPayment=getNeedsPaymentForOrder(getState());if(!needsPayment){_context.next=16;break;}// Pick payment method. defaultPaymentMethod=getDefaultPaymentMethod(getState());if(defaultPaymentMethod){_context.next=16;break;}return _context.abrupt("return",dispatch(errorCheckout('checkout.errors.missingPaymentMethod','shopgate.checkout.getPaymentMethods',{message:'No payment method found.'})));case 16:// Test if order is available in store. order=getCheckoutOrder(getState());if(order){_context.next=19;break;}return _context.abrupt("return",dispatch(errorCheckout('checkout.errors.genericInitialize','shopgate.checkout.initialize',{message:'No order received from API'})));case 19:// Put logging info to sentry. Sentry.configureScope(function(scope){scope.setTag('checkout_order_id',order.id);scope.addBreadcrumb({category:'checkout',message:"Initialize checkout with order id ".concat(order.id),level:'info'});});paymentMethods=getPaymentMethods(getState());paymentAlreadyInitialized=!!(order===null||order===void 0?void 0:(_order$paymentTransac=order.paymentTransactions)===null||_order$paymentTransac===void 0?void 0:(_order$paymentTransac2=_order$paymentTransac[0])===null||_order$paymentTransac2===void 0?void 0:_order$paymentTransac2.paymentMethod);if(!(needsPayment&&initializePayment&&!paymentAlreadyInitialized&&paymentMethods.length===1)){_context.next=33;break;}_context.prev=23;_context.next=26;return dispatch(updateCheckoutOrder({paymentTransactions:[{paymentMethod:{code:defaultPaymentMethod.code}}]}));case 26:_context.next=28;return dispatch(fetchCheckoutOrder());case 28:_context.next=33;break;case 30:_context.prev=30;_context.t1=_context["catch"](23);return _context.abrupt("return",{success:false,needsPayment:false});case 33:return _context.abrupt("return",{needsPayment:needsPayment,success:true});case 34:case"end":return _context.stop();}},_callee,null,[[1,6],[23,30]]);}));return function(_x,_x2){return _ref2.apply(this,arguments);};}());};