partner.js 1.85 KB
const MODULE_NAME = 'PARTNER';

const uniqid = require('uniqid');

const logger = require('tektrans-logger');
const config = require('komodo-sdk/config');
const { report } = require('komodo-sdk/gateway/pull');

const parseRemoteProduct = require('./parse-remote-product');

exports.buy = (task, callerXid) => {
    const xid = callerXid || uniqid();

    logger.info(`${MODULE_NAME} 1AEA18CE: Processing task`, {
        xid, task,
    });

    const remoteProduct = parseRemoteProduct(task.remote_product);

    logger.verbose(`${MODULE_NAME} 6022C627: Remote product parsed`, { xid, remoteProduct });

    if (config.only_direct_response || remoteProduct.ONLY_DIRECT_RESPONSE) {
        logger.verbose(`${MODULE_NAME} 6CB89503: Sending final direct response`, { xid });
        report({
            trx_id: task.trx_id,
            rc: remoteProduct.RC || '00',
            amount: remoteProduct.AMOUNT || null,
            message: {
                xid,
                msg: 'SELESAI',
            },
        });

        return;
    }

    logger.verbose(`${MODULE_NAME} 207C6F31: Sending intermediate report`, { xid });

    report({
        trx_id: task.trx_id,
        rc: '68',
        amount: remoteProduct.AMOUNT || null,
        message: {
            xid,
            msg: 'DIPROSES',
        },
    });

    const delay = Math.floor(
        Math.random() * (config.max_wait_ms_before_callback || 3000),
    );

    logger.verbose(`${MODULE_NAME} 091163B7: Will send callback`, {
        xid,
        delay,
    });

    setTimeout(() => {
        logger.verbose(`${MODULE_NAME} 685B89B9: Sending final callback`, { xid });

        report({
            trx_id: task.trx_id,
            rc: remoteProduct.RC,
            amount: remoteProduct.AMOUNT || null,
            message: {
                xid,
                msg: 'SELESAI',
            },
        }, xid);
    }, delay);
};