Blame view
lib/core-callback/sender.js
6.11 KB
93ea90c3e
|
1 |
const MODULE_NAME = 'CORE-CALLBACK.SENDER'; |
f2c18879a
|
2 |
|
2e715a750
|
3 |
const axios = require('axios'); |
93ea90c3e
|
4 |
const config = require('komodo-sdk/config'); |
810fa18da
|
5 |
const logger = require('tektrans-logger'); |
f2c18879a
|
6 |
|
d05e75946
|
7 |
const dumper = require('./dumper/sender'); |
cd4feda87
|
8 |
const matrix = require('../matrix'); |
93ea90c3e
|
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
const HTTP_TIMEOUT = Number( config.callback_sender && config.callback_sender.http_timeout_ms, ) || 30 * 1000; const SLEEP_BEFORE_RETRY_MS = Number( config.callback_sender && config.callback_sender.sleep_before_retry_ms, ) || 10 * 1000; const MAX_RETRY = Number( config.callback_sender && config.callback_sender.max_retry, ) || 10; logger.verbose(`${MODULE_NAME} 848B9104: Initialized`, { HTTP_TIMEOUT, SLEEP_BEFORE_RETRY_MS, MAX_RETRY, }); |
05d016dd0
|
26 27 28 29 |
const axiosHeaders = { 'Content-Type': 'application/json', 'User-Agent': 'KOMODO-HTTPGETX callback sender', }; |
f2c18879a
|
30 31 32 33 |
const sleep = require('../sleep'); const urlConcatQs = require('../url-concat-qs'); const sender = async (data, xid, retry) => { |
93ea90c3e
|
34 35 36 37 38 39 40 41 |
if (!data.reverse_url) { logger.verbose(`${MODULE_NAME} C4FF18FB: Ignoring missing reverse url`, { xid, dataFromCore: data, }); return; } |
f2c18879a
|
42 43 44 |
const params = { httpgetx_xid: xid, |
635fc54bf
|
45 |
command: data.command, |
05d016dd0
|
46 |
|
f2c18879a
|
47 48 49 |
request_id: data.request_id && data.request_id.toString(), transaction_id: data.transaction_id && data.transaction_id.toString(), transaction_date: data.transaction_date, |
05d016dd0
|
50 |
|
f2c18879a
|
51 52 |
store_name: data.store_name, terminal_name: data.terminal_name, |
05d016dd0
|
53 |
|
f2c18879a
|
54 55 |
product_name: data.product_name, destination: data.destination, |
93ea90c3e
|
56 |
|
f2c18879a
|
57 |
rc: data.rc, |
050d92ffb
|
58 |
sn: data.sn || undefined, |
56ec0ab1f
|
59 60 |
amount: Number(data.amount) || undefined, ending_balance: Number(data.ending_balance) || undefined, |
93ea90c3e
|
61 |
|
f2c18879a
|
62 |
message: data.message, |
93ea90c3e
|
63 64 65 66 67 68 69 |
bill_count: Number(data.bill_count) || undefined, bill_amount: Number(data.bill_amount) || undefined, fee_per_bill: Number(data.fee) || undefined, fee_total: Number(data.fee_total) || undefined, bill_detail: data.bill_detail || undefined, |
1b9e31efc
|
70 |
struk: data.struk || undefined, |
f2c18879a
|
71 |
}; |
93ea90c3e
|
72 73 |
if (data.command === 'INQUIRY' && data.amount_to_charge) { params.amount_to_charge = data.amount_to_charge; |
9ced2cfdf
|
74 |
} |
05d016dd0
|
75 76 77 78 |
const isPostpaid = ['INQUIRY', 'PAY'].indexOf(data.command) >= 0; const isHttpPost = isPostpaid; const endpointUrl = isHttpPost ? data.reverse_url : urlConcatQs(data.reverse_url, params); |
93ea90c3e
|
79 80 |
logger.info(`${MODULE_NAME} 8B6A4CEC: Sending to PARTNER`, { xid, |
92282c155
|
81 |
retry: retry || 0, |
05d016dd0
|
82 83 84 |
isPostpaid, isHttpPost, endpointUrl, |
f2c18879a
|
85 |
}); |
d05e75946
|
86 87 |
let responseToDump; let errorResponseToDump; |
f2c18879a
|
88 |
try { |
05d016dd0
|
89 90 91 92 93 94 95 96 97 98 |
const response = isHttpPost ? await axios.post(data.reverse_url, params, { timeout: HTTP_TIMEOUT, headers: axiosHeaders, }) : await axios.get(data.reverse_url, { params, timeout: HTTP_TIMEOUT, headers: axiosHeaders, }); |
f2c18879a
|
99 |
|
d05e75946
|
100 |
responseToDump = response; |
b403448ad
|
101 |
matrix.callback_sender.sent += 1; |
e7f5d2a50
|
102 103 104 105 106 107 |
matrix.callback_sender.active_count += 1; matrix.callback_sender.active_sending[xid] = { ts: new Date(), trxId: data.trx_id, reverseUrl: data.reverse_url, }; |
cd4feda87
|
108 109 |
if (isPostpaid) { |
b403448ad
|
110 |
matrix.callback_sender.sent_using_post += 1; |
cd4feda87
|
111 |
} else { |
b403448ad
|
112 |
matrix.callback_sender.sent_using_get += 1; |
cd4feda87
|
113 |
} |
93ea90c3e
|
114 115 116 117 118 |
logger.info(`${MODULE_NAME} 3641FBD7: Has been sent to PARTNER successfully`, { xid, retry, httpStatus: response.status, responseBody: response && response.data, |
f2c18879a
|
119 120 |
}); } catch (e) { |
b403448ad
|
121 |
matrix.callback_sender.sent_failed += 1; |
dc2d0f90e
|
122 123 124 125 126 |
matrix.callback_sender.last_error = { xid, ts: new Date(), eCode: e.code, eMessage: e.message, |
e7f5d2a50
|
127 |
trxId: data.trx_id, |
dc2d0f90e
|
128 129 130 131 |
reverseUrl: data.reverse_url, httpStatus: e.response && e.response.status, responseBody: e.response && e.response.data, }; |
cd4feda87
|
132 |
|
d05e75946
|
133 134 |
responseToDump = e.response && e.response.data; errorResponseToDump = e; |
93ea90c3e
|
135 |
logger.warn(`${MODULE_NAME} A1EC9E70: Failed on sending to PARTNER`, { |
f2c18879a
|
136 137 138 139 140 141 |
xid, retry, maxRetry: MAX_RETRY, errCode: e.code, errMessage: e.message, reverseUrl: data.reverse_url, |
05d016dd0
|
142 |
endpointUrl, |
f2c18879a
|
143 |
httpStatus: e.response && e.response.status, |
93ea90c3e
|
144 |
responseBody: e.response && e.response.data, |
f2c18879a
|
145 |
}); |
e1cd1755a
|
146 147 148 149 150 151 152 |
if (e.response && e.response.status) { logger.verbose(`${MODULE_NAME} 10AE785C: Skip retry on http status presence`, { xid, httpStatus: e.response && e.response.status, }); return; } |
f2c18879a
|
153 154 |
if ((retry || 0) < MAX_RETRY) { await sleep(SLEEP_BEFORE_RETRY_MS); |
92282c155
|
155 |
|
f2c18879a
|
156 |
logger.verbose(`${MODULE_NAME} D8958695: Going to retry sending CORE-CALLBACK TO PARTNER`, { |
92282c155
|
157 158 159 |
xid, retried: retry, sleepTime: SLEEP_BEFORE_RETRY_MS, |
f2c18879a
|
160 |
}); |
92282c155
|
161 |
|
f2c18879a
|
162 163 |
sender(data, xid, (retry || 0) + 1); } |
e7f5d2a50
|
164 165 166 167 168 |
} finally { matrix.callback_sender.active_count -= 1; if (matrix.callback_sender.active_sending[xid]) { delete matrix.callback_sender.active_sending[xid]; } |
d05e75946
|
169 |
|
ae566890d
|
170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
if (config.listener.partner.webhook) { try { axios.post(config.listener.partner.webhook, { webhookType: 'KOMODO-CENTER-HTTPGETX.CORE-CALLBACK', body: params, }); } catch (e) { logger.warn(`${MODULE_NAME} F722520A: Exception on calling webhook`, { xid, eCode: e.code, eMessage: e.message || e.toString(), }); } } |
d05e75946
|
184 185 186 187 188 189 190 191 |
dumper( xid, isHttpPost ? 'POST' : 'GET', endpointUrl, params, responseToDump, errorResponseToDump, ); |
f2c18879a
|
192 193 194 195 |
} }; module.exports = sender; |