handler-postpaid.js 2.01 KB
const MODULE_NAME = 'CALLBACK.HANDLER-POSTPAID';

const logger = require('tektrans-logger');
const report = require('../report/postpaid');
const getFromReq = require('../get-from-params-body-qs');
const translateRc = require('../translate-rc');

module.exports = (req, res) => {
    const { xid } = res.locals;
    res.json({
        status: 'OK',
        error: null,
        ts: new Date(),
        xid,
    });

    const reportCommand = getFromReq(req, 'command');
    if (!reportCommand) {
        logger.warn(`${MODULE_NAME} C7A4A26C: Unknown command`, { xid });
        return;
    }

    const isInquiry = reportCommand === 'INQUIRY';

    const rcFromRequest = getFromReq(req, 'rc');
    const translatedRc = rcFromRequest && (translateRc[rcFromRequest] || rcFromRequest);
    logger.verbose(`${MODULE_NAME} 475DA596: RC translated`, {
        xid,
        reportCommand,
        rcFromRequest,
        translatedRc,
    });

    const struk = getFromReq(req, 'struk');
    if (struk) {
        logger.verbose(`${MODULE_NAME} 2E480E83: Got a struk`, {
            xid,
            typeofStruk: typeof struk,
        });
    }

    report(xid, {
        command: reportCommand,
        trx_id: getFromReq(req, 'request_id'),
        rc: translatedRc
            || (isInquiry && '90')
            || '68',
        sn: getFromReq(req, 'sn'),
        amount: Number(getFromReq(req, 'amount')) || undefined,
        balance: Number(getFromReq(req, 'ending_balance')) || undefined,

        amount_to_charge: (isInquiry && Number(getFromReq(req, 'amount_to_charge')))
            || undefined,
        bill_count: Number(getFromReq(req, 'bill_count')) || undefined,
        base_bill_amount: Number(getFromReq(req, 'bill_amount')) || Number(getFromReq(req, 'base_bill_amount')) || undefined,

        message: {
            xid,
            CALLBACK: {
                ip: req.ip,
                method: req.method,
                body: req.body,
                qs: req.query,
            },
        },

        struk: struk || undefined,
    });
};