partner-bayarkilat.js 5.08 KB
var request = require('request');
var url = require('url');
var winston = require('winston');
var xml2jsParser = require('xml2js').parseString;

var config;
var aaa;
var callbackReport;
var logger;

process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

function start(_config, _callbackReport, options) {
    config = _config;
    callbackReport = _callbackReport;

    if (options && options.aaa) {
            aaa = options.aaa;
    }

    if (options && options.logger) {
        logger = options.logger;
    } else {
        logger = new winston.Logger({
            transports: [
              new (winston.transports.Console)()
            ]
        });
    }
}


function topupRequest(task, retry) {
    aaa.insertTaskToMongoDb(task);

    var partnerUrl = url.parse(config.h2h_out.partner);
    var product = prepareRemoteProductCode(task.remoteProduct);

    var options = {
        url: config.h2h_out.partner,
        qs: {
            request: 'PURCHASE*'
                + task.requestId + '*'
                + product.product + '*'
                + product.productDetail + '*'
                + task.destination + '*'
                + product.nominal + '*'
                + '0*'
                + config.h2h_out.noid + '*'
                + config.h2h_out.userid + '*'
                + config.h2h_out.password
        }
    };

    logger.verbose('Requesting to partner', {requestOption: options});
    request(options, function(error, response, body) {
        if (error) {
            logger.warn('Error requesting to partner', {error: error});
            callbackReport(task.requestId, '68', 'Error requesting to partner. ' + error);
            return;
        }

        if (response.statusCode != 200) {
            var message = 'Partner response with http status code other that 200 (' + response.statusCode +')';

            logger.warn(message);
            callbackReport(task.requestId, '68', message);
            return;
        }

        /*
        logger.verbose('Got response', {requestId: task.requestId, responseBody: body});
        callbackReport(task.requestId, '68', body);
        */

        logger.verbose('Got respose', {rawBody: body});
        parseResponse(body, task);
    });
}

function getSn(response) {
    try {
        var sn = response.xml.ket1[0];
        return sn;
    }
    catch (err) {
        return;
    }
}

function parseResponse(body, task) {
    xml2jsParser(body, function(err, response) {
        if (err) {
            logger.warn('Error parsing XML', {error: err, task: task, responseBody: body});

            var message = 'Error parsing XML. ' + err + '. ' + body;

            var _response = {
                raw: body,
                parsed: {
                    MESSAGE: message
                }
            }
            aaa.pushResponseToMongoDb(task, _response, '68');

            callbackReport(task.requestId, '68', message);
            return;
        }

        logger.info('Got response', {response: response});

        var responseCode;
        var message;
        var saldo;

        try {
            responseCode = response.xml.response_code[0];
            saldo = response.xml.saldo[0];
            message = response.xml.response_message[0];
        }
        catch(errGetParam) {
            logger.warn('Exception saat parsing hasil', {error: errGetParam, task: task, responseBody: body});

            var _response = {
                raw: body,
                parsed: {
                    MESSAGE: 'Exception saat parsing hasil. ' + errGetParam,
                    xml: response.xml
                }
            }
            aaa.pushResponseToMongoDb(task, _response, '68');

            callbackReport(task.requestId, '68', 'Exception saat parsing hasil. ' +  errGetParam);
            return;
        }

        var st24rc;
        if (parseInt(responseCode) == 0) {
            st24rc = '00';
        }
        else if (parseInt(responseCode) == '99') {
            st24rc = '68'
        }
        else {
            st24rc = '40';
        }

        var st24message = message;
        if (responseCode) {
            st24message = responseCode + ' ' + st24message;
        }

        if (st24rc == '00') {
            var sn = getSn(response);

            if (sn) {
                st24message = 'SN=' + sn + ';' + st24message + '. SN=' + sn;
            }
        }

        if (saldo) {
            st24message = st24message + '. Saldo ' + saldo;
            aaa.updateBalance(saldo);
        }

        var _response = {
            raw: body,
            parsed: {
                MESSAGE: st24message,
                xml: response.xml
            }
        }

        aaa.pushResponseToMongoDb(task, _response, st24rc);

        callbackReport(task.requestId, st24rc, st24message);



    });
}

function prepareRemoteProductCode(remoteProduct) {
    var product = remoteProduct.split(',');

    if (product.length != 3) {
        return;
    }

    return {
        product: product[0],
        productDetail: product[1],
        nominal: product[2]
    }
}

exports.start = start;
exports.topupRequest = topupRequest;
exports.prepareRemoteProductCode = prepareRemoteProductCode;