UNPKG

vtex.js

Version:
2 lines 17.1 kB
(function(){function e(t,e){return function(){return t.apply(e,arguments)}}var t,r,i,o,n,s,a,d,h,u,c,m=[].slice;function p(t){null==t&&(t={}),this._getFinishTransactionURL=e(this._getFinishTransactionURL,this),this._getGatewayCallbackURL=e(this._getGatewayCallbackURL,this),this._getProfileURL=e(this._getProfileURL,this),this._getPostalCodeURL=e(this._getPostalCodeURL,this),this._getSimulationURL=e(this._getSimulationURL,this),this._getOrdersURL=e(this._getOrdersURL,this),this._manualPriceURL=e(this._manualPriceURL,this),this._getAddToCartURL=e(this._getAddToCartURL,this),this._getRemoveGiftRegistryURL=e(this._getRemoveGiftRegistryURL,this),this._getUpdateSelectableGifts=e(this._getUpdateSelectableGifts,this),this._getCloneItemURL=e(this._getCloneItemURL,this),this._getUpdateItemURL=e(this._getUpdateItemURL,this),this._startTransactionURL=e(this._startTransactionURL,this),this._getAddCouponURL=e(this._getAddCouponURL,this),this._getCustomDataUrl=e(this._getCustomDataUrl,this),this._getChangeOrdinationURL=e(this._getChangeOrdinationURL,this),this._getItemAttachmentURL=e(this._getItemAttachmentURL,this),this._getBundleItemAttachmentURL=e(this._getBundleItemAttachmentURL,this),this._getRemoveOfferingsURL=e(this._getRemoveOfferingsURL,this),this._getAddOfferingsURL=e(this._getAddOfferingsURL,this),this._getSaveAttachmentURL=e(this._getSaveAttachmentURL,this),this._getOrderFormURLWithId=e(this._getOrderFormURLWithId,this),this._getOrderFormURL=e(this._getOrderFormURL,this),this._getOrderFormIdFromURL=e(this._getOrderFormIdFromURL,this),this._getOrderFormIdFromCookie=e(this._getOrderFormIdFromCookie,this),this._getOrderFormId=e(this._getOrderFormId,this),this.replaceSKU=e(this.replaceSKU,this),this.getChangeToAnonymousUserURL=e(this.getChangeToAnonymousUserURL,this),this.removeAccountId=e(this.removeAccountId,this),this.clearMessages=e(this.clearMessages,this),this.getOrders=e(this.getOrders,this),this.startTransaction=e(this.startTransaction,this),this.getProfileByEmail=e(this.getProfileByEmail,this),this.getAddressInformation=e(this.getAddressInformation,this),this.simulateShipping=e(this.simulateShipping,this),this.calculateShipping=e(this.calculateShipping,this),this.removeGiftRegistry=e(this.removeGiftRegistry,this),this.removeDiscountCoupon=e(this.removeDiscountCoupon,this),this.setCustomData=e(this.setCustomData,this),this.addDiscountCoupon=e(this.addDiscountCoupon,this),this.removeBundleItemAttachment=e(this.removeBundleItemAttachment,this),this.addBundleItemAttachment=e(this.addBundleItemAttachment,this),this.removeItemAttachment=e(this.removeItemAttachment,this),this.addItemAttachment=e(this.addItemAttachment,this),this.removeManualPrice=e(this.removeManualPrice,this),this.setManualPrice=e(this.setManualPrice,this),this.changeItemsOrdination=e(this.changeItemsOrdination,this),this.cloneItem=e(this.cloneItem,this),this.removeAllItems=e(this.removeAllItems,this),this.removeItems=e(this.removeItems,this),this.updateItems=e(this.updateItems,this),this.addToCart=e(this.addToCart,this),this.removeOffering=e(this.removeOffering,this),this.addOffering=e(this.addOffering,this),this.addOfferingWithInfo=e(this.addOfferingWithInfo,this),this.updateSelectableGifts=e(this.updateSelectableGifts,this),this.finishTransaction=e(this.finishTransaction,this),this.sendLocale=e(this.sendLocale,this),this.sendAttachment=e(this.sendAttachment,this),this.getOrderForm=e(this.getOrderForm,this),this._updateOrderForm=e(this._updateOrderForm,this),this._broadcastOrderFormUnlessPendingRequests=e(this._broadcastOrderFormUnlessPendingRequests,this),this._decreasePendingRequests=e(this._decreasePendingRequests,this),this._increasePendingRequests=e(this._increasePendingRequests,this),this._cacheOrderForm=e(this._cacheOrderForm,this),t.hostURL&&(u=t.hostURL),t.ajax?this.ajax=t.ajax:window.AjaxQueue?this.ajax=window.AjaxQueue($.ajax):this.ajax=$.ajax,this.promise=t.promise||$.when,this.CHECKOUT_ID="checkout",this.orderForm=void 0,this.orderFormId=void 0,this._pendingRequestCounter=0,this._urlToRequestMap={},this._allOrderFormSections=["items","totalizers","clientProfileData","shippingData","paymentData","sellers","messages","marketingData","clientPreferencesData","storePreferencesData","giftRegistryData","ratesAndBenefitsData","openTextField","commercialConditionData","customData"]}s=function(t){return t.replace(/^\s+|\s+$/g,"")},r=function(t,e,r,i,o){var n,s,a,d,h,u,c;for(s={},d=0,h=(u=t.split(e)).length;d<h;d++)n=(c=u[d].split(r))[0],a=2<=c.length?m.call(c,1):[],s[i(n)]=o(a.join("="));return s},d=function(){return r(window.location.search.substring(1),"&","=",decodeURIComponent,decodeURIComponent)},a=function(t){return d()[t]},o=function(){return r(document.cookie,";","=",s,unescape)},i=function(t){return o()[t]},n=function(t,e){return r(e,"&","=",function(t){return t},unescape)[t]},(h=window.location).origin||(h.origin=window.location.protocol+"//"+window.location.hostname+(window.location.port?":"+window.location.port:"")),u=window.location.origin,c={ORDER_FORM_UPDATED:"orderFormUpdated.vtex",REQUEST_BEGIN:"checkoutRequestBegin.vtex",REQUEST_END:"checkoutRequestEnd.vtex"},p.prototype._cacheOrderForm=function(t){return this.orderFormId=t.orderFormId,this.orderForm=t},p.prototype._increasePendingRequests=function(t){return this._pendingRequestCounter++,$(window).trigger(c.REQUEST_BEGIN,[t])},p.prototype._decreasePendingRequests=function(){return this._pendingRequestCounter--,$(window).trigger(c.REQUEST_END,arguments)},p.prototype._broadcastOrderFormUnlessPendingRequests=function(t){if(0===this._pendingRequestCounter)return $(window).trigger(c.ORDER_FORM_UPDATED,[t])},p.prototype._orderFormHasExpectedSections=function(t,e){var r,i;if(!t||!t instanceof Object)return!1;for(r=0,i=e.length;r<i;r++)if(!t[e[r]])return!1;return!0},p.prototype._updateOrderForm=function(t){var e,r,i=this;if(!(null!=t?t.url:void 0))throw new Error("options.url is required when sending request");return t.type||(t.type="POST"),t.contentType||(t.contentType="application/json; charset=utf-8"),t.dataType||(t.dataType="json"),this._increasePendingRequests(t),e=this.ajax(t),null!=(r=this._urlToRequestMap[t.url])&&r.abort(),(this._urlToRequestMap[t.url]=e).always(function(){return delete i._urlToRequestMap[t.url]}),e.always(this._decreasePendingRequests),e.done(this._cacheOrderForm),e.done(this._broadcastOrderFormUnlessPendingRequests),e},p.prototype.getOrderForm=function(t){var e,r;return null==t&&(t=this._allOrderFormSections),this._orderFormHasExpectedSections(this.orderForm,t)?this.promise(this.orderForm):(e={expectedOrderFormSections:t},(r=this.ajax({url:this._getOrderFormURLWithId(),type:"POST",contentType:"application/json; charset=utf-8",dataType:"json",data:JSON.stringify(e)})).done(this._cacheOrderForm),r.done(this._broadcastOrderFormUnlessPendingRequests))},p.prototype.sendAttachment=function(t,e,r){var i;return null==r&&(r=this._allOrderFormSections),void 0===t||void 0===e?((i=$.Deferred()).reject("Invalid arguments"),i.promise()):(e.expectedOrderFormSections=r,this._updateOrderForm({url:this._getSaveAttachmentURL(t),data:JSON.stringify(e)}))},p.prototype.sendLocale=function(t){return null==t&&(t="pt-BR"),this.sendAttachment("clientPreferencesData",{locale:t},[])},p.prototype.finishTransaction=function(t,e){return null==e&&(e=this._allOrderFormSections),this._updateOrderForm({url:this._getFinishTransactionURL(t)})},p.prototype.updateSelectableGifts=function(t,e,r){var i;return null==r&&(r=this._allOrderFormSections),i={id:t,selectedGifts:e,expectedOrderFormSections:r},this._updateOrderForm({url:this._getUpdateSelectableGifts(t),data:JSON.stringify(i)})},p.prototype.addOfferingWithInfo=function(t,e,r,i){var o;return null==i&&(i=this._allOrderFormSections),o={id:t,info:e,expectedOrderFormSections:i},this._updateOrderForm({url:this._getAddOfferingsURL(r),data:JSON.stringify(o)})},p.prototype.addOffering=function(t,e,r){return this.addOfferingWithInfo(t,null,e,r)},p.prototype.removeOffering=function(t,e,r){var i;return null==r&&(r=this._allOrderFormSections),i={Id:t,expectedOrderFormSections:r},this._updateOrderForm({url:this._getRemoveOfferingsURL(e,t),data:JSON.stringify(i)})},p.prototype.addToCart=function(t,e,r){var i,o;return null==e&&(e=this._allOrderFormSections),i={orderItems:t,expectedOrderFormSections:e},o="",r&&(o="?sc="+r),this._updateOrderForm({url:this._getAddToCartURL()+o,data:JSON.stringify(i)})},p.prototype.updateItems=function(t,e,r){var i;return null==e&&(e=this._allOrderFormSections),null==r&&(r=!0),i={orderItems:t,expectedOrderFormSections:e,noSplitItem:!r},this._updateOrderForm({url:this._getUpdateItemURL(),data:JSON.stringify(i)})},p.prototype.removeItems=function(t,e){var r,i,o,n,s;if(null==e&&(e=this._allOrderFormSections),t&&0===t.length)return this.getOrderForm(e);for(o=[],r=n=0,s=t.length;n<s;r=++n)i=t[r],o.push({index:i.index,quantity:0});return this.updateItems(o,e)},p.prototype.removeAllItems=function(s){var a=this;return null==s&&(s=this._allOrderFormSections),this.getOrderForm(["items"]).then(function(t){var e,r,i,o,n;if((r=t.items)&&0===r.length)return t;for(i=[],e=o=0,n=r.length;o<n;e=++o)r[e],i.push({index:e,quantity:0});return a.updateItems(i,s)})},p.prototype.cloneItem=function(t,e,r){return null==r&&(r=this._allOrderFormSections),this._updateOrderForm({url:this._getCloneItemURL(t),data:JSON.stringify(e)})},p.prototype.changeItemsOrdination=function(t,e,r){var i;return null==r&&(r=this._allOrderFormSections),i={criteria:t,ascending:e,expectedOrderFormSections:r},this._updateOrderForm({url:this._getChangeOrdinationURL(),data:JSON.stringify(i)})},p.prototype.setManualPrice=function(t,e){var r;return r={price:e},this._updateOrderForm({url:this._manualPriceURL(t),type:"PUT",contentType:"application/json; charset=utf-8",dataType:"json",data:JSON.stringify(r)})},p.prototype.removeManualPrice=function(t){return this._updateOrderForm({url:this._manualPriceURL(t),type:"DELETE",contentType:"application/json; charset=utf-8",dataType:"json"})},p.prototype.addItemAttachment=function(t,e,r,i,o){var n;return null==i&&(i=this._allOrderFormSections),null==o&&(o=!0),n={content:r,expectedOrderFormSections:i,noSplitItem:!o},this._updateOrderForm({url:this._getItemAttachmentURL(t,e),data:JSON.stringify(n)})},p.prototype.removeItemAttachment=function(t,e,r,i){var o;return null==i&&(i=this._allOrderFormSections),o={content:r,expectedOrderFormSections:i},this._updateOrderForm({url:this._getItemAttachmentURL(t,e),type:"DELETE",data:JSON.stringify(o)})},p.prototype.addBundleItemAttachment=function(t,e,r,i,o){var n;return null==o&&(o=this._allOrderFormSections),n={content:i,expectedOrderFormSections:o},this._updateOrderForm({url:this._getBundleItemAttachmentURL(t,e,r),data:JSON.stringify(n)})},p.prototype.removeBundleItemAttachment=function(t,e,r,i,o){var n;return null==o&&(o=this._allOrderFormSections),n={content:i,expectedOrderFormSections:o},this._updateOrderForm({url:this._getBundleItemAttachmentURL(t,e,r),type:"DELETE",data:JSON.stringify(n)})},p.prototype.addDiscountCoupon=function(t,e){var r;return null==e&&(e=this._allOrderFormSections),r={text:t,expectedOrderFormSections:e},this._updateOrderForm({url:this._getAddCouponURL(),data:JSON.stringify(r)})},p.prototype.setCustomData=function(t){var e;return e={value:t.value},this._updateOrderForm({type:"PUT",url:this._getCustomDataUrl({app:t.app,field:t.field}),data:JSON.stringify(e)})},p.prototype.removeDiscountCoupon=function(t){return this.addDiscountCoupon("",t)},p.prototype.removeGiftRegistry=function(t){var e;return null==t&&(t=this._allOrderFormSections),e={expectedOrderFormSections:t},this._updateOrderForm({url:this._getRemoveGiftRegistryURL(),data:JSON.stringify(e)})},p.prototype.calculateShipping=function(t){return this.sendAttachment("shippingData",{address:t})},p.prototype.simulateShipping=function(){var t,e,r,i,o,n,s;return e=null,t=(o=[arguments[2],arguments[3]])[0],r=o[1],e=Array.isArray(arguments[0])?(console.warn("Calling simulateShipping with a list of items and postal code is deprecated.\nCall it with shippingData and orderFormId instead."),{items:(n=[arguments[0],arguments[1]])[0],postalCode:n[1],country:t}):{shippingData:(s=[arguments[0],arguments[1]])[0],orderFormId:s[1],country:t},i="",r&&(i="?sc="+r),this.ajax({url:this._getSimulationURL()+i,type:"POST",contentType:"application/json; charset=utf-8",dataType:"json",data:JSON.stringify(e)})},p.prototype.getAddressInformation=function(t){return this.ajax({url:this._getPostalCodeURL(t.postalCode,t.country),type:"GET",timeout:2e4})},p.prototype.getProfileByEmail=function(t,e){return null==e&&(e=1),this.ajax({url:this._getProfileURL(),type:"GET",data:{email:t,sc:e}})},p.prototype.startTransaction=function(t,e,r,i,o,n){var s;return null==i&&(i=!1),null==n&&(n=this._allOrderFormSections),s={referenceId:this._getOrderFormId(),savePersonalData:i,optinNewsLetter:o,value:t,referenceValue:e,interestValue:r,expectedOrderFormSections:n},this._updateOrderForm({url:this._startTransactionURL(),data:JSON.stringify(s)})},p.prototype.getOrders=function(t){return this.ajax({url:this._getOrdersURL(t),type:"GET",contentType:"application/json; charset=utf-8",dataType:"json"})},p.prototype.clearMessages=function(t){var e;return null==t&&(t=this._allOrderFormSections),e={expectedOrderFormSections:t},this.ajax({url:this._getOrderFormURL()+"/messages/clear",type:"POST",contentType:"application/json; charset=utf-8",dataType:"json",data:JSON.stringify(e)})},p.prototype.removeAccountId=function(t,e){var r;return null==e&&(e=this._allOrderFormSections),r={expectedOrderFormSections:e},this._updateOrderForm({url:this._getOrderFormURL()+"/paymentAccount/"+t+"/remove",data:JSON.stringify(r)})},p.prototype.getLogoutURL=p.prototype.getChangeToAnonymousUserURL=function(){return u+"/checkout/changeToAnonymousUser/"+this._getOrderFormId()},p.prototype.replaceSKU=function(t,e,r){return null==e&&(e=this._allOrderFormSections),null==r&&(r=!0),this._updateOrderForm({url:this._getAddToCartURL(),type:"PATCH",data:JSON.stringify({orderItems:t,expectedOrderFormSections:e,noSplitItem:!r})})},p.prototype._getOrderFormId=function(){return this._getOrderFormIdFromURL()||this.orderFormId||this._getOrderFormIdFromCookie()||""},p.prototype._getOrderFormIdFromCookie=function(){var t;if(void 0!==(t=i("checkout.vtex.com"))&&""!==t)return n(t,"__ofid")},p.prototype._getOrderFormIdFromURL=function(){return a("orderFormId")},p.prototype._getBaseOrderFormURL=function(){return u+"/api/checkout/pub/orderForm"},p.prototype._getOrderFormURL=function(){var t;if(""===(t=this._getOrderFormId()))throw new Error("This method requires an OrderForm. Use getOrderForm beforehand.");return this._getBaseOrderFormURL()+"/"+t},p.prototype._getOrderFormURLWithId=function(){var t;return(t=this._getOrderFormId())?this._getBaseOrderFormURL()+"/"+t:this._getBaseOrderFormURL()},p.prototype._getSaveAttachmentURL=function(t){return this._getOrderFormURL()+"/attachments/"+t},p.prototype._getAddOfferingsURL=function(t){return this._getOrderFormURL()+"/items/"+t+"/offerings"},p.prototype._getRemoveOfferingsURL=function(t,e){return this._getOrderFormURL()+"/items/"+t+"/offerings/"+e+"/remove"},p.prototype._getBundleItemAttachmentURL=function(t,e,r){return this._getOrderFormURL()+"/items/"+t+"/bundles/"+e+"/attachments/"+r},p.prototype._getItemAttachmentURL=function(t,e){return this._getOrderFormURL()+"/items/"+t+"/attachments/"+e},p.prototype._getChangeOrdinationURL=function(){return this._getOrderFormURL()+"/itemsOrdination"},p.prototype._getCustomDataUrl=function(t){return this._getOrderFormURL()+"/customData/"+t.app+"/"+t.field},p.prototype._getAddCouponURL=function(){return this._getOrderFormURL()+"/coupons"},p.prototype._startTransactionURL=function(){return this._getOrderFormURL()+"/transaction"},p.prototype._getUpdateItemURL=function(){return this._getOrderFormURL()+"/items/update/"},p.prototype._getCloneItemURL=function(t){return this._getOrderFormURL()+"/items/"+t+"/clone"},p.prototype._getUpdateSelectableGifts=function(t){return this._getOrderFormURL()+"/selectable-gifts/"+t},p.prototype._getRemoveGiftRegistryURL=function(){return this._getBaseOrderFormURL()+"/giftRegistry/"+this._getOrderFormId()+"/remove"},p.prototype._getAddToCartURL=function(){return this._getOrderFormURL()+"/items"},p.prototype._manualPriceURL=function(t){return this._getOrderFormURL()+"/items/"+t+"/price"},p.prototype._getOrdersURL=function(t){return u+"/api/checkout/pub/orders/order-group/"+t},p.prototype._getSimulationURL=function(){return u+"/api/checkout/pub/orderForms/simulation"},p.prototype._getPostalCodeURL=function(t,e){return null==t&&(t=""),null==e&&(e="BRA"),u+"/api/checkout/pub/postal-code/"+e+"/"+t},p.prototype._getProfileURL=function(){return u+"/api/checkout/pub/profiles/"},p.prototype._getGatewayCallbackURL=function(){return u+"/checkout/gatewayCallback/{0}/{1}/{2}"},p.prototype._getFinishTransactionURL=function(t){return u+"/api/checkout/pub/gatewayCallback/"+t},t=p,window.vtexjs||(window.vtexjs={}),window.vtexjs.Checkout=t,window.vtexjs.checkout=new window.vtexjs.Checkout}).call(this); //# sourceMappingURL=checkout.min.js.map