sender.js
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const MODULE_NAME = 'CORE-CALLBACK-SENDER';
const MAX_RETRY = 10;
const SLEEP_BEFORE_RETRY_MS = 60 * 1000;
const axios = require('axios').default;
const logger = require('komodo-sdk/logger');
const sleep = require('../sleep');
const urlConcatQs = require('../url-concat-qs');
const sender = async (data, xid, retry) => {
if (!data.reverse_url) return;
const params = {
httpgetx_xid: xid,
command: data.command,
request_id: data.request_id && data.request_id.toString(),
transaction_id: data.transaction_id && data.transaction_id.toString(),
transaction_date: data.transaction_date,
store_name: data.store_name,
terminal_name: data.terminal_name,
product_name: data.product_name,
destination: data.destination,
rc: data.rc,
sn: data.sn,
message: data.message,
amount: data.amount,
ending_balance: data.ending_balance,
};
if (data.command === 'INQUIRY' && data.amount_to_charge) {
params.amount_to_charge = data.amount_to_charge;
}
const fullUrl = urlConcatQs(data.reverse_url, params);
logger.info(`${MODULE_NAME} 8B6A4CEC: Sending CORE-CALLBACK to PARTNER`, {
xid, retry, params, fullUrl,
});
try {
const result = await axios.get(data.reverse_url, {
params,
timeout: 60 * 1000,
});
logger.info(`${MODULE_NAME} 3641FBD7: CORE-CALLBACK has been sent to PARTNER successfully`, {
xid, retry, fullUrl, body: result && result.data,
});
} catch (e) {
logger.warn(`${MODULE_NAME} A1EC9E70: Failed on sending CORE-CALLBACK to PARTNER`, {
xid,
retry,
maxRetry: MAX_RETRY,
errCode: e.code,
errMessage: e.message,
reverseUrl: data.reverse_url,
fullUrl,
httpStatus: e.response && e.response.status,
body: e.response && e.response.data,
});
if ((retry || 0) < MAX_RETRY) {
await sleep(SLEEP_BEFORE_RETRY_MS);
logger.verbose(`${MODULE_NAME} D8958695: Going to retry sending CORE-CALLBACK TO PARTNER`, {
xid, sleepTime: SLEEP_BEFORE_RETRY_MS,
});
sender(data, xid, (retry || 0) + 1);
}
}
};
module.exports = sender;