1/** 2 * Pepper Module 3 * 4 * Copyright (c) 2020 Pepper 5 * 6 * @category Payment 7 * @author Pepper 8 * @copyright 2020, Pepper 9 * @link https://peppermoney.es/ 10 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 11 * 12 * Description: 13 * 14 * Plugin de Prestashop para conectar con la pasarela de Pepper. 15 * 16 * -- 17 * 18 * NOTICE OF LICENSE 19 * 20 * This source file is subject to the Open Software License (OSL 3.0) 21 * that is bundled with this package in the file LICENSE. 22 * It is also available through the world-wide-web at this URL: 23 * http://opensource.org/licenses/osl-3.0.php 24 * If you did not receive a copy of the license and are unable to 25 * obtain it through the world-wide-web, please send an email 26 * to [email protected] so we can send you a copy immediately. 27 */ 28function deleteManaged() { 29 document.cookie = 'managed_mode=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'; 30} 31//Apoyo para trabajo sessionStorage 32function setSessionStoragePepper(key, value) { 33 if(typeof(Storage) !== "undefined") { 34 sessionStorage.setItem(key, value); 35 } 36} 37function getSessionStoragePepper (key) { 38 if(typeof(Storage) !== "undefined") { 39 return sessionStorage.getItem(key); 40 } 41 else { 42 return null; 43 } 44} 45 46if (typeof PEPPER !== 'object') { 47 var PEPPER = {} 48} 49 50PEPPER.config = { 51 config: null, 52 environment: null, 53 language: null, 54 currency: null, 55 sessionName: 'pepper_config', 56 domain: null, 57 domainConfig: null, 58 apiKey: null, 59 publicKey: null, 60 61 init: async function (environment, language, currency, apiKey, publicKey) { 62 this.environment = environment; 63 this.domain = (environment == "PRO") ? 'https://api.peppermoney.es' : 'https://play-api.peppermoneytest.es'; 64 this.domainConfig = (environment == "PRO") ? 'https://merchant-config.pepperfinance.es' : 'https://play-merchant-config.pepperfinance.es'; 65 this.language = language; 66 this.publicKey = publicKey; 67 this.apiKey = apiKey; 68 this.currency = currency; 69 try { 70 await this.getConfig(); 71 } catch (err) { 72 console.error(err); 73 } 74 }, 75 getConfig: async function() { 76 try{ 77 if (getSessionStoragePepper(this.sessionName) !== null) { 78 this.config = JSON.parse(getSessionStoragePepper(this.sessionName)).data; 79 if (this.config.enabled === true) { 80 await this.injectAssets(); 81 } 82 } 83 else { 84 await this.getConfigAjax() 85 .catch(function(error){ 86 console.error(error); 87 }); 88 } 89 } 90 catch (err) { 91 console.error(err); 92 } 93 }, 94 getConfigAjax: async function () { 95 var that = this; 96 return new Promise(function(resolve, reject) { 97 var xmlHttp = new XMLHttpRequest(); 98 xmlHttp.onreadystatechange = function() { 99 if (xmlHttp.readyState == 4) { 100 var response = JSON.parse(xmlHttp.responseText); 101 if (typeof response == 'object') { 102 if (!response.result.errors) { 103 that.config = response.data; 104 setSessionStoragePepper(that.sessionName, JSON.stringify(response)); 105 if (that.config.enabled === true) { 106 resolve(that.injectAssets()); 107 } 108 if (that.config.error) { 109 reject(that.config.error); 110 } 111 } else { 112 reject (response.result); 113 } 114 } 115 } 116 } 117 xmlHttp.onerror = () => reject('Petition error'); 118 xmlHttp.ontimeout = () => reject('Timeour error'); 119 xmlHttp.open('GET', that.domainConfig + '/api/v1/merchants/' + that.publicKey + '/config?language=' + that.language + '¤cy=' + that.currency + '&origin=' + encodeURIComponent(location.origin) , true); 120 xmlHttp.setRequestHeader("Content-type", "application/json"); 121 xmlHttp.send(); 122 123 }); 124 }, 125 injectAssets: async function () { 126 try { 127 this.getCss(); 128 await this.getJs(); 129 } catch (err) { 130 throw err; 131 } 132 }, 133 getCss: function () { 134 var url = this.config.css_version; 135 var elmt = document.createElement("link"); 136 elmt.setAttribute('rel', 'stylesheet'); 137 elmt.setAttribute('type', 'text/css'); 138 elmt.setAttribute('href', url); 139 140 document.getElementsByTagName("head")[0].appendChild(elmt); 141 }, 142 getJs: async function () { 143 var that = this; 144 return new Promise(function(resolve, reject) { 145 var elmt = document.createElement("script"); 146 elmt.setAttribute('type', 'text/javascript'); 147 elmt.setAttribute('src', that.config.js_version); 148 149 var elmt2 = document.createElement("script"); 150 elmt2.setAttribute('type', 'text/javascript'); 151 elmt2.setAttribute('src', that.config.js_platform_version); 152 153 elmt2.addEventListener('load', function() { 154 try { 155 if (typeof PEPPER === 'object' && typeof PEPPER.widgets === 'object') { 156 PEPPER.widgets.init( 157 that.config, that.domain, that.publicKey, that.apiKey, that.currency 158 ); 159 } else { 160 console.error('PEPPER.widgets is not an object'); 161 } 162 } catch (err) { 163 console.error(err); 164 } 165 }); 166 167 var head_element = document.getElementsByTagName("head")[0]; 168 elmt.addEventListener('load', function() { 169 head_element.appendChild(elmt2); 170 }); 171 head_element.appendChild(elmt); 172 resolve(); 173 }); 174 } 175}; 176 177 178let settimeoutId=null; 179document.addEventListener("DOMContentLoaded", function(event) { 180 PEPPER.config.init( 181 pepper_environment, 182 "ES", 183 "EUR", 184 pepper_api_key, 185 pepper_public_key 186 ); 187 188 //Show DVN action. 189 settimeoutId = setTimeout(showPaymentNWithQuotes, 100); 190 //Managed mode. 191 var aux = new Date(); 192 var expireDate = new Date(aux.getTime() + 10*60000); 193 if (window.location.href.indexOf('managed_mode') !== -1) 194 document.cookie = ['managed_mode=1; path=/', '; expires=' + expireDate.toUTCString()].join(''); 195}); 196 197 198window.addEventListener("message", function(event) { 199 if (event.data == 'closePepperIframe' || (event.data.message && event.data.message == 'close iframe')) { 200 if (event.data.message == 'close iframe') alert('Acceso enviado, cierre esta pestaña del navegador'); 201 if (document.getElementById('peppermodal')) document.getElementById('peppermodal').style.display = 'none'; 202 if (document.getElementById('peppermodaln')) document.getElementById('peppermodaln').style.display = 'none'; 203 } 204}); 205 206function showPaymentNWithQuotes() { 207 clearTimeout(settimeoutId); 208 if (PEPPER.widgets) { 209 let tag = document.querySelector('#pepper-payment-n-options'); 210 if (tag) { 211 const spc = sessionStorage.getItem('pepper_config');
212 const pc = JSON.parse(spc); 213 const pc_checkout = pc.data.widget_sections.filter(x=>x.section=='checkout')[0]; 214 const price_tag = document.querySelector(pc_checkout.dom_var_price); 215 let amount = parseAmountHelperPepper(price_tag); 216 let data_dvn = PEPPER.widgets.getFinancialInfo(amount, null, 'total', 'DVN'); 217 218 sectionConf = PEPPER.config.config.widget_sections.find(x=>x.section=='checkout'); 219 if(sectionConf){ 220 showType = sectionConf.dom_payment_type.toLowerCase(); 221 if(showType=='radio'){ 222 prepareDVNasRadio(tag, data_dvn); 223 let widget = pc_checkout.widgets.filter(x=>x.type=='DVN')[0] 224 widget.dom_payment_check = "input[type='radio'][data-module-name=pepper_n]"; 225 sessionStorage.setItem('pepper_config', JSON.stringify(pc)); 226 }else if(showType=='class'){ 227 prepareDVNasClass(tag, data_dvn); 228 } 229 } 230 } 231 } else { 232 settimeoutId = setTimeout(showPaymentNWithQuotes, 100); 233 } 234} 235 236function parseAmountHelperPepper(amount) { 237 var splitAmount = getInnerTextNumberHelperPepper(amount, "0").replace(/[^0-9.,]+/gm, '').split(','); 238 var finalAmount = null; 239 if (splitAmount[splitAmount.length - 1].indexOf('.') > -1) { 240 var decimalAmount = splitAmount[splitAmount.length - 1].split('.'); 241 splitAmount[splitAmount.length - 1] = decimalAmount[0]; 242 finalAmount = splitAmount.join(''); 243 if (decimalAmount.length === 2 && decimalAmount[decimalAmount.length -1].length === 3) { 244 finalAmount = decimalAmount.join('') 245 } 246 else if (decimalAmount.length === 2) { 247 finalAmount += '.' + decimalAmount[1]; 248 } 249 } else { 250 finalAmount = splitAmount[0].replace('.', '') + '.' + splitAmount[1]; 251 } 252 253 return parseFloat(finalAmount); 254} 255 256function getInnerTextNumberHelperPepper(elem, defaultValue){ 257 var result = defaultValue; 258 if (elem!==null){ 259 try { 260 if (elem.value){ 261 result = elem.value; 262 } 263 else if (elem.innerText){ 264 result = elem.innerText 265 } 266 else { 267 result = defaultValue; 268 } 269 } catch (err) { 270 result = defaultValue; 271 } 272 } 273 return result; 274} 275 276function prepareDVNasRadio(tag, data_dvn){ 277 let parentDescription = tag.parentElement; 278 let key = parentDescription.id.replace('-additional-information',''); 279 if (!key) return; 280 if (data_dvn.payment && data_dvn.term > 0) { 281 let input = document.querySelector('#'+key); 282 input.setAttribute('data-module-name','pepper_n'); 283 let label = document.querySelector('label[for='+key+'] span'); 284 let text = label.textContent; 285 text = text.replace('{{NumCuotas}}', data_dvn.term); 286 label.textContent = text; 287 } else { 288 tagContainer = document.querySelector('#'+key+'-container'); 289 let parentContainer = tagContainer.parentElement; 290 parentContainer.style.display = 'none'; 291 parentDescription.style.display = 'none'; 292 } 293} 294 295function prepareDVNasClass(tag, data_dvn){ 296 if (data_dvn.payment && data_dvn.term > 0) { 297 let img = tag.querySelector('img'); 298 let text = img.getAttribute('title'); 299 text = text.replace('{{NumCuotas}}', data_dvn.term); 300 img.setAttribute('title', text); 301 } else { 302 let parentContainer = tag.parentElement; 303 parentContainer.style.display = 'none'; 304 } 305}
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.