| 1 |
/* |
| 2 |
|
| 3 |
This file is part of the plugin Pay with Vipps and MobilePay for WooCommerce |
| 4 |
Copyright (c) 2019 WP-Hosting AS |
| 5 |
|
| 6 |
MIT License |
| 7 |
|
| 8 |
Copyright (c) 2019 WP-Hosting AS |
| 9 |
|
| 10 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 |
of this software and associated documentation files (the "Software"), to deal |
| 12 |
in the Software without restriction, including without limitation the rights |
| 13 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 |
copies of the Software, and to permit persons to whom the Software is |
| 15 |
furnished to do so, subject to the following conditions: |
| 16 |
|
| 17 |
The above copyright notice and this permission notice shall be included in all |
| 18 |
copies or substantial portions of the Software. |
| 19 |
|
| 20 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 |
SOFTWARE. |
| 27 |
*/ |
| 28 |
|
| 29 |
jQuery(document).ready(function () { |
| 30 |
var validating = 0; |
| 31 |
|
| 32 |
function validateExpressCheckout () { |
| 33 |
var termscheck = validateTermsAndConditions(); |
| 34 |
var validation = termscheck; |
| 35 |
if (typeof wp !== 'undefined' && typeof wp.hooks !== 'undefined') { |
| 36 |
validation = wp.hooks.applyFilters('vippsValidateExpressCheckoutForm', validation); |
| 37 |
} |
| 38 |
return validation; |
| 39 |
} |
| 40 |
|
| 41 |
function validateTermsAndConditions () { |
| 42 |
var termsbox = jQuery('.input-checkbox[name="terms"]'); |
| 43 |
if (termsbox.length == 0) return true; |
| 44 |
if (termsbox.is(':checked')) return true; |
| 45 |
termsbox.closest('.validate-required').addClass('woocommerce-invalid'); |
| 46 |
termsbox.closest('.validate-required').addClass('woocommerce-invalid-required-field '); |
| 47 |
var message= VippsCheckoutMessages['termsAndConditionsError']; |
| 48 |
addErrorMessage(message, jQuery('#vippsdata')); |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
// Remove old error messages |
| 53 |
function removeErrorMessages () { |
| 54 |
jQuery('.woocommerce-error.vipps-error').fadeOut(300, function () { jQuery(this).remove(); }); |
| 55 |
jQuery(document).trigger('woo-vipps-remove-errors'); |
| 56 |
if (typeof wp !== 'undefined' && typeof wp.hooks !== 'undefined') { |
| 57 |
wp.hooks.doAction('vippsRemoveErrorMessages'); |
| 58 |
} |
| 59 |
validating=0; |
| 60 |
jQuery('.woocommerce-invalid woocommerce-invalid-required-field').removeClass('woocommerce-invalid woocommerce-invalid-required-field'); |
| 61 |
jQuery('.woocommerce-invalid').removeClass('woocommerce-invalid'); |
| 62 |
jQuery('.woocommerce-invalid-required-field').removeClass('woocommerce-invalid-required-field'); |
| 63 |
} |
| 64 |
|
| 65 |
// And add new ones |
| 66 |
function addErrorMessage(msg,element) { |
| 67 |
// Allow developers to customize error message by hiding vipps-default-error-message and hooking woo-vipps-error-message <messsage>,<element> |
| 68 |
var msg = "<p><ul class='woocommerce-error vipps-error vipps-default-error-message vipps-buy-now-error'><li>"+msg+"!</li></ul></p>"; |
| 69 |
if (typeof wp !== 'undefined' && typeof wp.hooks !== 'undefined') { |
| 70 |
msg = wp.hooks.applyFilters('vippsErrorMessage',msg, element); |
| 71 |
} |
| 72 |
jQuery(document).trigger('woo-vipps-error-message',[msg, element]); |
| 73 |
if (typeof wp !== 'undefined' && typeof wp.hooks !== 'undefined') { |
| 74 |
wp.hooks.doAction('vippsAddErrorMessage', msg, element); |
| 75 |
} |
| 76 |
|
| 77 |
jQuery(msg).hide().insertAfter(element).fadeIn(300); |
| 78 |
jQuery('.woocommerce-error.vipps-error').click(removeErrorMessages); |
| 79 |
} |
| 80 |
|
| 81 |
// Show a success message, allow overriding using hooks |
| 82 |
function vippsSuccess() { |
| 83 |
var msgcontent = VippsCheckoutMessages['successMessage']; |
| 84 |
jQuery('.vipps-express-checkout').blur(); |
| 85 |
jQuery('#do-express-checkout').hide(); |
| 86 |
|
| 87 |
if (typeof wp !== 'undefined' && typeof wp.hooks !== 'undefined' && wp.hooks.hasAction('vippsSuccessMessage')) { |
| 88 |
wp.hooks.doAction('vippsSuccessMessage', msgcontent); |
| 89 |
return; |
| 90 |
} |
| 91 |
jQuery('#vipps-status-message').empty(); |
| 92 |
var msg = jQuery('<div id=\'success\' class="woocommerce-info">'+ msgcontent +'</div>'); |
| 93 |
jQuery('#vipps-status-message').append(msg); |
| 94 |
jQuery('.vipps-express-checkout').blur(); |
| 95 |
} |
| 96 |
// The default no-info "temporarily unavailable" message |
| 97 |
function vippsError() { |
| 98 |
jQuery('#do-express-checkout').hide(); |
| 99 |
jQuery('.vipps-express-checkout').blur(); |
| 100 |
var msgcontent = VippsCheckoutMessages['temporaryError']; |
| 101 |
|
| 102 |
if (typeof wp !== 'undefined' && typeof wp.hooks !== 'undefined' && wp.hooks.hasAction('vippsSuccessMessage')) { |
| 103 |
wp.hooks.doAction('vippsErrorMessage', msgcontent); |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
jQuery('#vipps-status-message').empty(); |
| 108 |
var msg = jQuery('<div id=\'error\' class="woocommerce-message woocommerce-error">'+ msgcontent +'</div>'); |
| 109 |
jQuery('#vipps-status-message').append(msg); |
| 110 |
} |
| 111 |
// An actual failure message from the backend |
| 112 |
function vippsFailure(msgcontent) { |
| 113 |
jQuery('#do-express-checkout').hide(); |
| 114 |
jQuery('.vipps-express-checkout').blur(); |
| 115 |
|
| 116 |
if (typeof wp !== 'undefined' && typeof wp.hooks !== 'undefined' && wp.hooks.hasAction('vippsFailureMessage')) { |
| 117 |
wp.hooks.doAction('vippsFailureMessage', msgcontent); |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
jQuery('#vipps-status-message').empty(); |
| 122 |
var msg = jQuery('<div id=\'failure\' class="woocommerce-message woocommerce-error">'+ msgcontent +'</div>'); |
| 123 |
jQuery('#vipps-status-message').append(msg); |
| 124 |
} |
| 125 |
|
| 126 |
function doExpressCheckout (e) { |
| 127 |
if (e) e.preventDefault(); |
| 128 |
if (validating) return false; |
| 129 |
validating=true; |
| 130 |
removeErrorMessages(); |
| 131 |
var ok = validateExpressCheckout(); |
| 132 |
if (!ok) { |
| 133 |
jQuery('.vipps-express-checkout').blur(); |
| 134 |
validating=false; |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
// Required for the order attribution thing IOK 2024-01-16 |
| 139 |
if (jQuery('#vippsorderattribution').val() == 1) { |
| 140 |
try { |
| 141 |
console.log("Triggering init_checkout"); |
| 142 |
jQuery(document.body).trigger( 'init_checkout' ); |
| 143 |
} catch (error) { |
| 144 |
console.log("Could not trigger init-checkout: %j", error); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
jQuery('#do-express-checkout').prop('disabled',true); |
| 149 |
jQuery('#do-express-checkout').prop('inactive',true); |
| 150 |
jQuery('body').addClass('processing'); |
| 151 |
|
| 152 |
var data = jQuery("#vippsdata").serialize(); |
| 153 |
jQuery.ajax(VippsConfig['vippsajaxurl'], { |
| 154 |
"method": "POST", |
| 155 |
"data":data, |
| 156 |
"cache":false, |
| 157 |
"headers": {"Accept-Language": `${VippsConfig['vippslocale']}, *`}, |
| 158 |
"dataType": "json", |
| 159 |
"error": function (xhr, statustext, error) { |
| 160 |
console.log("Error creating express checkout:" + statustext + " " + error); |
| 161 |
vippsError(); |
| 162 |
jQuery('body').removeClass('processing'); |
| 163 |
validating=0; |
| 164 |
}, |
| 165 |
"success": function (result, statustext, xhr) { |
| 166 |
if (result["ok"]) { |
| 167 |
vippsSuccess(); |
| 168 |
validating=0; |
| 169 |
window.location.replace(result["url"]); |
| 170 |
} else { |
| 171 |
console.log("Failure!"); |
| 172 |
jQuery('#do-express-checkout').hide(); |
| 173 |
vippsFailure(result['msg']); |
| 174 |
jQuery('body').removeClass('processing'); |
| 175 |
jQuery('.vipps-express-checkout').blur(); |
| 176 |
validating=0; |
| 177 |
} |
| 178 |
}, |
| 179 |
"timeout": 0 |
| 180 |
}); |
| 181 |
} |
| 182 |
|
| 183 |
// No interaction neccessary => just buy stuff |
| 184 |
var buttons = jQuery('#do-express-checkout'); |
| 185 |
if (buttons.length == 0) { |
| 186 |
// Give other scripts a chance to run |
| 187 |
setTimeout(doExpressCheckout, 100); |
| 188 |
// doExpressCheckout(); |
| 189 |
} else { |
| 190 |
buttons.click(doExpressCheckout); |
| 191 |
jQuery('input').change(removeErrorMessages); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
}); |
| 197 |
|