* Add jquery form event 'crmBillingFormReloadComplete' and document jquery events.
* Add jquery form event 'crmBillingFormNotValid' so 3rd-party integrations can re-enable custom submit buttons etc.
* Add support for sweetalert library on form validation errors so we popup nice messages when you are missing required fields and for card errors and you click submit.
* Make sure we don't submit if we have a reCaptcha and it is not valid.
CRM.$(function(e){h("civicrm_stripe loaded, dom-ready function firing.");if(window.civicrmStripeHandleReload){h("calling existing civicrmStripeHandleReload.");window.civicrmStripeHandleReload();return}varu;varb;varc;vary;vars=false;window.onbeforeunload=null;window.civicrmStripeHandleReload=function(){h("civicrmStripeHandleReload");varF=document.getElementById("card-element");if((typeofF!=="undefined")&&(F)){if(!F.children.length){h("checkAndLoad from document.ready");q()}}d("crmBillingFormReloadComplete")};window.civicrmStripeHandleReload();functionz(H,F){h(H+": success - submitting form");varG=document.createElement("input");G.setAttribute("type","hidden");G.setAttribute("name",H);G.setAttribute("value",F.id);c.appendChild(G);c.submit()}functiont(){for(i=0;i<y.length;++i){y[i].setAttribute("disabled",true)}returnc.submit()}functionk(G){h("error: "+G.message);varF=document.getElementById("card-errors");F.style.display="block";F.textContent=G.message;document.querySelector("#billing-payment-block").scrollIntoView();window.scrollBy(0,-50);c.dataset.submitted=false;for(i=0;i<y.length;++i){y[i].removeAttribute("disabled")}d("crmBillingFormNotValid");l("error","",result.error.message)}functionD(){h("handle card payment");u.createPaymentMethod("card",b).then(function(F){if(F.error){k(F.error)}else{if(j()||a()){z("paymentMethodID",F.paymentMethod)}else{varG=CRM.url("civicrm/stripe/confirm-payment");e.post(G,{payment_method_id:F.paymentMethod.id,amount:v().toFixed(2),currency:CRM.vars.stripe.currency,id:CRM.vars.stripe.id,description:document.title,csrfToken:CRM.vars.stripe.csrfToken}).then(function(H){A(H)})}}})}functionA(F){h("handleServerResponse");if(F.error){k(F.error)}else{if(F.requires_action){w(F)}else{z("paymentIntentID",F.paymentIntent)}}}functionw(F){u.handleCardAction(F.payment_intent_client_secret).then(function(G){if(G.error){k(G.error)}else{z("paymentIntentID",G.paymentIntent)}})}e(document).ajaxComplete(function(H,I,G){if((G.url.match("civicrm(/|%2F)payment(/|%2F)form")!==null)||(G.url.match("civicrm(/|%2F)contact(/|%2F)view(/|%2F)participant")!==null)){if(typeofCRM.vars.stripe==="undefined"){return}varF=o();if(F!==null){if(F!==parseInt(CRM.vars.stripe.id)){h("payment processor changed to id: "+F);if(F===0){returnp()}CRM.api3("PaymentProcessor","getvalue",{"return":"user_name",id:F,payment_processor_type_id:CRM.vars.stripe.paymentProcessorTypeID}).done(function(J){varK=J.result;if(K){h("Setting new stripe key to: "+K);CRM.vars.stripe.publishableKey=K}else{returnp()}h("checkAndLoad from ajaxComplete");q()})}}}});functionp(){h("New payment processor is not Stripe, clearing CRM.vars.stripe");if((typeofb!=="undefined")&&(b)){h("destroying card element");b.destroy();b=undefined}delete (CRM.vars.stripe)}functionq(){if(typeofCRM.vars.stripe==="undefined"){h("CRM.vars.stripe not defined! Not a Stripe processor?");return}if(typeofStripe==="undefined"){if(s){return}s=true;h("Stripe.js is not loaded!");e.getScript("https://js.stripe.com/v3",function(){h("Script loaded and executed.");s=false;g()})}else{g()}}functiong(){h("loadStripeBillingBlock");if(typeofu==="undefined"){u=Stripe(CRM.vars.stripe.publishableKey)}varM=u.elements({locale:CRM.vars.stripe.locale});varJ={base:{fontSize:"20px"}};varG=document.getElementById("billing_postal_code-"+CRM.vars.stripe.billingAddressID).value;h("existing postcode: "+G);b=M.create("card",{style:J,value:{postalCode:G}});b.mount("#card-element");h("created new card element",b);if(document.getElementById("billing_postal_code-5").value){document.getElementById("billing_postal_code-5").setAttribute("disabled",true)}else{document.getElementsByClassName("billing_postal_code-"+CRM.vars.stripe.billingAddressID+"-section")[0].setAttribute("hidden",true)}b.addEventListener("change",function(N){m(N)});c=n();if(typeofc.length==="undefined"||c.length===0){h("No billing form!");return}f();y=E();c.dataset.submitdontprocess=false;varF=c.querySelectorAll('[type="submit"][formnovalidate="1"], [type="submit"][formnovalidate="formnovalidate"], [type="submit"].cancel, [type="submit"].webform-previous'),I;for(I=0;I<F.length;++I){F[I].addEventListener("click",L())}functionL(){h("adding submitdontprocess");c.dataset.submitdontprocess=true}for(I=0;I<y.length;++I){y[I].addEventListener("click",H)}functionH(N){if(c.dataset.submitted===true){return}c.dataset.submitted=true;if(typeofCRM.vars.stripe==="undefined"){returnt()}h("clearing submitdontprocess");c.dataset.submitdontprocess=false;returnK(N)}for(I=0;I<y.length;++I){y[I].removeAttribute("onclick")}r();if(C()){e("[type=submit]").click(function(){x(this.value)});c.addEventListener("keydown",function(N){if(N.code==="Enter"){x(this.value);K(N)}});e("#billingcheckbox:input").hide();e('label[for="billingcheckbox"]').hide()}functionK(P){P.preventDefault();h("submit handler");if(e(c).valid()===false){h("Form not valid");e("div#card-errors").hide();document.querySelector("#billing-payment-block").scrollIntoView();window.scrollBy(0,-50);d("crmBillingFormNotValid");l("error","",ts("Please check and fill in all required fields!"));returnfalse}if(typeofCRM.vars.stripe==="undefined"){h("Submitting - not a stripe processor");returntrue}if(c.dataset.submitted===true){h("form already submitted");returnfalse}varR=parseInt(CRM.vars.stripe.id);varO=null;if(C()){if(!e('input[name="submitted[civicrm_1_contribution_1_contribution_payment_processor_id]"]').length){O=R}else{O=parseInt(c.querySelector('input[name="submitted[civicrm_1_contribution_1_contribution_payment_processor_id]"]:checked').value)}}else{if((c.querySelector(".crm-section.payment_processor-section")!==null)||(c.querySelector(".crm-section.credit_card_info-section")!==null)){R=CRM.vars.stripe.id;if(c.querySelector('input[name="payment_processor_id"]:checked')!==null){O=parseInt(c.querySelector('input[name="payment_processor_id"]:checked').value)}}}if((O===0)||(R===null)||((O===null)&&(R===null))){h("Not a Stripe transaction, or pay-later");returnt()}else{h("Stripe is the selected payprocessor")}if(typeofCRM.vars.stripe.publishableKey==="undefined"){h("submit missing stripe-pub-key element or value");returntrue}if(c.dataset.submitdontprocess===true){h("non-payment submit detected - not submitting payment");returntrue}if(C()){if(e("#billing-payment-block").is(":hidden")){h("no payment processor on webform");returntrue}varQ=e('[name="submitted[civicrm_1_contribution_1_contribution_payment_processor_id]"]');if(Q.length){if(Q.filter(":checked").val()==="0"||Q.filter(":checked").val()===0){h("no payment processor selected");returntrue}}}varN=v();if(N===0){h("Total amount is 0");returnt()}if(c.dataset.submitted===true){alert("Form already submitted. Please wait.");returnfalse}else{c.dataset.submitted=true}for(I=0;I<y.length;++I){y[I].setAttribute("disabled",true)}D();returntrue}}functionC(){if(c!==null){returnc.classList.contains("webform-client-form")||c.classList.contains("webform-submission-form")}returnfalse}functionn(){varF=e("div#card-element").closest("form").prop("id");if((typeofF==="undefined")||(!F.length)){F=e("input[name=hidden_processor]").closest("form").prop("id")}returndocument.getElementById(F)}functionE(){varF=null;if(C()){F=c.querySelectorAll('[type="submit"].webform-submit');if(!F){F=c.querySelectorAll('[type="submit"].webform-button--submit')}}else{F=c.querySelectorAll('[type="submit"].validate')}returnF}functionv(){varF=0;if(a()){F=null}else{if(document.getElementById("totalTaxAmount")!==null){F=parseFloat(B());h("Calculated amount using internal calculateTaxAmount()")}else{if(typeofcalculateTotalFee=="function"){F=parseFloat(calculateTotalFee())}else{if(C()){e(".line-item:visible","#wf-crm-billing-items").each(function(){F+=parseFloat(e(this).data("amount"))})}else{if(document.getElementById("total_amount")){F=parseFloat(document.getElementById("total_amount").value)}}}}}h("getTotalAmount: "+F);returnF}functionB(){varF=0;if(document.getElementById("totalTaxAmount")===null){returnF}if(document.getElementById("totalTaxAmount").textContent.length===0){F=document.getElementById("total_amount").value}else{F=document.getElementById("totalTaxAmount").textContent.split("").pop()}returnF}functionj(){varF=false;if(C()){if(e('input[id$="contribution-installments"]').length!==0&&e('input[id$="contribution-installments"]').val()>1){F=true}}if(document.getElementById("is_recur")!==null){if(document.getElementById("is_recur").type=="hidden"){F=(document.getElementById("is_recur").value==1)}else{F=Boolean(document.getElementById("is_recur").checked)}}else{if(e('input[name="auto_renew"]').length!==0){if(e('input[name="auto_renew"]').prop("checked")){F=true}else{if(document.getElementById("auto_renew").type=="hidden"){F=(document.getElementById("auto_renew").value==1)}else{F=Boolean(document.getElementById("auto_renew").checked)}}}}h("isRecur is "+F);returnF}functionm(G){if(G.empty){e("div#card-errors").hide()}else{if(G.error){k(G.error)}else{if(G.complete){e("div#card-errors").hide();varF=document.getElementById("billing_postal_code-"+CRM.vars.stripe.billingAddressID);if(F){F.value=G.value.postalCode}}}}}functionr(){cividiscountElements=c.querySelectorAll("input#discountcode");varF=function(G){if(G.code==="Enter"){G.preventDefault();h("adding submitdontprocess");c.dataset.submitdontprocess=true}};for(i=0;i<cividiscountElements.length;++i){cividiscountElements[i].addEventListener("keydown",F)}}functionf(){e("div.label span.crm-marker").each(function(){e(this).closest("div").next("div").find("input").addClass("required")});varF=e(c).validate();F.settings.errorClass="crm-inline-error alert-danger";F.settings.ignore=".select2-offscreen, [readonly], :hidden:not(.crm-select2)"}functiona(){if((document.getElementById("additional_participants")!==null)&&(document.getElementById("additional_participants").value.length!==0)){h("We don't know the final price - registering additional participants");returntrue}returnfalse}functionx(G){varF=null;if(document.getElementById("action")!==null){F=document.getElementById("action")}else{F=document.createElement("input")}F.setAttribute("type","hidden");F.setAttribute("name","op");F.setAttribute("id","action");F.setAttribute("value",G);c.appendChild(F)}functiono(){if((typeofc==="undefined")||(!c)){c=n();if(!c){returnnull}}varF=c.querySelector('input[name="payment_processor_id"]:checked');if(F!==null){returnparseInt(F.value)}returnnull}functionh(F){if((typeof(CRM.vars.stripe)==="undefined")||(Boolean(CRM.vars.stripe.jsDebug)===true)){console.log(newDate().toISOString()+" civicrm_stripe.js: "+F)}}functiond(F){h("Firing Event: "+F);e(c).trigger(F)}functionl(F,H,G){if(typeofSwal==="function"){Swal.fire({icon:F,title:H,text:G})}}});
\ No newline at end of file
CRM.$(function(e){h("civicrm_stripe loaded, dom-ready function firing.");if(window.civicrmStripeHandleReload){h("calling existing civicrmStripeHandleReload.");window.civicrmStripeHandleReload();return}varu;varb;varc;vary;vars=false;window.onbeforeunload=null;window.civicrmStripeHandleReload=function(){h("civicrmStripeHandleReload");varF=document.getElementById("card-element");if((typeofF!=="undefined")&&(F)){if(!F.children.length){h("checkAndLoad from document.ready");q()}}d("crmBillingFormReloadComplete")};window.civicrmStripeHandleReload();functionz(H,F){h(H+": success - submitting form");varG=document.createElement("input");G.setAttribute("type","hidden");G.setAttribute("name",H);G.setAttribute("value",F.id);c.appendChild(G);c.submit()}functiont(){for(i=0;i<y.length;++i){y[i].setAttribute("disabled",true)}returnc.submit()}functionk(G){h("error: "+G.message);varF=document.getElementById("card-errors");F.style.display="block";F.textContent=G.message;document.querySelector("#billing-payment-block").scrollIntoView();window.scrollBy(0,-50);c.dataset.submitted=false;for(i=0;i<y.length;++i){y[i].removeAttribute("disabled")}d("crmBillingFormNotValid");l("error","",result.error.message)}functionD(){h("handle card payment");u.createPaymentMethod("card",b).then(function(F){if(F.error){k(F.error)}else{if(j()||a()){z("paymentMethodID",F.paymentMethod)}else{varG=CRM.url("civicrm/stripe/confirm-payment");e.post(G,{payment_method_id:F.paymentMethod.id,amount:v().toFixed(2),currency:CRM.vars.stripe.currency,id:CRM.vars.stripe.id,description:document.title,csrfToken:CRM.vars.stripe.csrfToken}).then(function(H){A(H)})}}})}functionA(F){h("handleServerResponse");if(F.error){k(F.error)}else{if(F.requires_action){w(F)}else{z("paymentIntentID",F.paymentIntent)}}}functionw(F){u.handleCardAction(F.payment_intent_client_secret).then(function(G){if(G.error){k(G.error)}else{z("paymentIntentID",G.paymentIntent)}})}e(document).ajaxComplete(function(H,I,G){if((G.url.match("civicrm(/|%2F)payment(/|%2F)form")!==null)||(G.url.match("civicrm(/|%2F)contact(/|%2F)view(/|%2F)participant")!==null)){if(typeofCRM.vars.stripe==="undefined"){return}varF=o();if(F!==null){if(F!==parseInt(CRM.vars.stripe.id)){h("payment processor changed to id: "+F);if(F===0){returnp()}CRM.api3("PaymentProcessor","getvalue",{"return":"user_name",id:F,payment_processor_type_id:CRM.vars.stripe.paymentProcessorTypeID}).done(function(J){varK=J.result;if(K){h("Setting new stripe key to: "+K);CRM.vars.stripe.publishableKey=K}else{returnp()}h("checkAndLoad from ajaxComplete");q()})}}}});functionp(){h("New payment processor is not Stripe, clearing CRM.vars.stripe");if((typeofb!=="undefined")&&(b)){h("destroying card element");b.destroy();b=undefined}delete (CRM.vars.stripe)}functionq(){if(typeofCRM.vars.stripe==="undefined"){h("CRM.vars.stripe not defined! Not a Stripe processor?");return}if(typeofStripe==="undefined"){if(s){return}s=true;h("Stripe.js is not loaded!");e.getScript("https://js.stripe.com/v3",function(){h("Script loaded and executed.");s=false;g()})}else{g()}}functiong(){h("loadStripeBillingBlock");if(typeofu==="undefined"){u=Stripe(CRM.vars.stripe.publishableKey)}varM=u.elements({locale:CRM.vars.stripe.locale});varJ={base:{fontSize:"20px"}};varG=document.getElementById("billing_postal_code-"+CRM.vars.stripe.billingAddressID).value;h("existing postcode: "+G);b=M.create("card",{style:J,value:{postalCode:G}});b.mount("#card-element");h("created new card element",b);if(document.getElementById("billing_postal_code-5").value){document.getElementById("billing_postal_code-5").setAttribute("disabled",true)}else{document.getElementsByClassName("billing_postal_code-"+CRM.vars.stripe.billingAddressID+"-section")[0].setAttribute("hidden",true)}b.addEventListener("change",function(N){m(N)});c=n();if(typeofc.length==="undefined"||c.length===0){h("No billing form!");return}f();y=E();c.dataset.submitdontprocess=false;varF=c.querySelectorAll('[type="submit"][formnovalidate="1"], [type="submit"][formnovalidate="formnovalidate"], [type="submit"].cancel, [type="submit"].webform-previous'),I;for(I=0;I<F.length;++I){F[I].addEventListener("click",L())}functionL(){h("adding submitdontprocess");c.dataset.submitdontprocess=true}for(I=0;I<y.length;++I){y[I].addEventListener("click",H)}functionH(N){if(c.dataset.submitted===true){return}c.dataset.submitted=true;if(typeofCRM.vars.stripe==="undefined"){returnt()}h("clearing submitdontprocess");c.dataset.submitdontprocess=false;returnK(N)}for(I=0;I<y.length;++I){y[I].removeAttribute("onclick")}r();if(C()){e("[type=submit]").click(function(){x(this.value)});c.addEventListener("keydown",function(N){if(N.code==="Enter"){x(this.value);K(N)}});e("#billingcheckbox:input").hide();e('label[for="billingcheckbox"]').hide()}functionK(P){P.preventDefault();h("submit handler");if(e(c).valid()===false){h("Form not valid");e("div#card-errors").hide();document.querySelector("#billing-payment-block").scrollIntoView();window.scrollBy(0,-50);d("crmBillingFormNotValid");l("error","",ts("Please check and fill in all required fields!"));returnfalse}if(!(typeofgrecaptcha==="undefined"||(grecaptcha&&grecaptcha.getResponse().length!==0))){h("recaptcha active and not valid");e("div#card-errors").hide();d("crmBillingFormNotValid");l("error","",ts("Please complete the reCaptcha"));returnfalse}if(typeofCRM.vars.stripe==="undefined"){h("Submitting - not a stripe processor");returntrue}if(c.dataset.submitted===true){h("form already submitted");returnfalse}varR=parseInt(CRM.vars.stripe.id);varO=null;if(C()){if(!e('input[name="submitted[civicrm_1_contribution_1_contribution_payment_processor_id]"]').length){O=R}else{O=parseInt(c.querySelector('input[name="submitted[civicrm_1_contribution_1_contribution_payment_processor_id]"]:checked').value)}}else{if((c.querySelector(".crm-section.payment_processor-section")!==null)||(c.querySelector(".crm-section.credit_card_info-section")!==null)){R=CRM.vars.stripe.id;if(c.querySelector('input[name="payment_processor_id"]:checked')!==null){O=parseInt(c.querySelector('input[name="payment_processor_id"]:checked').value)}}}if((O===0)||(R===null)||((O===null)&&(R===null))){h("Not a Stripe transaction, or pay-later");returnt()}else{h("Stripe is the selected payprocessor")}if(typeofCRM.vars.stripe.publishableKey==="undefined"){h("submit missing stripe-pub-key element or value");returntrue}if(c.dataset.submitdontprocess===true){h("non-payment submit detected - not submitting payment");returntrue}if(C()){if(e("#billing-payment-block").is(":hidden")){h("no payment processor on webform");returntrue}varQ=e('[name="submitted[civicrm_1_contribution_1_contribution_payment_processor_id]"]');if(Q.length){if(Q.filter(":checked").val()==="0"||Q.filter(":checked").val()===0){h("no payment processor selected");returntrue}}}varN=v();if(N===0){h("Total amount is 0");returnt()}if(c.dataset.submitted===true){alert("Form already submitted. Please wait.");returnfalse}else{c.dataset.submitted=true}for(I=0;I<y.length;++I){y[I].setAttribute("disabled",true)}D();returntrue}}functionC(){if(c!==null){returnc.classList.contains("webform-client-form")||c.classList.contains("webform-submission-form")}returnfalse}functionn(){varF=e("div#card-element").closest("form").prop("id");if((typeofF==="undefined")||(!F.length)){F=e("input[name=hidden_processor]").closest("form").prop("id")}returndocument.getElementById(F)}functionE(){varF=null;if(C()){F=c.querySelectorAll('[type="submit"].webform-submit');if(!F){F=c.querySelectorAll('[type="submit"].webform-button--submit')}}else{F=c.querySelectorAll('[type="submit"].validate')}returnF}functionv(){varF=0;if(a()){F=null}else{if(document.getElementById("totalTaxAmount")!==null){F=parseFloat(B());h("Calculated amount using internal calculateTaxAmount()")}else{if(typeofcalculateTotalFee=="function"){F=parseFloat(calculateTotalFee())}else{if(C()){e(".line-item:visible","#wf-crm-billing-items").each(function(){F+=parseFloat(e(this).data("amount"))})}else{if(document.getElementById("total_amount")){F=parseFloat(document.getElementById("total_amount").value)}}}}}h("getTotalAmount: "+F);returnF}functionB(){varF=0;if(document.getElementById("totalTaxAmount")===null){returnF}if(document.getElementById("totalTaxAmount").textContent.length===0){F=document.getElementById("total_amount").value}else{F=document.getElementById("totalTaxAmount").textContent.split("").pop()}returnF}functionj(){varF=false;if(C()){if(e('input[id$="contribution-installments"]').length!==0&&e('input[id$="contribution-installments"]').val()>1){F=true}}if(document.getElementById("is_recur")!==null){if(document.getElementById("is_recur").type=="hidden"){F=(document.getElementById("is_recur").value==1)}else{F=Boolean(document.getElementById("is_recur").checked)}}else{if(e('input[name="auto_renew"]').length!==0){if(e('input[name="auto_renew"]').prop("checked")){F=true}else{if(document.getElementById("auto_renew").type=="hidden"){F=(document.getElementById("auto_renew").value==1)}else{F=Boolean(document.getElementById("auto_renew").checked)}}}}h("isRecur is "+F);returnF}functionm(G){if(G.empty){e("div#card-errors").hide()}else{if(G.error){k(G.error)}else{if(G.complete){e("div#card-errors").hide();varF=document.getElementById("billing_postal_code-"+CRM.vars.stripe.billingAddressID);if(F){F.value=G.value.postalCode}}}}}functionr(){cividiscountElements=c.querySelectorAll("input#discountcode");varF=function(G){if(G.code==="Enter"){G.preventDefault();h("adding submitdontprocess");c.dataset.submitdontprocess=true}};for(i=0;i<cividiscountElements.length;++i){cividiscountElements[i].addEventListener("keydown",F)}}functionf(){e("div.label span.crm-marker").each(function(){e(this).closest("div").next("div").find("input").addClass("required")});varF=e(c).validate();F.settings.errorClass="crm-inline-error alert-danger";F.settings.ignore=".select2-offscreen, [readonly], :hidden:not(.crm-select2)"}functiona(){if((document.getElementById("additional_participants")!==null)&&(document.getElementById("additional_participants").value.length!==0)){h("We don't know the final price - registering additional participants");returntrue}returnfalse}functionx(G){varF=null;if(document.getElementById("action")!==null){F=document.getElementById("action")}else{F=document.createElement("input")}F.setAttribute("type","hidden");F.setAttribute("name","op");F.setAttribute("id","action");F.setAttribute("value",G);c.appendChild(F)}functiono(){if((typeofc==="undefined")||(!c)){c=n();if(!c){returnnull}}varF=c.querySelector('input[name="payment_processor_id"]:checked');if(F!==null){returnparseInt(F.value)}returnnull}functionh(F){if((typeof(CRM.vars.stripe)==="undefined")||(Boolean(CRM.vars.stripe.jsDebug)===true)){console.log(newDate().toISOString()+" civicrm_stripe.js: "+F)}}functiond(F){h("Firing Event: "+F);e(c).trigger(F)}functionl(F,H,G){if(typeofSwal==="function"){Swal.fire({icon:F,title:H,text:G})}}});