forked from pollen8/fabrik
/
stripe-min.js
3 lines (2 loc) · 5.35 KB
/
stripe-min.js
1
2
3
/*! Fabrik */
define(["jquery","fab/fabrik"],function(s,p){"use strict";return new Class({options:{publicKey:"",item:"",zipCode:!0,allowRememberMe:!1,email:"",name:"",panelLabel:"",useCheckout:!0,billingAddress:!1,couponElement:"",renderOrder:""},initialize:function(t){var e=this;if(this.options=s.extend(this.options,t),this.form=p.getBlock("form_"+this.options.formid),p.FabrikStripeForm=null,p.FabrikStripeFormSubmitting=!1,""!==this.options.couponElement){this.couponElement=e.form.formElements.get(this.options.couponElement);var o=this.couponElement.getBlurEvent();e.form.dispatchEvent("",this.options.couponElement,o,function(t){e.getCoupon(t)})}else this.couponElement=!1;if(""!==this.options.productElement){this.productElement=e.form.formElements.get(this.options.productElement);var i=this.productElement.getBlurEvent();e.form.dispatchEvent("",this.options.productElement,i,function(t){e.getCost(t)})}else this.productElement=!1;if(""!==this.options.qtyElement){this.qtyElement=e.form.formElements.get(this.options.qtyElement);var n=this.qtyElement.getBlurEvent();e.form.dispatchEvent("",this.options.qtyElement,n,function(t){e.getCost(t)}),"fabrikfield"===this.qtyElement.plugin&&e.form.dispatchEvent("",this.options.qtyElement,"input",function(t){e.getCost(t)})}else this.qtyElement=!1;if(""!==this.options.totalElement?this.totalElement=e.form.formElements.get(this.options.totalElement):this.totalElement=!1,this.options.useCheckout)requirejs(["https://checkout.stripe.com/checkout.js?"],function(t){e.handler=StripeCheckout.configure({key:e.options.publicKey,image:"https://stripe.com/img/documentation/checkout/marketplace.png",locale:"auto",currency:e.options.currencyCode,token:function(t,e){p.FabrikStripeForm.form.adopt(new Element("input",{name:"stripe_token_id",value:t.id,type:"hidden"})),p.FabrikStripeForm.form.adopt(new Element("input",{name:"stripe_token_email",value:t.email,type:"hidden"})),p.FabrikStripeForm.form.adopt(new Element("input",{name:"stripe_token_opts",value:JSON.stringify(e),type:"hidden"})),p.FabrikStripeForm.mockSubmit()},closed:function(){p.FabrikStripeFormSubmitting=!0}})}),p.addEvent("fabrik.form.submit.start",function(t,e,o){this.options.useCheckout&&(this.options.ccOnFree||0!=this.options.amount)&&(void 0!==p.FabrikStripeForm&&!0===p.FabrikStripeFormSubmitting&&0!==s("input[name=stripe_token_id]").length||(p.FabrikStripeForm=t,this.handler.open({name:this.options.name,description:this.options.item,amount:this.options.amount,zipCode:this.options.zipCode,allowRememberMe:this.options.allowRememberMe,email:this.options.email,panelLabel:this.options.panelLabel,billingAddress:this.options.billingAddress}),e.preventDefault(),t.result=!1))}.bind(this)),window.addEventListener("popstate",function(){this.handler.close()});else if(this.options.updateCheckout){var r=this.form.form.getElement(".fabrikStripeChange");"null"!==typeOf(r)&&requirejs(["https://checkout.stripe.com/checkout.js?"],function(t){e.handler=StripeCheckout.configure({key:e.options.publicKey,image:"https://stripe.com/img/documentation/checkout/marketplace.png",locale:"auto",currency:e.options.currencyCode,token:function(t,e){p.FabrikStripeForm.form.adopt(new Element("input",{name:"stripe_token_id",value:t.id,type:"hidden"})),p.FabrikStripeForm.form.adopt(new Element("input",{name:"stripe_token_email",value:t.email,type:"hidden"})),p.FabrikStripeForm.form.adopt(new Element("input",{name:"stripe_token_opts",value:JSON.stringify(e),type:"hidden"})),s(".fabrikStripeLast4").text(Joomla.JText._("PLG_FORM_STRIPE_CUSTOMERS_UPDATE_CC_UPDATED"))}}),r.addEvent("click",function(t){t.preventDefault(),p.FabrikStripeForm=e.form,e.handler.open({name:e.options.name,description:e.options.item,zipCode:e.options.zipCode,allowRememberMe:e.options.allowRememberMe,email:e.options.email,panelLabel:e.options.panelLabel,billingAddress:e.options.billingAddress})}.bind(this))})}},getCoupon:function(t){p.loader.start("form_"+this.options.formid,Joomla.JText._("PLG_FORM_STRIPE_CALCULATING"));var e=this.couponElement.getValue(),o=this.options.formid,i=this;s.ajax({url:p.liveSite+"index.php",method:"post",dataType:"json",data:{option:"com_fabrik",format:"raw",task:"plugin.pluginAjax",plugin:"stripe",method:"ajax_getCoupon",amount:this.options.origAmount,g:"form",v:e,formid:o,renderOrder:this.options.renderOrder}}).always(function(){p.loader.stop("form_"+i.options.formid)}).fail(function(t,e,o){window.alert(e)}).done(function(t){i.updateForm(t)})},getCost:function(t){this.totalElement&&p.loader.start(this.options.totalElement,Joomla.JText._("PLG_FORM_STRIPE_CALCULATING"));var e=""!==this.options.productElement?this.productElement.getValue():"",o=""!==this.options.qtyElement?this.qtyElement.getValue():"",i=""!==this.options.couponElement?this.couponElement.getValue():"",n=this.options.formid,r=this;s.ajax({dataType:"json",url:p.liveSite+"index.php",method:"post",data:{option:"com_fabrik",format:"raw",task:"plugin.pluginAjax",plugin:"stripe",method:"ajax_getCost",amount:this.options.origAmount,g:"form",productId:e,qty:o,coupon:i,formid:n,renderOrder:this.options.renderOrder}}).always(function(){r.totalElement&&p.loader.stop(r.options.totalElement)}).fail(function(t,e,o){window.alert(e)}).done(function(t){r.updateForm(t)})},updateForm:function(t){this.options.amount=t.stripe_amount,s(".fabrikStripePrice").html(t.display_amount),s(".fabrikStripeCouponText").html(t.msg),this.totalElement&&this.totalElement.update(t.display_amount)}})});