reverse-report.js 2.33 KB
"use strict";

const xmlrpc = require('xmlrpc');
const stringify = require("json-stringify-pretty-compact");

const config = require('komodo-sdk/config');
const logger = require('tektrans-logger');
const matrix = require('komodo-sdk/matrix');

const st24 = require('./st24');
const partner = require('./partner');
// const partnerRc = require('./partner-rc.json');

function create() {
    if (!config || !config.reverse_report_port) {
        logger.info('Not creating reverse report server because of undefined config.reverse_report_port');
        return;
    }

    const server = xmlrpc.createServer({ port: config.reverse_report_port });

    logger.info('Reverse report server listen on port ' + config.reverse_report_port);

    server.on('NotFound', function (method, params) {
        logger.warn('REVERSEREPORT: Unknown method recevied on XMLRPC server', {method: method, params: params});
    });

    server.on('topUpReport', function (err, params, callback) {

        logger.info('REVERSEREPORT: Got XMLRPC topUpReport request from partner', {method: 'topUpReport', params: params});
        matrix.last_topupReport_params = params;

        const paramsCount = params.length;
        for (let i = 0; i < paramsCount; i++) {
            let value = params[i];

            partner.report({
                trx_id: value.REQUESTID,
                // rc: partnerRc[value.RESPONSECODE] || '40',
                rc: partner.komodoRc(value[partner.RESPONSECODE_TAG]) || '40',
                //message: value.MESSAGE,
                message: 'topUpReport: ' + stringify(value),
                sn: (value.SN || '').replace(/;+$/, '').replace(/#+$/, '') || st24.extractSnFromMessage(
                    value.MESSAGE, 
                    config.partner.sn_pattern || config.sn_pattern,
                ),
                amount: value.PRICE || st24.extractPriceFromMsg(
                    value.MESSAGE,
                    config.partner.price_pattern || config.partner.amount_pattern || config.amount_pattern,
                ),
                balance: st24.extractBalanceFromMsg(
                    value.MESSAGE,
                    config.partner.balance_pattern || config.balance_pattern,
                ),
                raw: value,
                misc: {
                }
            });
        }

        callback(null, 'ACK REPORT OK');
    })
}

create();