diff --git a/lib/hit/postpaid-inquiry.js b/lib/hit/postpaid-inquiry.js deleted file mode 100644 index a65dadb..0000000 --- a/lib/hit/postpaid-inquiry.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = () => { - // -}; diff --git a/lib/hit/postpaid.js b/lib/hit/postpaid.js new file mode 100644 index 0000000..da436bf --- /dev/null +++ b/lib/hit/postpaid.js @@ -0,0 +1,152 @@ +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('komodo-sdk/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, 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, + ); + } +}; diff --git a/lib/partner-postpaid.js b/lib/partner-postpaid.js index a6122d6..ab78a13 100644 --- a/lib/partner-postpaid.js +++ b/lib/partner-postpaid.js @@ -1,10 +1,9 @@ -const hitInquiry = require('./hit/postpaid-inquiry'); -const hitPay = require('./hit/postpaid-pay'); +const hit = require('./hit/postpaid'); exports.inquiry = (task) => { - hitInquiry(task); + hit(task, false); }; exports.pay = (task) => { - hitPay(task); + hit(task, true); }; diff --git a/lib/report/postpaid.js b/lib/report/postpaid.js new file mode 100644 index 0000000..3cb4059 --- /dev/null +++ b/lib/report/postpaid.js @@ -0,0 +1,22 @@ +const MODULE_NAME = 'REPORT.POSTPAID'; + +const stringify = require('json-stringify-pretty-compact'); + +const logger = require('komodo-sdk/logger'); +const postpaidSdk = require('komodo-sdk-postpaid'); + +module.exports = (xid, data) => { + if (!data) return; + + logger.verbose(`${MODULE_NAME} 0682477A: Reporting to CORE`, { + xid, + data, + }); + + const reportData = JSON.parse(JSON.stringify(data)); + if (typeof reportData.message !== 'string') { + reportData.message = stringify(reportData.message); + } + + postpaidSdk.report(reportData, xid); +}; diff --git a/lib/report/prepaid.js b/lib/report/prepaid.js index dd1300e..2e57e43 100644 --- a/lib/report/prepaid.js +++ b/lib/report/prepaid.js @@ -13,10 +13,10 @@ module.exports = (xid, data) => { data, }); - const reportMsg = JSON.parse(JSON.stringify(data)); - if (typeof reportMsg.message !== 'string') { - reportMsg.message = stringify(reportMsg.message); + const reportData = JSON.parse(JSON.stringify(data)); + if (typeof reportData.message !== 'string') { + reportData.message = stringify(reportData.message); } - pull.report(reportMsg); + pull.report(reportData); };