odoo.define('pos_pax.pax_device', function (require) { "use strict"; var $ = require('pos_pax.jquery_base64'); // Hibou Corp. © 2020 - Wrapped library for PAX Device // Additions and Fixes: // 2020-11-12 Fixed variable i and ii in getLRC // 2020-11-12 Fixed/added 'fail' mechanisms for XHR // 2020-11-23 Added new DoDebit method //HEX TO BASE64 function hexToBase64(str) { return $.base64.btoa(String.fromCharCode.apply(null, str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")) ); } //BASE64 TO HEX function base64ToHex(str) { for (var i = 0, bin = $.base64.atob(str), hex = []; i < bin.length; ++i) { var tmp = bin.charCodeAt(i).toString(16); if (tmp.length === 1) tmp = "0" + tmp; hex[hex.length] = tmp; } return hex.join(" "); } function StringToHex(response){ var responseHex = ""; for(var i=0; i0)?String.fromCharCode(lrc):0; }, //Connect to the server HttpCommunication : function(commandType,url,callback,timeout,fail){ var xhr = null; if(window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else { try{ xhr = new ActiveXObject('Microsoft.XMLHttp'); }catch(e){ xhr = new ActiveXObject('msxml2.xmlhttp'); } } //get xhr.open("GET", url,true); xhr.onreadystatechange=function(){ if(xhr.readyState==4) { //alert(xhr.status); if(xhr.status==200) { var response = xhr.responseText; console.log("Raw response: "+response); var checkParams = StringToHex(response).split(" ").pop(); var RedundancyCheck = StringToHex(response).split(" ").pop().substring(1); var check = PAX.getLRC(checkParams); if(check == RedundancyCheck){ //get package detail info var packetInfo = []; var len = StringToHex(response).indexOf("03"); var hex = StringToHex(response).slice(0,len).split(/02|1c/); console.log(hex); if(commandType == "DoCredit" || commandType == "DoDebit"){ var subHex=[], subPacketInfo=[]; for(var i=0; i0){ subHex = hex[i].split("1f"); console.log(subHex); subPacketInfo = []; for(var j=0; j"+customData+""); // } var url = this.mDestinationIP + '?' + final_b64; console.log("URL: " + url); this.HttpCommunication('DoCredit',url,function(response){ callback(response); },PAX.timeout.DoCredit,fail); }, //DoDebit DoDebit : function(doDebitInfo,callback,fail){ var amountInformation,accountInformation,traceInformation,avsInformation,cashierInformation,commercialInformation,motoEcommerce,additionalInformation; var params = [this.mStx.hex,doDebitInfo.command, this.mFS.hex, doDebitInfo.version]; params.push(this.mFS.hex); // if(doDebitInfo.transactionType != ''){ params.push(doDebitInfo.transactionType); // } params.push(this.mFS.hex); params = this.PushParams(params,"amountInformation",doDebitInfo.amountInformation); params.push(this.mFS.hex); params = this.PushParams(params,"accountInformation",doDebitInfo.accountInformation); params.push(this.mFS.hex); params = this.PushParams(params,"traceInformation",doDebitInfo.traceInformation); // params.push(this.mFS.hex); // params = this.PushParams(params,"avsInformation",doCreditInfo.avsInformation); params.push(this.mFS.hex); params = this.PushParams(params,"cashierInformation",doDebitInfo.cashierInformation); // params.push(this.mFS.hex); // params = this.PushParams(params,"commercialInformation",doCreditInfo.commercialInformation); // // params.push(this.mFS.hex); // params = this.PushParams(params,"motoEcommerce",doCreditInfo.motoEcommerce); params.push(this.mFS.hex); params = this.PushParams(params,"additionalInformation",doDebitInfo.additionalInformation); params.push(this.mEtx.hex); var lrc = this.getLRC(params); console.log(params); //prepare for base64 encoding. var command_hex = base64ToHex($.base64.btoa(doDebitInfo.command)); var version_hex = base64ToHex($.base64.btoa(doDebitInfo.version)); var transactionType_hex = base64ToHex($.base64.btoa(doDebitInfo.transactionType)); var amountInformation_hex = base64ToHex($.base64.btoa(doDebitInfo.amountInformation)); var accountInformation_hex = base64ToHex($.base64.btoa(doDebitInfo.accountInformation)); var traceInformation_hex = base64ToHex($.base64.btoa(doDebitInfo.traceInformation)); var avsInformation_hex = base64ToHex($.base64.btoa(doDebitInfo.avsInformation)); var cashierInformation_hex = base64ToHex($.base64.btoa(doDebitInfo.cashierInformation)); var commercialInformation_hex = base64ToHex($.base64.btoa(doDebitInfo.commercialInformation)); var motoEcommerce_hex = base64ToHex($.base64.btoa(doDebitInfo.motoEcommerce)); var additionalInformation_hex = base64ToHex($.base64.btoa(doDebitInfo.additionalInformation)); //var elements = [this.mStx.code, command_hex, this.mFS.code, version_hex, this.mFS.code, uploadFlag_hex, this.mFS.code, timeout, this.mEtx.code, base64ToHex($.base64.btoa(lrc))]; var elements = [this.mStx.code]; elements.push(command_hex); elements.push(this.mFS.code); elements.push(version_hex); elements.push(this.mFS.code); if(transactionType_hex != ''){ elements.push(transactionType_hex); } elements.push(this.mFS.code); elements = this.AddBase64(elements,"amountInformation",doDebitInfo.amountInformation); elements.push(this.mFS.code); elements = this.AddBase64(elements,"accountInformation",doDebitInfo.accountInformation); elements.push(this.mFS.code); elements = this.AddBase64(elements,"traceInformation",doDebitInfo.traceInformation); // elements.push(this.mFS.code); // elements = this.AddBase64(elements,"avsInformation",doCreditInfo.avsInformation); elements.push(this.mFS.code); elements = this.AddBase64(elements,"cashierInformation",doDebitInfo.cashierInformation); // elements.push(this.mFS.code); // elements = this.AddBase64(elements,"commercialInformation",doCreditInfo.commercialInformation); // elements.push(this.mFS.code); // elements = this.AddBase64(elements,"motoEcommerce",doCreditInfo.motoEcommerce); elements.push(this.mFS.code); elements = this.AddBase64(elements,"additionalInformation",doDebitInfo.additionalInformation); elements.push(this.mEtx.code); elements.push(base64ToHex($.base64.btoa(lrc))); console.log("elements"); console.log(elements); var final_string = elements.join(" "); var final_b64 = hexToBase64(final_string); console.log("LRC: " + lrc); console.log("Base64: " + final_b64); // if(customData != ''){ // final_b64 = hexToBase64(final_string+"&custom_data="+customData+""); // } var url = this.mDestinationIP + '?' + final_b64; console.log("URL: " + url); this.HttpCommunication('DoDebit',url,function(response){ callback(response); },PAX.timeout.DoDebit,fail); }, }; return PAX; });