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