cekstatus.js 3.19 KB
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 config = {
    webreport: {
        login_url: 'http://103.11.75.142:9009/dealer/index.php/admin/login',
        username: 'reload97',
        password: '903333',
    }
};

function login() {
    var jar = request.jar();
    
    var options = {
        url: config.webreport.login_url, 
        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(jar);
        
    });
}

function getTrxStatusPage(jar) {
    var options = {
        url: 'http://103.11.75.142:9009/dealer/index.php/transaction/index',
        jar: jar,
        followAllRedirects: true,
        form: {
            startdate: '2016-01-27',
            enddate: '2016-01-27',
            pdate: '',
            trxid: '40477696',
            destmisdn: '',
            type: 0,
            ts: '',
            submit: 'Find',
        },
    };
    
    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 = {
            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(),
        }
        
        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');
                return;
            }
            
            status.response = result.result;
            
            
            console.log(status);
        });
        
        
        
    });
    
}
login();