const MODULE_NAME = 'HIT.POSTPAID';
const axios = require('axios').default;
const urljoin = require('url-join');
const uniqid = require('uniqid');
const config = require('komodo-sdk/config');
const logger = require('tektrans-logger');
const translateRc = require('../translate-rc');
const composeCallbackUrl = require('./compose-callback-url');
const dumpReqRes = require('./dump-req-res');
const report = require('../report/postpaid');
const axiosErrorIsSafe = require('./axios-error-is-safe');
module.exports = async (task, isPay) => {
const xid = uniqid();
const hitType = isPay ? 'PAY' : 'INQUIRY';
logger.verbose(`${MODULE_NAME} 0EDCEB4F: Processing task`, {
xid,
hitType,
task,
});
const params = {
request_id: task.trx_id,
terminal_name: config.partner.terminal_name,
password: config.partner.password,
destination: task.destination,
product_name: task.remote_product,
reverse_url: composeCallbackUrl(xid, task, true),
};
const endpointUrl = urljoin(
config.partner.url,
isPay ? 'pay' : 'inquiry',
);
let lastResponse = null;
try {
logger.verbose(`${MODULE_NAME} EFCF6C2A: Going to HIT POSTPAID endpoint`, {
xid,
endpointUrl,
params,
});
const response = await axios.get(endpointUrl, {
headers: {
'User-Agent': 'KOMODO-GW-HTTPGETX',
},
timeout: config.partner.hit_timeout_ms || 30 * 1000,
params,
});
if (!response) {
const e = new Error(`${MODULE_NAME} 364AB160: Empty response`);
e.rc = isPay ? '68' : '90';
e.response = response;
throw e;
}
if (!response.data) {
const e = new Error(`${MODULE_NAME} E64BCD17: Empty response data`);
e.rc = isPay ? '68' : '90';
e.response = response;
throw e;
}
if (typeof response.data !== 'object') {
const e = new Error(`${MODULE_NAME} E64BCD17: Response data is not a JSON`);
e.rc = isPay ? '68' : '90';
e.response = response;
throw e;
}
lastResponse = response;
logger.verbose(`${MODULE_NAME} 924E4510: Got a direct response`, {
xid,
responseBody: response.data,
});
report(xid, {
command: response.data.command || hitType,
trx_id: task.trx_id,
rc: response.data.rc ? translateRc[response.data.rc] || response.data.rc
: '68',
sn: response.data.sn || null,
amount: Number(response.data.amount) || undefined,
amount_to_charge: Number(response.data.amount_to_charge) || undefined,
balance: Number(response.data.ending_balance)
|| Number(response.data.balance)
|| undefined,
base_bill_amount: (response.data.base_bill_amount) || undefined,
bill_count: Number(response.data.bill_count) || undefined,
message: {
xid,
'DIRECT-RESPONSE': response.data,
},
info: response.data.info || undefined,
detail: response.data.detail || undefined,
data: response.data.data || undefined,
struk: response.data.struk || undefined,
});
} catch (e) {
const rc = e.rc
|| (axiosErrorIsSafe(e) && '91')
|| (!isPay && '91')
|| '68';
logger.warn(`${MODULE_NAME} 57764852: Exception`, {
xid,
eCode: e.code,
eMessage: e.message,
eRc: e.rc,
rc,
responseHttpStatus: e.response && e.response.status,
responseBody: e.response && e.response.data,
});
lastResponse = e.response;
report(xid, {
command: hitType,
trx_id: task.trx_id,
rc,
message: {
xid,
'KOMODO-GW-ERROR': {
eCode: e.code,
eMessage: e.message,
responseHttpStatus: e.response && e.response.status,
responseBody: e.response && e.response.data,
},
},
});
} finally {
dumpReqRes(
xid,
task,
'GET',
endpointUrl,
params,
lastResponse && lastResponse.data,
lastResponse && lastResponse.status,
lastResponse,
false,
);
}
};