var request = require('request'); var xpath = require('xpath'); var parse5 = require('parse5'); var xmlser = require('xmlserializer'); var dom = require('xmldom').DOMParser; var xml2js = require('xml2js').parseString; var argv = require('minimist')(process.argv.slice(2), {string: ['requestid', 'destination']}); var strftime = require('strftime'); var fs = require('fs'); var ini = require('ini'); var config; function advice(data, callback) { if (!data.trxid && !data.destination) { console.log('Webreport advice request without data. Canceling'); return; } if (data.config) { config = data.config; } else { try { config = ini.parse(fs.readFileSync(__dirname + '/config.ini', 'utf-8')); } catch(err) { console.log('Error loading config file'); return; } } login(data, callback); }; function login(data, callback) { var jar = request.jar(); var options = { url: 'http://103.11.75.142:9009/dealer/index.php/admin/login', jar: jar, followAllRedirects: true, form: { username: config.webreport.username, password: config.webreport.password, Submit: 'Login', }, }; //console.log('Requesting', options); request.post(options, function(error, httpResponse, body) { if (error) { console.log('Error retrieving login'); return; } if (body.search('Incorrect username') >= 0) { console.log('Salah username / password'); return; } getTrxStatusPage(data, jar, callback); }); } function getTrxStatusPage(data, jar, callback) { var options = { url: 'http://103.11.75.142:9009/dealer/index.php/transaction/index', jar: jar, followAllRedirects: true, form: { startdate: '1970-01-01', enddate: strftime('%F'), pdate: '', trxid: '', destmisdn: '', type: 0, ts: '', submit: 'Find', }, }; if (data.trxid) { options.form.trxid = data.trxid; } if (data.destination) { options.form.destmisdn = data.destination; } //console.log(options); request.post(options, function(error, httpResponse, body) { if (error) { console.log('Error retrieving trx status page'); return; } body = body.replace(/<\/thead>\s<\/tbody>/, "</thead>\n<tbody>"); var document = parse5.parse(body); var xhtml = xmlser.serializeToString(document); var doc = new dom().parseFromString(xhtml); var select = xpath.useNamespaces({"x": "http://www.w3.org/1999/xhtml"}); var nodes = select('//x:*[@id="dirlist"]/x:tbody/x:tr/x:td', doc); var status = {}; try { status = { trxId: nodes[0].firstChild.data, trxDate: nodes[1].firstChild.data, updateDate: nodes[2].firstChild.data, product: nodes[3].firstChild.data, amount: nodes[4].firstChild.data, msisdn: nodes[5].firstChild.data, reffId: nodes[6].firstChild.data, status: nodes[7].firstChild.data, response: nodes[8].toString(), } } catch(errStatus) { console.log('Error parsing status'); if (callback) { callback(); } return; } status.response = status.response.replace('<td>', ''); status.response = status.response.replace('</td>', ''); status.response = status.response.replace(/\n/g, ''); status.response = status.response.trim(); //status.response_raw = status.response; xml2js(status.response, function (err, result) { if (err) { console.log('Gagal parsing XML', status.response); if (callback) { status.response = {}; callback(status); } return; } status.response = result.result; if (!callback) { console.log(status); } else { callback(status); } }); }); } if (require.main === module) { var data = { trxid: argv.trxid, destination: argv.destination, } if (argv.trxid) { data.trxid = argv.trxid; } if (argv.destination) { data.destinaton = argv.destination; } advice(data); } exports.advice = advice;