partner-komodo.js 4.54 KB
"use strict";

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

const http = require('http');
const url = require('url');
const request = require('request');
const resendDelay = require('sate24/resend-delay')

const komodoRc = {
    '00': '00',
    '03': '40',
    '13': '13',
    '14': '14',
    '30': '40',
    '40': '40',
    '68': '68',
    '55': '55',
    '91': '91',
    '92': '40',
    '96': '68'
}

var config;
var aaa;
var logger;

function start(options) {
    if (!options) {
        console.log('Undefined options, terminating....');
        process.exit(1);
    }

    if (options.config) {
        config = options.config;
    } else {
        console.log('Undefined options.config, terminating....')
        process.exit(1);
    }

    if (options.aaa) {
        aaa = options.aaa;
    } else {
        console.log('Undefined options.aaa, terminating....')
        process.exit(1);
    }

    if (options && options.logger) {
        logger = options.logger;
    } else {
        console.log('Undefined options.logger, terminating....')
        process.exit(1);
    }

    resendDelay.init({config: config, logger: logger, topupRequest: topupAdvice});

    createReverseHttpServer();

}

function callbackReport(requestId, rc, message, options) {
    aaa.callbackReportWithPushToMongoDb(requestId, rc, message);

    if (rc === '68') {
        if (options && options.task) resendDelay.register(options.task);
    } else {
        resendDelay.cancel(requestId);
    }
}

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

    const requestOptions = {
        url: config.h2h_out.partner,
        qs: {
            terminal_name: config.h2h_out.terminal_name,
            password: config.h2h_out.password,
            product_name: task.remoteProduct,
            destination: task.destination,
            request_id: task.requestId,
            reverse_url: config.h2h_out.reverse_url
        }
    }

    logger.verbose('Requesting to partner', {request_options: requestOptions});

    request(requestOptions, function(err, response, body) {
        if (err) {
            logger.warn('Error requesting to partner', {err: err});

            let rc = '68';

            if (err.syscall == 'connect' && !pendingOnConnectError) {
                rc = '91';
            }
            callbackReport(task.requestId, rc, 'Error requesting to partner: ' + err, {task: task});
            return;
        }

        if (response.statusCode != 200) {
            let rc = '68';

            callbackReport(task.requestId, rc, 'Partner returning HTTP status code ' + response.statusCode + ', not 200', {task: task});
            return;
        }

        logger.verbose('Got response from partner', {task: task, body: body});

        let result = parsePartnerMessage(body);

        if (!result) {
            callbackReport(task.requestId, '40', 'Error parsing response from partner. Partner response: ' + body, {task: task});
            return;
        }

        processPartnerResponse(result, task);

    })
}

function topupAdvice(task) {
    topupRequest(task, true);
}

function processPartnerResponse(resObj, task) {
    let st24Rc = '40';

    if (!resObj || !resObj.rc) {
        st24Rc = '68';
    }
    else if (komodoRc[resObj.rc]) {
        st24Rc = komodoRc[resObj.rc];
    }

    let st24Message = resObj.message;

    if (resObj && resObj.rc) {
        st24Message += '| KOMODORC: ' + resObj.rc;
    }

    if (resObj.sn) {
        let sn = resObj.sn;
        if (sn.indexOf('/') >= 0) {
            sn = sn.trim().replace(/[\s-]+/g, '-');
        }
        st24Message = 'SN=' + sn + '; ' + st24Message;
    }

    callbackReport(resObj.request_id, st24Rc, st24Message, {task: task});
}

function parsePartnerMessage(partner_message) {
    let result;
    try {
        result = JSON.parse(partner_message);

    }
    catch(e) {
        logger.verbose('Exception on parsing partner message: ' + partner_message);
        result = null;
    }

    return result;
}

function createReverseHttpServer() {
    let listenPort = config.h2h_out.listen_port;

    http.createServer(onReverseReport).listen(listenPort, function() {
        logger.info('HTTP Reverse/Report server listen on port ' + listenPort);
    });
}

function onReverseReport(request, response) {
    response.end('OK');

    const qs = url.parse(request.url, true).query;
    qs.message = 'REVERSE REPORT: ' + qs.message;
    logger.verbose('Got reverse report from partner', {qs: qs});
    processPartnerResponse(qs);
}


exports.start = start;
exports.topupRequest = topupRequest;
exports.topupAdvice = topupAdvice;