1/** 2 * Prestaworks AB 3 * 4 * NOTICE OF LICENSE 5 * 6 * This source file is subject to the End User License Agreement(EULA) 7 * that is bundled with this package in the file LICENSE.txt. 8 * It is also available through the world-wide-web at this URL: 9 * http://license.prestaworks.se/license.html 10 * 11 * @author Prestaworks AB <[email protected]> 12 * @copyright Copyright Prestaworks AB (https://www.prestaworks.se/) 13 * @license http://license.prestaworks.se/license.html 14 */ 15 16/* Loading widget */ 17function initiateKlarnaWidget(client_token, container, payment_method_category) { 18 try { 19 Klarna.Payments.init({client_token}); 20 21 Klarna.Payments.load({ 22 container, 23 payment_method_category 24 }, 25 JSON.parse(kp_payload), 26 function (res) { 27 // 28 } 29 ); 30 } catch (err) { 31 console.log(err); 32 } 33} 34 35 36/* Authorize order through Klarna */ 37function makeAuthorizeCall(payment_method_category) { 38 try { 39 Klarna.Payments.authorize({ 40 payment_method_category 41 }, 42 getLatestOrderDetails(payment_method_category), 43 function (res) { 44 $('<div class="loading"></div>').appendTo('body'); //Loading icon overlay 45 $('.klarnapayments-buy-button').removeClass('is-processing'); 46 47 if (res.approved) { 48 postAuthToken(res); 49 } else if (!res.approved) { 50 $('.loading').remove(); 51 } else if (res.show_form && res.error) { 52 displayError(error_authorize, true); 53 } else if (res.show_form && !res.error) { 54 displayError(error_cancel, false); // Kunden klickar bort/stänger ner widget modalen 55 } else if (!res.show_form) { 56 displayError(error_offer, false); 57 } else { 58 displayError(error_unexpected, true); 59 } 60 61 $('.klarnapayments-buy-button').prop("disabled", false); 62 } 63 ); 64 } catch (err) { 65 console.log(err); 66 } 67} 68 69 70/* Setting error messages */ 71function displayError(message, refreshButton) { 72 $('.klarnapayments-buy-button').prop("disabled", false); 73 $('.loading').remove(); 74 $('.klarnapayments-buy-button').removeClass('is-processing'); 75 $('.klarna_errorbox').fadeIn(); 76 $('.error-message').text(message); 77 78 if (refreshButton) $('.klarna_errorbox').append('<button onclick="location.reload();">Refresh page</button>'); 79} 80 81 82/* Handling the displayment of selected Klarna payment method */ 83var initiate_klarna_widget = false; 84function handleOnClick(event, widget_container) { 85 event.preventDefault(); 86 87 d = document.createElement('div'); 88 d.innerHTML = ''; 89 d.className = 'klarna_dialog loading'; 90 document.body.appendChild(d); 91 d.addEventListener('click', (e) => { 92 e.currentTarget.remove(); 93 $('.klarna_dialog.open').fadeOut(); 94 }); 95 96 if (typeof widget_container == 'undefined') { 97 widget_container = $('#' + event.target.id).data('container'); 98 } 99 100 let payment_category = widget_container.substring(26, widget_container.length); 101 102 initiateKlarnaWidget(kp_client_token, '#' + widget_container, payment_category); 103 initiate_klarna_widget = true; 104 105 // Hides all klarna options that is not the "selected" one 106 let klarna_options = $('.klarna-container-element'); 107 for (let i = 0; i < klarna_options.length; i++) { 108 if (klarna_options[i].id !== widget_container) { 109 $('#' + klarna_options[i].id).hide(); 110 } 111 } 112 113 $('#' + widget_container).addClass('klarna_dialog').addClass('open') 114 $('#' + widget_container).fadeIn(); 115} 116 117 118/* Disables submit button, adding loading icon and runs the klarna auth() */ 119function handleClickAuthorize(event) { 120 event.preventDefault(); 121 event.stopPropagation(); 122 123 let category = event.target.getAttribute('data-category'); 124 $('.klarnapayments-buy-button').prop("disabled", true); 125 $('.klarnapayments-buy-button').addClass('is-processing'); 126 $('.klarna_dialog.open').fadeOut(); 127 makeAuthorizeCall(category); 128} 129 130function handleOnClickOrAuthorize(event, widget_container) { 131 event.preventDefault(); 132 event.stopPropagation(); 133 134 if ('' == event.target.id) { 135 return handleOnClick(event, widget_container); 136 } 137 if (!initiate_klarna_widget) { 138 handleOnClick(event, widget_container) 139 } else { 140 d = document.createElement('div'); 141 d.innerHTML = ''; 142 d.className = 'klarna_dialog loading'; 143 document.body.appendChild(d); 144 d.addEventListener('click', (e) => { 145 e.currentTarget.remove(); 146 }); 147 148 handleClickAuthorize(event) 149 } 150} 151
152/* Posts auth token to server to place an order */ 153function postAuthToken(res) { 154 $.ajax({ 155 type: 'POST', 156 url: kp_ajax_url, 157 dataType: 'text', 158 data: { 159 action: 'validate', 160 authorization_token: res.authorization_token, 161 token: kp_secure_key 162 }, 163 success: function (response) { 164 let parsedResponse = JSON.parse(response); 165 if (parsedResponse.statusCode === 200) window.location.replace(parsedResponse.redirect_url); 166 else if (parsedResponse.statusCode >= 401) displayError(parsedResponse.message, true); 167 else if (parsedResponse.statusCode >= 400) displayError(parsedResponse.message, false); 168 else displayError(parsedResponse.message, true); 169 }, 170 error: function (err) { 171 displayError(err, true); 172 } 173 }); 174} 175 176 177function getLatestOrderDetails(dataModuleName) { 178 let result; 179 $.ajax({ 180 type: 'POST', 181 url: kp_ajax_url, 182 dataType: 'text', 183 data: { 184 action: 'order_details', 185 token: kp_secure_key, 186 paymentOption: dataModuleName 187 }, 188 async: false, 189 success: function (response) { 190 result = JSON.parse(response); 191 }, 192 error: function (err) { 193 displayError(err, true); 194 } 195 }); 196 197 return result; 198}
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.