1jQuery( ( $ ) => { 2 const { __, _x, _n, _nx } = wp.i18n 3 4 /** 5 * Validate On place order event 6 */ 7 let on_place_order = ( e ) => { 8 // e.preventDefault(); 9 10 if ( $( '#wc-ptranz_payment_bac_two-cc-form' ).length ) { 11 let payment_method = $( 'input[name="payment_method"]' ) 12 13 if ( ( payment_method.length && $( 'input[name="payment_method"]:checked' ).val() == pwtz_bac_two_params.gateway_id ) || 14 ( $( '#payment_method_ptranz_payment_bac_two' ).length && $( '#payment_method_ptranz_payment_bac_two' ).prop( 'checked' ) ) 15 ) { 16 let cc_fields = get_card_form_fields(), 17 is_valid_form = validate_cc_form_fields( cc_fields ) 18 19 return is_valid_form 20 } 21 } 22 } 23 $( document ).on( 'click', '#place_order', on_place_order ) 24 25 let on_cc_number_change = ( e ) => { 26 let _this = $( e.target ), 27 card_brand = $.payment.cardType( _this.val() ) 28 29 use_bin_checker( _this.val() ) 30 $( '#ptranz_payment_bac_two-card-brand' ).val( card_brand ) 31 32 if ( card_brand == 'amex' ) { 33 $( '#ptranz_payment_bac_two-card-cvc' ).attr( 'maxlength', 4 ) 34 } else { 35 $( '#ptranz_payment_bac_two-card-cvc' ).attr( 'maxlength', 3 ) 36 } 37 } 38 $( document ).on( 'change', '#ptranz_payment_bac_two-card-number', on_cc_number_change ) 39 40 /** 41 * Validate each CC form field 42 * 43 * @param {Object} inputs 44 * @returns 45 */ 46 let validate_cc_form_fields = ( inputs ) => { 47 let errors = 0, 48 error_classes = 'validate-required woocommerce-invalid woocommerce-invalid-required-field', 49 card_expiry = inputs.card_expiry.val(), 50 error_messages = [], 51 card_brand = $.payment.cardType( inputs.card_number.val() ), 52 response = { 53 cardBrand: card_brand, 54 expirationMonth: card_expiry.substring(0, 2), 55 expirationYear: card_expiry.slice(-2) 56 } 57 58 $.each( inputs, ( key, value ) => { 59 if ( value.val() == '' ) { 60 value.closest( '.form-row' ).addClass( error_classes ); 61 errors++; 62 } else if ( key == 'card_holder' && value.val().length > 45 ) { 63 value.closest( '.form-row' ).addClass( error_classes ) 64 error_messages.push( __( 'El nombre del tarjetahabiente excede el máximo permitido', 'pwtz-payment-gateway' ) ) 65 errors++ 66 } else { 67 value.closest( '.form-row' ).removeClass( error_classes ) 68 } 69 } ) 70 71 if ( errors == 0 && $.payment != undefined ) { 72 if ( ! $.payment.validateCardNumber( inputs.card_number.val() ) ) { 73 errors++ 74 error_messages.push( __( 'Formato de tarjeta inválido', 'pwtz-payment-gateway' ) ) 75 inputs.card_number.closest( '.form-row' ).addClass( error_classes ) 76 } 77 78 if ( ! $.payment.validateCardExpiry( response.expirationMonth, response.expirationYear ) ) { 79 errors++ 80 error_messages.push( __( 'Formato inválido de fecha de expiración', 'pwtz-payment-gateway' ) ) 81 inputs.card_expiry.closest( '.form-row' ).addClass( error_classes ) 82 } 83 84 if ( ! $.payment.validateCardCVC( inputs.card_cvc.val() ) ) { 85 errors++ 86 error_messages.push( __( 'Formato inválido de código de seguridad (CVC)', 'pwtz-payment-gateway' ) ) 87 inputs.card_cvc.closest( '.form-row' ).addClass( error_classes ) 88 } 89 90 if ( pwtz_params.accepted_cc.indexOf( card_brand ) < 0 ) { 91 errors++ 92 error_messages.push( __( 'La tarjeta ingresada no es permitida', 'pwtz-payment-gateway' ) ) 93 inputs.card_number.closest( '.form-row' ).addClass( error_classes ) 94 } 95 } else { 96 error_messages.push( __( 'Por favor revise los campos requeridos', 'pwtz-payment-gateway' ) ) 97 } 98 99 if ( errors > 0 ) { 100 show_error_notices( error_messages ) 101 102 return false;
103 } 104 105 return response 106 } 107 108 /** 109 * Wrap all error messages 110 * @param {Object} error_messages 111 */ 112 let show_error_notices = ( error_messages ) => { 113 let wc_notice = $( '.payment_box.payment_method_ptranz_payment_bac_two' ), 114 error_wrapper = wc_notice.find( '.wc-pwtz-error-wrapper' ) 115 error_wrapper.html( '<div class="woocommerce-error wc-pwtz-error"><ul><li>' + error_messages.join('</li><li>' ) + '</li></ul></div>' ); 116 scroll_to_error( error_wrapper ); 117 } 118 119 /** 120 * Get all CC form fields 121 * 122 * @returns {object} 123 */ 124 let get_card_form_fields = () => { 125 let form_wrapper = $( '#wc-ptranz_payment_bac_two-cc-form' ) 126 127 return { 128 card_holder: form_wrapper.find( "#ptranz_payment_bac_two-card-holder" ), 129 card_number: form_wrapper.find( "#ptranz_payment_bac_two-card-number" ), 130 card_expiry: form_wrapper.find( "#ptranz_payment_bac_two-card-expiry" ), 131 card_cvc: form_wrapper.find( "#ptranz_payment_bac_two-card-cvc" ) 132 } 133 } 134 135 /** 136 * Returns the selected payment method HTML element. 137 * 138 * @return {HTMLElement} 139 */ 140 let scroll_to_error = ( target_obj ) => { 141 var offset = 15, 142 scrollPosition = 0 143 144 if ( $ ('#wpadminbar').length ) { 145 offset += $( '#wpadminbar' ).outerHeight() 146 } 147 148 if ( target_obj.length ) { 149 scrollPosition = target_obj.offset().top - offset 150 $( 'html, body' ).animate( { 151 scrollTop: scrollPosition 152 }, 1000, 'swing' ) 153 } 154 } 155 156 /** 157 * @version 1.0.9 158 * 159 * @param string cc_number 160 */ 161 let use_bin_checker = async ( cc_number ) => { 162 const bank_name_el = $( '#ptranz_payment_bac_two-bank-name' ), 163 bank_country_el = $( '#ptranz_payment_bac_two-bank-country' ) 164 165 if ( 166 bank_name_el.length 167 && bank_country_el.length 168 && pwtz_params.bin_checker_api 169 && cc_number !== '' 170 ) { 171 let cc_clean = cc_number.split(' ').join('') 172 bin_check = cc_clean.slice( 0, 6 ) 173 174 let request_params = { 175 method: 'GET', 176 redirect: 'follow', 177 headers: { 178 'apikey': pwtz_params.bin_checker_api 179 } 180 } 181 182 await fetch( `https://api.apilayer.com/bincheck/${bin_check}`, request_params ) 183 .then( response => response.text() ) 184 .then( result => { 185 let data = JSON.parse( result ), 186 bank_name = 'N/D', 187 bank_country = 'N/D' 188 189 if ( 'undefined' !== typeof data.bank_name && null !== data.bank_name ) { 190 bank_name = data.bank_name 191 } else { 192 console.log( result ) 193 } 194 195 if ( 'undefined' !== typeof data.country && null !== data.country ) { 196 bank_country = data.country 197 } 198 199 bank_name_el.val( bank_name ) 200 bank_country_el.val( bank_country ) 201 } ) 202 .catch( error => { 203 console.log( 'error', error ) 204 } ) 205 } 206 } 207} )
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.