pay.js 3.81 KB
const axios = require('axios');
const express = require('express');
const coreUrl = require('komodo-sdk/core-url');

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

const getFromBodyQsParams = require('../../get-from-body-qs-params');
const ipv6ToIpv4 = require('../../ipv6-to-ipv4');
const dumper = require('../dumper');

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

const CORE_ENDPOINT = `${coreUrl}/postpaid2/pay`;

const mainHandler = async (req, res) => {
    const { xid } = res.locals;

    const requestId = (getFromBodyQsParams(req, 'request_id') || '').toString().trim();
    const terminalNameWithoutIp = (getFromBodyQsParams(req, 'terminal_name') || '').toString().trim();
    const terminalName = `${terminalNameWithoutIp}@${ipv6ToIpv4(req.ip)}`;
    const productName = (getFromBodyQsParams(req, 'product_name') || '').trim().toUpperCase();
    const destination = (getFromBodyQsParams(req, 'destination') || '').toString().trim();
    const password = getFromBodyQsParams(req, 'password');
    const reverseUrl = getFromBodyQsParams(req, 'reverse_url');

    if (!requestId || !terminalNameWithoutIp || !productName || !destination) {
        const msg = 'INVALID REQUEST. Missing request_id or terminal_name or product_name or destination.';
        res.end(msg);
        dumper(xid, req, msg);
        return;
    }

    const params = {
        origin: config.name,
        report_ip: config.listener.core.from_ip,
        report_port: config.listener.core.port || 25614,
        request_id: requestId,
        terminal_name: terminalName,
        product_name: productName,
        destination,
        terminal_password: password,
        reverse_url: reverseUrl,
    };

    logger.info('Forwarding PAY request to CORE', { xid, params });
    try {
        const result = await axios.get(CORE_ENDPOINT, {
            params,
            timeout: 10000,
        });

        if (!result || !result.data) {
            const newError = new Error('0D428E4C: Empty CORE PAY direct-response');
            logger.warn(newError.message, { xid });
            throw newError;
        }

        logger.verbose('Got PAY direct-response from CORE', {
            xid,
            coreResponse: result.data,
        });

        const resultForPartner = {
            httpgetx_xid: xid,
            command: result.data.command,
            request_id: result.data.request_id && result.data.request_id.toString(),
            transaction_id: result.data.transaction_id && result.data.transaction_id.toString(),
            transaction_date: result.data.transaction_date,
            store_name: result.data.store_name,
            terminal_name: result.data.terminal_name,
            product_name: result.data.product_name,
            destination: result.data.destination,
            rc: result.data.rc,
            sn: result.data.sn,
            message: result.data.message,
            amount: result.data.amount,
            ending_balance: result.data.ending_balance,
        };

        logger.verbose('Forwarding CORE PAY direct-response to partner', {
            xid,
            resultForPartner,
        });

        res.json(resultForPartner);
        dumper(xid, req, resultForPartner);
    } catch (e) {
        logger.warn('EXCEPTION on forwarding PAY request to CORE', {
            xid,
            errCode: e.code,
            errMessage: e.message,
        });

        const resultForPartner = {
            httpgetx_xid: xid,
            command: 'PAY',
            request_id: requestId,
            terminal_name: terminalName,
            product_name: productName,
            destination,
            rc: '68',
            message: 'CORE tidak merespon dengan benar, tidak dapat mengetahui status request anda',
        };

        dumper(xid, req, resultForPartner);
    }
};

router.all('/', mainHandler);