Blame view
lib/hit/prepaid-topup.js
5.08 KB
8c258ccd9
|
1 |
const MODULE_NAME = 'HIT.PREPAID-TOPUP'; |
d4661aa84
|
2 |
|
56a753a4a
|
3 |
const axios = require('axios'); |
d4661aa84
|
4 |
const urljoin = require('url-join'); |
442388f0b
|
5 |
const querystring = require('querystring'); |
d4661aa84
|
6 7 |
const config = require('komodo-sdk/config'); |
3d8701130
|
8 |
const logger = require('tektrans-logger'); |
d4661aa84
|
9 10 11 12 |
const report = require('../report/prepaid'); const translateRc = require('../translate-rc'); const composeCallbackUrl = require('./compose-callback-url'); |
385d8ff59
|
13 |
const dumpReqRes = require('./dump-req-res'); |
8c258ccd9
|
14 |
const axiosErrorIsSafe = require('./axios-error-is-safe'); |
d4661aa84
|
15 |
|
18291ea80
|
16 |
module.exports = async (xid, task) => { |
385d8ff59
|
17 18 |
logger.verbose(`${MODULE_NAME} 2272F01F: Processing task`, { xid, |
385d8ff59
|
19 20 |
task, }); |
d4661aa84
|
21 22 23 24 25 26 |
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, |
5eccae055
|
27 |
reverse_url: composeCallbackUrl(xid, task, false), |
d4661aa84
|
28 |
}; |
8c258ccd9
|
29 |
const endpointUrl = urljoin(config.partner.url, '/topup'); |
442388f0b
|
30 31 32 33 34 |
const fullEndpointUrl = urljoin( endpointUrl, [ '?', querystring.stringify(params), |
da236d0f7
|
35 |
].join(''), |
442388f0b
|
36 |
); |
385d8ff59
|
37 |
let lastResponse = null; |
d4661aa84
|
38 39 40 |
try { logger.verbose(`${MODULE_NAME} 4AAD4F99: Going to HIT prepaid endpoint`, { xid, |
d4661aa84
|
41 42 |
endpointUrl, params, |
249d4d6a2
|
43 44 45 |
fullEndpointUrl: (!config.partner.method || config.partner.method === 'GET') ? fullEndpointUrl : endpointUrl, |
d4661aa84
|
46 |
}); |
4231c7ef0
|
47 48 49 50 51 52 |
report(xid, { trx_id: task.trx_id, rc: '68', message: { xid, msg: 'Sending request to partner', |
249d4d6a2
|
53 |
method: config.partner.method || 'GET', |
4231c7ef0
|
54 55 56 |
endpointUrl, }, }); |
9e5ff9470
|
57 58 59 |
let response; if (config.partner.method === 'POST') { response = await axios.post(endpointUrl, new URLSearchParams(params), { |
249d4d6a2
|
60 61 62 63 |
headers: { 'User-Agent': 'KOMODO-GW-HTTPGETX', }, timeout: config.partner.hit_timeout_ms || 60 * 1000, |
9e5ff9470
|
64 65 66 67 68 69 70 71 72 73 |
}); } else if (config.partner.method === 'POST-JSON') { response = await axios.post(endpointUrl, params, { headers: { 'User-Agent': 'KOMODO-GW-HTTPGETX', }, timeout: config.partner.hit_timeout_ms || 60 * 1000, }); } else { response = await axios.get(endpointUrl, { |
249d4d6a2
|
74 75 76 77 78 79 |
headers: { 'User-Agent': 'KOMODO-GW-HTTPGETX', }, timeout: config.partner.hit_timeout_ms || 60 * 1000, params, }); |
9e5ff9470
|
80 |
} |
d4661aa84
|
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
if (!response) { const e = new Error(`${MODULE_NAME} 8CF4E04D: Empty response`); e.rc = '68'; e.response = response; throw e; } if (!response.data) { const e = new Error(`${MODULE_NAME} E72B5A53: Empty response data`); e.rc = '68'; e.response = response; throw e; } if (typeof response.data !== 'object') { const e = new Error(`${MODULE_NAME} 507680AB: Response data is not a JSON`); e.rc = '68'; e.response = response; throw e; } |
385d8ff59
|
102 |
lastResponse = response; |
d4661aa84
|
103 104 105 106 107 108 109 110 111 112 |
logger.verbose(`${MODULE_NAME} E51AFBBA: Got a direct response`, { xid, responseBody: response.data, }); report(xid, { trx_id: task.trx_id, rc: response.data.rc ? translateRc[response.data.rc] || response.data.rc : '68', sn: response.data.sn || null, |
1e63f3741
|
113 114 |
amount: Number(response.data.amount) || undefined, balance: Number(response.data.ending_balance) || undefined, |
d4661aa84
|
115 116 117 |
message: { xid, 'DIRECT-RESPONSE': response.data, |
d4661aa84
|
118 119 120 |
}, }); } catch (e) { |
8c258ccd9
|
121 122 123 |
const rc = e.rc || (axiosErrorIsSafe(e) && '91') || '68'; |
d4661aa84
|
124 |
|
8391f7809
|
125 126 127 128 129 130 |
logger.warn(`${MODULE_NAME} 8E8E49F5: Exception`, { xid, eCode: e.code, eMessage: e.message, eRc: e.rc, rc, |
8391f7809
|
131 132 133 |
responseHttpStatus: e.response && e.response.status, responseBody: e.response && e.response.data, }); |
385d8ff59
|
134 |
lastResponse = e.response; |
d4661aa84
|
135 136 137 138 139 140 141 142 143 144 145 |
report(xid, { 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, }, |
d4661aa84
|
146 147 |
}, }); |
385d8ff59
|
148 149 150 151 |
} finally { dumpReqRes( xid, task, |
249d4d6a2
|
152 |
config.partner.method || 'GET', |
385d8ff59
|
153 154 155 156 157 |
endpointUrl, params, lastResponse && lastResponse.data, lastResponse && lastResponse.status, lastResponse, |
8c258ccd9
|
158 |
false, |
385d8ff59
|
159 |
); |
d4661aa84
|
160 161 |
} }; |