oont-contents/plugins/woocommerce-square/build/assets/frontend/wc-square-payment-gateway-payment-form.js
2025-02-08 15:10:23 +01:00

1 line
No EOL
7.4 KiB
JavaScript

(()=>{"use strict";(function(){const e=[].indexOf||function(e){for(let t=0,a=this.length;t<a;t++)if(t in this&&this[t]===e)return t;return-1};jQuery(document).ready((function(t){return window.Square_Payment_Form_Handler=function(){function a(e){if(this.id=e.id,this.id_dasherized=e.id_dasherized,this.plugin_id=e.plugin_id,this.type=e.type,this.csc_required=e.csc_required,this.csc_required_for_tokens=e.csc_required_for_tokens,this.enabled_card_types=e.enabled_card_types,t("form.checkout").length)this.form=t("form.checkout"),this.handle_checkout_page();else if(t("form#order_review").length)this.form=t("form#order_review"),this.handle_pay_page();else{if(!t("form#add_payment_method").length)return void console.log("No payment form found!");this.form=t("form#add_payment_method"),this.handle_add_payment_method_page()}var a;this.params=window.sv_wc_payment_gateway_payment_form_params,"echeck"===this.type&&this.form.on("click",".js-sv-wc-payment-gateway-echeck-form-check-hint, .js-sv-wc-payment-gateway-echeck-form-sample-check",(a=this,function(){return a.handle_sample_check_hint()})),t(document).trigger("sv_wc_payment_form_handler_init",{id:this.id,instance:this})}return a.prototype.handle_checkout_page=function(){var e;return"credit-card"===this.type&&t(document.body).on("updated_checkout",(e=this,function(){return e.format_credit_card_inputs()})),t(document.body).on("updated_checkout",function(e){return function(){return e.set_payment_fields()}}(this)),t(document.body).on("updated_checkout",function(e){return function(){return e.handle_saved_payment_methods()}}(this)),this.form.on("checkout_place_order_"+this.id,function(e){return function(){return e.validate_payment_data()}}(this))},a.prototype.handle_pay_page=function(){return this.set_payment_fields(),"credit-card"===this.type&&this.format_credit_card_inputs(),this.handle_saved_payment_methods(),this.form.submit((e=this,function(){if(t("#order_review input[name=payment_method]:checked").val()===e.id)return e.validate_payment_data()}));var e},a.prototype.handle_add_payment_method_page=function(){return this.set_payment_fields(),"credit-card"===this.type&&this.format_credit_card_inputs(),this.form.submit((e=this,function(){if(t("#add_payment_method input[name=payment_method]:checked").val()===e.id)return e.validate_payment_data()}));var e},a.prototype.set_payment_fields=function(){return this.payment_fields=t(".payment_method_"+this.id)},a.prototype.validate_payment_data=function(){let e,a;return!this.form.is(".processing")&&(this.saved_payment_method_selected=this.payment_fields.find(".js-sv-wc-payment-gateway-payment-token:checked").val(),a="credit-card"===this.type?this.validate_card_data():this.validate_account_data(),e=!1!==t(document.body).triggerHandler("sv_wc_payment_form_valid_payment_data",{payment_form:this,passed_validation:a}),a&&e)},a.prototype.format_credit_card_inputs=function(){return t(".js-sv-wc-payment-gateway-credit-card-form-account-number").payment("formatCardNumber").change(),t(".js-sv-wc-payment-gateway-credit-card-form-expiry").payment("formatCardExpiry").change(),t(".js-sv-wc-payment-gateway-credit-card-form-csc").payment("formatCardCVC").change(),t(".js-sv-wc-payment-gateway-credit-card-form-input").on("change paste keyup",(e=this,function(){return e.do_inline_credit_card_validation()}));var e},a.prototype.do_inline_credit_card_validation=function(){let a,r,n,i;return a=t(".js-sv-wc-payment-gateway-credit-card-form-account-number"),i=t(".js-sv-wc-payment-gateway-credit-card-form-expiry"),n=t(".js-sv-wc-payment-gateway-credit-card-form-csc"),r=t.payment.cardType(a.val()),e.call(this.enabled_card_types,r)<0?a.addClass("invalid-card-type"):a.removeClass("invalid-card-type"),t.payment.validateCardExpiry(i.payment("cardExpiryVal"))?i.addClass("identified"):i.removeClass("identified"),t.payment.validateCardCVC(n.val())?n.addClass("identified"):n.removeClass("identified")},a.prototype.validate_card_data=function(){let e,a,r,n;return r=[],a=this.payment_fields.find(".js-sv-wc-payment-gateway-credit-card-form-csc").val(),null!=a&&(a?(/\D/.test(a)&&r.push(this.params.cvv_digits_invalid),(a.length<3||a.length>4)&&r.push(this.params.cvv_length_invalid)):this.csc_required&&(this.saved_payment_method_selected&&!this.csc_required_for_tokens||r.push(this.params.cvv_missing))),this.saved_payment_method_selected||(e=this.payment_fields.find(".js-sv-wc-payment-gateway-credit-card-form-account-number").val(),n=t.payment.cardExpiryVal(this.payment_fields.find(".js-sv-wc-payment-gateway-credit-card-form-expiry").val()),e=e.replace(/-|\s/g,""),e?((e.length<12||e.length>19)&&r.push(this.params.card_number_length_invalid),/\D/.test(e)&&r.push(this.params.card_number_digits_invalid),t.payment.validateCardNumber(e)||r.push(this.params.card_number_invalid)):r.push(this.params.card_number_missing),t.payment.validateCardExpiry(n)||r.push(this.params.card_exp_date_invalid)),r.length>0?(this.render_errors(r),!1):(this.payment_fields.find(".js-sv-wc-payment-gateway-credit-card-form-account-number").val(e),!0)},a.prototype.validate_account_data=function(){let e,t,a;return!!this.saved_payment_method_selected||(t=[],a=this.payment_fields.find(".js-sv-wc-payment-gateway-echeck-form-routing-number").val(),e=this.payment_fields.find(".js-sv-wc-payment-gateway-echeck-form-account-number").val(),a?(9!==a.length&&t.push(this.params.routing_number_length_invalid),/\D/.test(a)&&t.push(this.params.routing_number_digits_invalid)):t.push(this.params.routing_number_missing),e?((e.length<3||e.length>17)&&t.push(this.params.account_number_length_invalid),/\D/.test(e)&&t.push(this.params.account_number_invalid)):t.push(this.params.account_number_missing),t.length>0?(this.render_errors(t),!1):(this.payment_fields.find(".js-sv-wc-payment-gateway-echeck-form-account-number").val(e),!0))},a.prototype.render_errors=function(e){return t(".woocommerce-error, .woocommerce-message").remove(),this.form.prepend('<ul class="woocommerce-error"><li>'+e.join("</li><li>")+"</li></ul>"),this.form.removeClass("processing").unblock(),this.form.find(".input-text, select").blur(),t("html, body").animate({scrollTop:this.form.offset().top-100},1e3)},a.prototype.handle_saved_payment_methods=function(){let e,a,r,n,i;if(i=this.id_dasherized,r=this.csc_required,n=this.csc_required_for_tokens,a=t("div.js-wc-"+i+"-new-payment-method-form"),e=a.find(".js-sv-wc-payment-gateway-credit-card-form-csc").closest(".form-row"),t("input.js-wc-"+this.id_dasherized+"-payment-token").change((function(){let r;if(r=t("input.js-wc-"+i+"-payment-token:checked").val(),r){if(a.slideUp(200),n)return e.removeClass("form-row-last").addClass("form-row-first"),a.after(e)}else if(a.slideDown(200),n)return e.removeClass("form-row-first").addClass("form-row-last"),a.find(".js-sv-wc-payment-gateway-credit-card-form-expiry").closest(".form-row").after(e)})).change(),t("input#createaccount").change((function(){let e;return e=t("input.js-wc-"+i+"-tokenize-payment-method").closest("p.form-row"),t(this).is(":checked")?(e.slideDown(),e.next().show()):(e.hide(),e.next().hide())})),!t("input#createaccount").is(":checked"))return t("input#createaccount").change()},a.prototype.handle_sample_check_hint=function(){let e;return e=this.payment_fields.find(".js-sv-wc-payment-gateway-echeck-form-sample-check"),e.is(":visible")?e.slideUp():e.slideDown()},a.prototype.block_ui=function(){return this.form.block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},a.prototype.unblock_ui=function(){return this.form.unblock()},a}()}))}).call(void 0)})();