trx-status.js 4.04 KB
const MODULE_NAME = 'PARTNER-LISTENER.ROUTERS.TRX-STATUS';

const express = require('express');
const moment = require('moment');

const logger = require('tektrans-logger');

const coreapi = require('../../coreapi');
const dumper = require('../dumper');

const router = express.Router();
module.exports = router;

async function pageIndex(req, res) {
    const { xid } = res.locals;
    if (!req.body) req.body = {};

    if (!req.body.terminal_name && !req.query.terminal_name) {
        const msg = 'Parameter terminal_name tidak terdefinisi';
        res.json({
            httpgetx_xid: xid,
            error: true,
            message: msg,
        });

        dumper(xid, req, msg);
        return;
    }

    if (!req.body.password && !req.query.password) {
        const msg = 'Parameter password tidak terdefinisi';
        res.json({
            httpgetx_xid: xid,
            error: true,
            message: msg,
        });

        dumper(xid, req, msg);
        return;
    }

    if (!req.body.request_id && !req.query.request_id) {
        const msg = 'Parameter request_id tidak terdefinisi';
        res.json({
            httpgetx_xid: xid,
            error: true,
            message: msg,
        });

        dumper(xid, req, msg);
        return;
    }

    const remoteIp = req.ip.replace(/^::ffff:/, '');
    const askerTerminalName = `${req.body.terminal_name || req.query.terminal_name}@${remoteIp}`;

    const coreResponse = await coreapi({
        xid,
        path: '/trx-status/view',
        qs: {
            asker_terminal_name: askerTerminalName,
            asker_terminal_password: req.body.password || req.query.password,
            request_id: req.body.request_id || req.query.request_id,
        },
    });

    if (!coreResponse || !coreResponse.status) {
        const msg = 'Status transaksi tidak dapat diketahui karena suatu kesalahan pada sistem';

        logger.warn(`${MODULE_NAME} 9983DB34: ${msg}`, {
            xid,
            ip: req.ip,
            terminal_name: req.body.terminal_name || req.query.terminal_name,
            request_id: req.body.request_id || req.query.request_id,
        });

        const responseBody = {
            httpgetx_xid: xid,
            error: true,
            from_ip: remoteIp,
            terminal_name: req.body.terminal_name || req.query.terminal_name,
            full_terminal_name: askerTerminalName,
            password: req.body.password || req.query.password,
            message: msg,
        };

        res.json(responseBody);
        dumper(xid, req, responseBody);

        return;
    }

    const trx = coreResponse && coreResponse.result ? {
        id: coreResponse.result.id,
        request_id: coreResponse.result.request_id,
        store_id: coreResponse.result.store_id,
        store_name: coreResponse.result.store_name,
        terminal_id: coreResponse.result.terminal_id,
        terminal_name: coreResponse.result.terminal_name,

        created: moment(coreResponse.result.created).format('YYYY-MM-DD HH:mm:ss'),
        modified: moment(coreResponse.result.modified).format('YYYY-MM-DD HH:mm:ss'),

        product_name: coreResponse.result.product_name,
        destination: coreResponse.result.destination,
        amount: coreResponse.result.amount,
        ending_balance: coreResponse.result.ending_balance,
        rc: coreResponse.result.rc,
        message: coreResponse.result.reply_message,
        sn: coreResponse.result.sn,
    }
        : null;

    const result = {
        httpgetx_xid: xid,
        error: coreResponse.status !== 'OK',
        from_ip: remoteIp,
        terminal_name: req.body.terminal_name || req.query.terminal_name,
        full_terminal_name: askerTerminalName,
        message: coreResponse.message,
        trx_found: !!trx,
        trx,
        // original_trx: coreResponse.result,
    };

    res.json(result);

    logger.info(`${MODULE_NAME} 480C4BB0: Partner request responded`, {
        xid,
        processing_time_in_ms: new Date() - res.locals.x_http_request_ts,
        result,
    });

    dumper(xid, req, result);
}

router.get('/', pageIndex);