buy-to-sds.js 1.52 KB
const MODULE_NAME = 'ACTIONS.BUY-TO-SDS';

const logger = require('tektrans-logger');
const axios = require('axios').default;

const config = require('../config');

const client = axios.create({
    baseURL: config.sds.url,
    timeout: config.sds.request_timeout_ms,
});

/**
 * request buy to sds
 *
 * @param {string} xid
 * @param {string} requestId
 * @param {string} destination
 * @param {string} productName
 * @param {number} quantity
 */
module.exports = async (
    xid,
    requestId,
    destination,
    productName,
    quantity,
) => {
    logger.verbose(`${MODULE_NAME} 4AD4DF41: buy to sds`, {
        requestId, destination, productName, quantity, xid,
    });

    const params = {
        trxid: requestId,
        username: config.sds.username,
        password: config.sds.password,
        number: destination,
        product: productName,
        amount: quantity,
        payment: config.sds.payment,
    };

    try {
        const response = await client.get('/request', {
            params,
        });
        if (!response) {
            throw new Error(`${MODULE_NAME} 6CE3E06E: Empty response sds`);
        }
        if (!response.data) {
            throw new Error(`${MODULE_NAME} F236476F: Empty response sds`);
        }

        return response.data;
    } catch (err) {
        logger.warn(`${MODULE_NAME} 48BAA6B7: Exception`, {
            xid,
            requestId,
            destination,
            productName,
            quantity,
            message: err.message,
        });
        throw err;
    }
};