1var methodsInstance = {}; 2 3jQuery(function ($) { 4 if (isAddPaymentPage()) { 5 var checkout_form = $('#add_payment_method'); 6 } else { 7 var checkout_form = $('form.checkout'); 8 } 9 var hipaySDK = {}; 10 11 var useOneClick = Boolean(hipay_config.useOneClick); 12 13 function destroy() { 14 for (var method in methodsInstance) { 15 methodsInstance[method].destroy(); 16 delete methodsInstance[method]; 17 } 18 19 $(document.body).off('click', '#place_order', submitOrder); 20 21 checkout_form.off( 22 'change', 23 'input[name="payment_method"]', 24 addPaymentMethod 25 ); 26 } 27 28 function init() { 29 var defaultMethod = getSelectedMethod(); 30 31 hipaySDK = HiPay({ 32 username: hipay_config.apiUsernameTokenJs, 33 password: hipay_config.apiPasswordTokenJs, 34 environment: hipay_config.environment, 35 lang: hipay_config.lang 36 }); 37 38 hipaySDK.injectBaseStylesheet(); 39 40 if (containerExist()) { 41 createHostedFieldsInstance(defaultMethod); 42 } 43 } 44 45 function addPaymentMethod() { 46 var method = getSelectedMethod(); 47 48 if (methodsInstance[method] === undefined) { 49 createHostedFieldsInstance(method); 50 } 51 } 52 53 function isHostedFields() { 54 return hipay_config_card.operating_mode === 'hosted_fields'; 55 } 56 function isOneClick() { 57 return ( 58 $( 59 'input[name="wc-hipayenterprise_credit_card-payment-token"]:checked' 60 ).val() !== undefined && 61 $( 62 'input[name="wc-hipayenterprise_credit_card-payment-token"]:checked' 63 ).val() !== 'new' 64 ); 65 } 66 function checkPayment() { 67 if (isOneClick()) { 68 injectInput( 69 $('#hipay-token-payment-data'), 70 'browser_info', 71 hipaySDK.getBrowserInfo(), 72 'card' 73 ); 74 injectInput( 75 $('#hipay-token-payment-data'), 76 'device_fingerprint', 77 hipaySDK.getDeviceFingerprint(), 78 'card' 79 ); 80 processPayment(); 81 } else { 82 processPayment(); 83 } 84 } 85 86 function submitOrder(e) { 87 if (isHiPayMethod()) { 88 e.preventDefault(); 89 e.stopPropagation(); 90 91 if ( 92 isCreditCardSelected() && 93 !isAddPaymentPage() && 94 (!isHostedFields() || isOneClick()) 95 ) { 96 checkPayment(); 97 } else { 98 var method = getSelectedMethod(); 99 100 if (isCreditCardSelected()) { 101 method = 'card'; 102 } 103 104 getPaymentData(method); 105 } 106 } 107 } 108 109 function processPayment() { 110 checkout_form.submit(); 111 } 112 113 function applyPaymentData(response, method) { 114 var methodForm = $('#' + methodsInstance[method].options.selector); 115 for (var key in response) { 116 injectInput(methodForm, key, response[key], method); 117 } 118 } 119 120 function injectInput(form, key, data, method) { 121 if (form) { 122 var valueResponse = data instanceof Object ? JSON.stringify(data) : data; 123 form.append( 124 $('<input>') 125 .attr('type', 'hidden') 126 .attr('name', method + '-' + key) 127 .val(valueResponse) 128 ); 129 } 130 } 131 132 function getPaymentData(method) { 133 hideErrorDiv(method); 134 methodsInstance[method].getPaymentData().then( 135 function (response) { 136 if (isCreditCardSelected() && !isCardTypeActivated(response)) { 137 fillErrorDiv(hipay_config_i18n.activated_card_error, method); 138 } else { 139 applyPaymentData(response, method); 140 processPayment(); 141 } 142 }, 143 function (error) { 144 handleError(error, method); 145 } 146 ); 147 } 148 149 function handleError(errors, method) { 150 for (var error in errors) { 151 var domElement = document.querySelector( 152 "[data-hipay-id='hipay-" + 153 method + 154 '-field-error-' + 155 errors[error].field + 156 "']" 157 ); 158 159 // If DOM element add error inside 160 if (domElement) { 161 domElement.innerText = errors[error].error; 162 } 163 } 164 } 165 166 function fillErrorDiv(error, method) { 167 $('#error-js-' + method).show(); 168 $('#error-js-' + method).html(error); 169 } 170 171 function hideErrorDiv(method) { 172 $('#error-js-' + method).hide(); 173 $('#error-js-' + method).html(''); 174 } 175 176 function isHiPayMethod() { 177 return ( 178 $('input[name="payment_method"]:checked') 179 .val() 180 .indexOf('hipayenterprise_') !== -1 181 ); 182 } 183 184 function isAddPaymentPage() { 185 return $('#add_payment_method').length; 186 } 187 188 function createHostedFieldsInstance(method) { 189 if (isPayPalV2()) { 190 return false;
191 } 192 193 if ( 194 !isHiPayMethod() || 195 (isCreditCardSelected() && !isHostedFields() && !isAddPaymentPage()) 196 ) { 197 return true; 198 } 199 200 var configHostedFields = {}; 201 202 if (isCreditCardSelected()) { 203 method = 'card'; 204 configHostedFields = getCardConfig(); 205 } else { 206 configHostedFields['template'] = 'auto'; 207 } 208 209 if (methodsInstance[method] !== undefined) { 210 return methodsInstance[method]; 211 } 212 213 blockUI(); 214 configHostedFields['selector'] = 'hipayHF-container-' + method; 215 configHostedFields['styles'] = { 216 base: { 217 fontFamily: hipay_config.fontFamily, 218 color: hipay_config.color, 219 fontSize: hipay_config.fontSize, 220 fontWeight: hipay_config.fontWeight, 221 placeholderColor: hipay_config.placeholderColor, 222 caretColor: hipay_config.caretColor, 223 iconColor: hipay_config.iconColor 224 }, 225 components: { 226 switch: { 227 mainColor: hipay_config.useOneClick.switch_color 228 }, 229 checkbox: { 230 mainColor: hipay_config.useOneClick.checkbox_color 231 } 232 } 233 }; 234 235 methodsInstance[method] = hipaySDK.create(method, configHostedFields); 236 237 if (isCreditCardSelected() && isHostedFields()) { 238 const cardForm = document.getElementById('hipayHF-card-form-container'); 239 if (!cardForm) { 240 console.error('Card form container not found'); 241 return; 242 } 243 244 if (useOneClick) { 245 methodsInstance[method].on('ready', function () { 246 const savedCards = getSavedCustomerCards(); 247 const payOtherCardButton = document.getElementById('pay-other-card'); 248 249 if (savedCards.length > 0 && payOtherCardButton) { 250 payOtherCardButton.addEventListener('click', () => { 251 cardForm.classList.toggle('hidden'); 252 }); 253 } else { 254 cardForm.classList.remove('hidden'); 255 } 256 257 const savedCardsElements = 258 document.getElementsByClassName('saved-card'); 259 if (savedCardsElements.length > 0) { 260 cardForm.classList.add('hidden'); 261 Array.from(savedCardsElements).forEach((card) => { 262 card.addEventListener('click', () => { 263 cardForm.classList.add('hidden'); 264 }); 265 }); 266 } 267 }); 268 } else { 269 cardForm.classList.remove('hidden'); 270 } 271 } 272 273 methodsInstance[method].on('blur', function (data) { 274 // Get error container 275 var domElement = document.querySelector( 276 "[data-hipay-id='hipay-" + 277 method + 278 '-field-error-' + 279 data.element + 280 "']" 281 ); 282 283 // Finish function if no error DOM element 284 if (!domElement) { 285 return; 286 } 287 288 // If not valid & not empty add error 289 if (!data.validity.valid && !data.validity.empty) { 290 domElement.innerText = data.validity.error; 291 } else { 292 domElement.innerText = ''; 293 } 294 }); 295 296 methodsInstance[method].on('inputChange', function (data) { 297 // Get error container 298 var domElement = document.querySelector( 299 "[data-hipay-id='hipay-" + 300 method + 301 '-field-error-' + 302 data.element + 303 "']" 304 ); 305 306 // Finish function if no error DOM element 307 if (!domElement) { 308 return; 309 } 310 311 // If not valid & not potentiallyValid add error (input is focused) 312 if (!data.validity.valid && !data.validity.potentiallyValid) { 313 domElement.innerText = data.validity.error; 314 } else { 315 domElement.innerText = ''; 316 } 317 }); 318 319 methodsInstance[method].on('helpButtonToggled', function (data) { 320 // Get error container 321 var domElement = document.querySelector( 322 "[data-hipay-id='hipay-help-" + data.element + "']" 323 ); 324 325 // Finish function if no error DOM element 326 if (!domElement) { 327 return; 328 } 329 330 if (domElement) { 331 // Toggle visible class 332 domElement.classList.toggle('hipay-visible'); 333 if (domElement.innerHTML.trim()) { 334 domElement.innerHTML = ''; 335 } else { 336 domElement.innerHTML = data.message; 337 } 338 } 339 }); 340 341 methodsInstance[method].on('ready', function () { 342 unBlockUI(); 343 }); 344 } 345 var isCardsDisplayedAreLimited = Boolean( 346 hipay_config?.useOneClick?.card_count && 347 Number(hipay_config.useOneClick.card_count) > 0 348 ); 349 function getCardConfig() { 350 var firstName = $('#billing_first_name').val(); 351 var lastName = $('#billing_last_name').val(); 352 353 return { 354 brand: hipay_config_current_cart.activatedCreditCard, 355 one_click: { 356 enabled: useOneClick, 357 ...(isCardsDisplayedAreLimited && { 358 cards_display_count: Number(hipay_config.useOneClick.card_count) 359 }), 360 cards: getSavedCustomerCards() 361 }, 362 fields: { 363 savedCards: { 364 selector: 'hipay-card-saved-cards' 365 }, 366 cardHolder: { 367 selector: 'hipay-card-field-cardHolder', 368 defaultFirstname: firstName, 369 defaultLastname: lastName 370 }, 371 cardNumber: { 372 selector: 'hipay-card-field-cardNumber' 373 }, 374 expiryDate: { 375 selector: 'hipay-card-field-expiryDate' 376 }, 377 cvc: { 378 selector: 'hipay-card-field-cvc', 379 helpButton: true 380 }, 381 savedCardButton: { 382 selector: 'hipay-saved-card-btn' 383 } 384 } 385 }; 386 } 387 388 function isCreditCardSelected() { 389 return getSelectedMethod() === 'credit-card'; 390 } 391 392 function getSelectedMethod() { 393 return $('input[name="payment_method"]:checked') 394 .val() 395 .replace('hipayenterprise_', '') 396 .replace(/_/g, '-'); 397 } 398 399 function blockUI() { 400 $( 401 '.woocommerce-checkout-payment, .woocommerce-checkout-review-order-table' 402 ).block({ 403 message: null, 404 overlayCSS: { 405 background: '#fff', 406 opacity: 0.6 407 } 408 }); 409 } 410 411 function unBlockUI() { 412 $( 413 '.woocommerce-checkout-payment, .woocommerce-checkout-review-order-table' 414 ).unblock(); 415 } 416 417 function containerExist() { 418 return $('.hipay-container-hosted-fields').length; 419 } 420 421 function isCardTypeActivated(result) { 422 return hipay_config_current_cart.activatedCreditCard.includes( 423 result.payment_product 424 ); 425 } 426 427 function getSavedCustomerCards() { 428 var savedCards = hipay_config_current_cart.savedCreditCards; 429 430 if (!savedCards || !savedCards.length) { 431 return []; 432 } 433 434 return savedCards.filter((card) => 435 hipay_config_current_cart.activatedCreditCard.includes(card.brand) 436 ); 437 } 438 439 function isPayPalV2() { 440 return paypal_version.v2 === '1' && getSelectedMethod() === 'paypal'; 441 } 442 443 $(document.body).on('updated_checkout', function () { 444 if ($('input[name="payment_method"]:checked').length) { 445 init(); 446 $(document.body).on('click', '#place_order', submitOrder); 447 checkout_form.on( 448 'change', 449 'input[name="payment_method"]', 450 addPaymentMethod 451 ); 452 } 453 }); 454 455 $(document.body).on('update_checkout', function () { 456 destroy(); 457 }); 458 459 $(document.body).on('init_add_payment_method', function () { 460 destroy(); 461 if (containerExist()) { 462 init(); 463 $(document.body).on('click', '#place_order', submitOrder); 464 } 465 }); 466 467 checkout_form.on( 468 'change', 469 '#billing_first_name, #billing_last_name', 470 function () { 471 if (containerExist()) { 472 $(document.body).trigger('update_checkout'); 473 } 474 } 475 ); 476});
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.