hit.js 3.86 KB
const MODULE_NAME = 'HIT';

const axios = require('axios').default;

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

const composePayload = require('./generic-xmlrpc/compose-payload');
const report = require('./report');
const dumper = require('./dumper/request');
const axiosSafeFailed = require('./axios-safe-failed');
const parseResult = require('./parse-result');

const defaultAxiosConfig = {
    headers: {
        'User-Agent': 'Komodo-GW-ST24B',
        'Content-Type': 'text/xml',
    },
    timeout: config.partner.hit_timeout = 2 * 60 * 120,
};

module.exports = async (xid, task) => {
    const methodName = config.partner.topuprequest_method_name || 'topUpRequest';

    logger.verbose(`${MODULE_NAME} E4F52474: Processing task`, {
        xid, methodName, task,
    });

    const params = {
        MSISDN: config.partner.msisdn,
        REQUESTID: task.trx_id,
        PIN: config.partner.pin,
        NOHP: task.destination,
        NOM: task.remote_product,
    };

    const payload = composePayload(methodName, params);
    const axiosConfig = JSON.parse(JSON.stringify(defaultAxiosConfig));
    axiosConfig.headers['Content-length'] = payload.length;

    let response;
    let eToDump;

    const endpointUrl = config.partner.url;

    report(xid, {
        trx_id: task.trx_id,
        rc: '68',
        message: {
            xid,
            msg: `Sending request to ${config.partner.url}`,
        },
    });

    try {
        logger.verbose(`${MODULE_NAME} 47FDCA85: Going to HIT partner`, {
            xid,
            endpointUrl,
            methodName,
            params,
        });

        response = await axios.post(endpointUrl, payload, axiosConfig);
        if (!response) {
            const e = new Error(`${MODULE_NAME} BAACC918: Empty response`);
            e.rc = '90';
            logger.warn(e.message, { xid });
            throw e;
        }

        if (!response.data) {
            const e = new Error(`${MODULE_NAME} C816D842: Empty response data`);
            e.rc = '90';
            logger.warn(e.message, { xid });
            throw e;
        }

        if (typeof response.data !== 'string') {
            const e = new Error(`${MODULE_NAME} 32A75E8E: Response data is not a string`);
            e.rc = '90';
            logger.warn(e.message, { xid });
            throw e;
        }

        logger.verbose(`${MODULE_NAME} D0CBD82A: Parsing response`, { xid, responseBody: response.data });
        parseResult(xid, task.trx_id, response.data, false);
    } catch (e) {
        eToDump = e;

        logger.warn(`${MODULE_NAME} 2653B932: Got an exception`, {
            xid,
            eCode: e.code,
            eMessage: e.message,
            eRc: e.rc,
            responseStatus: e.response && e.response.status,
            responseStatusText: e.response && e.response.statusText,
            responseContentType: e.response && e.response.headers && e.response.headers['content-type'],
            responseBody: e.response && e.response.data,
        });

        if (e.response) response = e.response;

        const rc = e.rc
            || (axiosSafeFailed(e) || '91')
            || '68';

        report(xid, {
            trx_id: task.trx_id,
            rc,
            message: {
                xid,
                'GW-ERROR': {
                    code: e.code,
                    message: e.message,
                    endpointUrl,
                    httpStatus: (e.response && e.response.status) || null,
                    httpStatusText: (e.response && e.response.statusText) || null,
                    responseBody: e.response && e.response.data,
                },
            },
        });
    } finally {
        dumper(
            xid,
            task,
            endpointUrl,
            payload,
            response.config || axiosConfig,
            response,
            eToDump,
        );
    }
};