(function() { /*************************************************************************/ /* YPSILON.NET Payment SCA-IFRAME Solution Juni 2019 /*************************************************************************/ if (typeof eventMessageSca === 'undefined') { eventMessageSca = null; } if (typeof scaFinished === 'undefined') { scaFinished = true; } function scaClient(config) { var classPrefix = 'SCA_P::'; var state = 'init'; // mandetory var transactionId = "b0aa94bf-aac3-4c6a-94a2-7678a6cacc58"; var wrapId = null; // var redirectUrl = null; var ypsOrigin = "https:\/\/psp-staging.infosys.de"; var debug = true; var onChallengeCallback = null; var onFinishedCallback = null; var iframe = { size: null, className: null, /* not editable */ name: 'yps_sca_frame', nameId: 'yps_sca_frame', domElement: null, domElementSrc: null, domElementWrap: null, iframeUrl: "https:\/\/psp-staging.infosys.de\/sca\/iframe", init: false, invalidCallback: function (msg) { alert(msg); }, ready: false, }; var self = this; self.init = init; /****************************** HELPERS ******************************/ function debug_log(prefix, msg) { if (debug) { console.log(classPrefix + prefix + ': ' + msg); } } function sameOrigin(incomingOrigin) { var prefix = '[sameOrigin()]'; if (ypsOrigin === incomingOrigin) { return true; } return false; } function sendOnChallengeCallback(data) { if (onChallengeCallback) { var challengeData = {}; if (data && typeof data === 'string') { challengeData = JSON.parse(data); } onChallengeCallback(challengeData); // onChallengeCallback(); } } function sendOnFinishedCallback(id, data) { scaFinished = true; if (onFinishedCallback) { var finishData = {}; var ids = { 1000: 'transactionId not set', 1001: 'transactionId not valid', 1002: 'wrapId not set', 1003: 'wrapId not found', }; if (ids[id]) { finishData.success = false; finishData.message = ids[id]; } if (data && typeof data === 'string') { finishData = JSON.parse(data); } onFinishedCallback(finishData); } } function createUrlParams(data) { return Object.keys(data).map(function(k) { return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) }).join('&'); } /****************************** HELPERS ******************************/ /*********************************************************************/ function addEvent(object, eventName, eventFunction) { if (object.addEventListener) { // For all major browsers, except IE 8 and earlier object.addEventListener(eventName,eventFunction); } else { // For IE 8 and earlier versions object.attachEvent('on'+eventName,eventFunction); } } /*********************************************************************/ function removeEvent(object, eventName, eventFunction) { if (object.removeEventListener) { // For all major browsers, except IE 8 and earlier object.removeEventListener(eventName, eventFunction); } else if (x.detachEvent) { // For IE 8 and earlier versions object.detachEvent('on'+eventName, eventFunction); } } /*********************************************************************/ /* * attach eventhandler for 'message' */ function attachMessageEventhandler() { var prefix = '[attachMessageEventhandler()]'; debug_log(prefix, 'attach Message handler to window'); if (eventMessageSca) { removeEvent(window, 'message', eventMessageSca); } eventMessageSca = parseMessage; if (eventMessageSca) { addEvent(window, 'message', eventMessageSca); } } /*********************************************************************/ /* * Message handler vor incomming Messages from the iframes * there is always e.data.func and e.data.type * which is either 'cc' or 'cvc' at the moment */ function parseMessage(e) { var prefix = '[parseMessage()]'; var func = e.data.func; if (!sameOrigin(e.origin)) { return; } if (func === 'init') { debug_log(prefix, 'init triggert'); iframe.init = true; iframe.domElementSrc = e.source; } else if (func === 'setState') { debug_log(prefix, 'setState triggert'); setState(e.data.state); } else if (func === 'sendChallenge') { debug_log(prefix, 'sendChallenge triggert'); sendOnChallengeCallback(e.data.challengeData); } else if (func === 'sendFinished') { debug_log(prefix, 'sendFinished triggert'); setState(e.data.state); sendOnFinishedCallback('', e.data.finishData); } else if (func === 'getRedirectUrl') { var data = { func: 'tryPostRedirect', redirectUrl: redirectUrl, finishData: e.data.finishData, }; iframe.domElementSrc.postMessage(data,'*'); } else if (func === 'redirectOut') { if (!redirectUrl) { debug_log(prefix, 'redirectUrl not set'); return; } redirectOut(e.data.finishData); } } function redirectOut(finishData) { var params = createUrlParams(finishData); var url = redirectUrl + '?' + params; window.location.replace(redirectUrl); } function addParamsToUrl(url, sizeNumber) { var ifrUrl = url + '/' + transactionId + '?'; ifrUrl += 'iframe_debug=' + debug; ifrUrl += '&challengeWindowSize=' + sizeNumber; return ifrUrl; } /*********************************************************************/ function setIframeSize() { var sizes = { height: '400px', width: '600px', number: '04', } if (iframe.size && scaClient['SIZE' + iframe.size]) { var size = scaClient.sizes[iframe.size]; sizes.width = size.x; sizes.height = size.y; sizes.number = size.number; } return sizes; } /*********************************************************************/ /* * append Iframes to all FOUND wrapper */ function buildIframes() { var prefix = '[buildIframes()]'; removePreviousIframe(); var wrapper = document.querySelector('#'+wrapId); var ifrm = document.createElement("IFRAME"); var ifrId = "yps_sca_frame_" + iframe.nameId + '_' + transactionId; var sizes = setIframeSize(); var src = addParamsToUrl(iframe.iframeUrl, sizes.number); ifrm.setAttribute("src", src); ifrm.setAttribute("class", 'yps_sca_frame'); ifrm.id = ifrId; ifrm.style.overflow = "none"; ifrm.name = iframe.name; ifrm.style.width = sizes.width; ifrm.style.height = sizes.height; iframe.domElement = ifrm; iframe.domElementWrap = wrapper; iframe.domElementWrap.appendChild(ifrm); } /*********************************************************************/ function removePreviousIframe() { var prevIframe = document.querySelector('.yps_sca_frame'); if (prevIframe) { // because of IE 10 / 11 prevIframe.parentNode.removeChild(prevIframe); } } /*********************************************************************/ function setIframeDefautParams(newCfg, defCfg) { var iframeParamsList = [ 'className', 'size', ]; for (var i = 0; i < iframeParamsList.length; i++) { var key = iframeParamsList[i]; defCfg[key] = getExistingValue(newCfg[key], defCfg[key]); } return defCfg; } /*********************************************************************/ /* * function to check given formId */ function checkTransactionId(transactionId) { var prefix = 'checkTransactionId()'; if (!transactionId) { sendOnFinishedCallback(1000); return false; } var rgx = '/^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB]'; rgx += '[0-9A-F]{3}-[0-9A-F]{12}$/i'; var re = new RegExp(rgx); if (re.test(transactionId)) { sendOnFinishedCallback(1001); return false; } return true; } /*********************************************************************/ /* * function to check given formId */ function checkWrapId(wrapId) { var prefix = 'checkFormId()'; if (!wrapId) { sendOnFinishedCallback(1002); return false; } var wrapper = document.getElementById(wrapId); if (!wrapper) { sendOnFinishedCallback(1003); return false; } return true; } /*********************************************************************/ /* * Returns existing value, else default value * * @param * value value that should be set * def default value * * @return * if success value, else default value */ function getExistingValue(value, def) { var prefix = 'getExistingValue'; if (typeof value !== 'undefined' && value !== null) { return value; } return def; } function isFinished() { if (scaFinished) { return true; } return false; } /*********************************************************************/ /* * Overwrite existing values, else set default values * * @param * cfg json with values * * @return * if success setting new values, else uses default values */ function init(cfg) { var prefix = '[init()]'; if (!scaFinished) { debug_log(prefix, 'SCA not finshed yet, try again later'); return false; } if (cfg) { /* callback configs */ onChallengeCallback = getExistingValue( cfg.onChallengeCallback, null ); onFinishedCallback = getExistingValue( cfg.onFinishedCallback, null ); /* global configs */ transactionId = getExistingValue(cfg.transactionId, transactionId); wrapId = getExistingValue(cfg.wrapId, wrapId); // redirectUrl = getExistingValue(cfg.redirectUrl, redirectUrl); if (cfg) { iframe = setIframeDefautParams(cfg, iframe); } } if (!checkTransactionId(transactionId)) { return false; } if (!checkWrapId(wrapId)) { return false; } scaFinished = false; buildIframes(); attachMessageEventhandler(); } self.init(config); } setSizeVaraibles(); function setSizeVaraibles() { // 01 = 250 x 400 // 02 = 390 x 400 // 03 = 500 x 600 // 04 = 600 x 400 // 05 = Full screen scaClient.sizes = { '250_400': { x: '250px', y: '400px', number: '01', }, '390_400': { x: '390px', y: '400px', number: '02', }, '500_600': { x: '500px', y: '600px', number: '03', }, '600_400': { x: '600px', y: '400px', number: '04', }, 'FULLSCREEN': { x: '100%', y: '100%', number: '05', } }; for(size in scaClient.sizes) { var sizeParams = scaClient.sizes[size]; scaClient["SIZE" + size] = size; } } setState('init'); function setState(state) { scaClient.STATE = state; } /*************************************************************************/ // for single-form scaClient.init = function(config) { // if singleform object doesen't exists create new one if (!scaClient._singleForm) { scaClient._singleForm = new scaClient(config); } return scaClient._singleForm; }; window.scaClient = scaClient; }());