Commit 56ec0ab1f742a3313a7f611a1a08101c534cce6d
1 parent
9693703099
Exists in
master
and in
1 other branch
Tune on callback-sender
Showing 2 changed files with 9 additions and 7 deletions Inline Diff
lib/core-callback/index.js
1 | const MODULE_NAME = 'CORE-CALLBACK'; | ||
2 | |||
1 | const DEFAULT_LISTENER_FROM_CORE = 25613; | 3 | const DEFAULT_LISTENER_FROM_CORE = 25613; |
2 | 4 | ||
3 | const express = require('express'); | 5 | const express = require('express'); |
4 | const config = require('komodo-sdk/config'); | 6 | const config = require('komodo-sdk/config'); |
5 | const logger = require('komodo-sdk/logger'); | 7 | const logger = require('komodo-sdk/logger'); |
6 | const middlewareCommon = require('../middlewares/common'); | 8 | const middlewareCommon = require('../middlewares/common'); |
7 | const sender = require('./sender'); | 9 | const sender = require('./sender'); |
8 | 10 | ||
9 | const app = express(); | 11 | const app = express(); |
10 | 12 | ||
11 | app.use(express.json({ extended: true })); | 13 | app.use(express.json({ extended: true })); |
12 | app.use(express.urlencoded({ extended: true })); | 14 | app.use(express.urlencoded({ extended: true })); |
13 | 15 | ||
14 | app.use((req, res, next) => { | 16 | app.use((req, res, next) => { |
15 | res.locals.httpgetx_subsystem = 'CORE-CALLBACK'; | 17 | res.locals.httpgetx_subsystem = MODULE_NAME; |
16 | next(); | 18 | next(); |
17 | }); | 19 | }); |
18 | 20 | ||
19 | app.use(middlewareCommon); | 21 | app.use(middlewareCommon); |
20 | 22 | ||
21 | app.use((req, res) => { | 23 | app.use((req, res) => { |
22 | res.end('OK'); | 24 | res.end('OK'); |
23 | sender(req.query, res.locals.xid); | 25 | sender(req.query, res.locals.xid); |
24 | }); | 26 | }); |
25 | 27 | ||
26 | const port = (config.listener && config.listener.core && config.listener.core.port) | 28 | const port = (config.listener && config.listener.core && config.listener.core.port) |
27 | || DEFAULT_LISTENER_FROM_CORE; | 29 | || DEFAULT_LISTENER_FROM_CORE; |
28 | 30 | ||
29 | app.listen(port, () => { | 31 | app.listen(port, () => { |
30 | logger.info(`Listen from CORE callback on port ${port}`); | 32 | logger.info(`${MODULE_NAME} 0375DC4E: Listen from CORE callback on port ${port}`); |
31 | }).on('error', (e) => { | 33 | }).on('error', (e) => { |
32 | logger.error(`Can not listen CORE callback on port ${port}. ${e.toString()}`); | 34 | logger.error(`${MODULE_NAME} A90E42D5: Can not listen CORE callback on port ${port}. ${e.toString()}`); |
33 | process.exit(1); | 35 | process.exit(1); |
34 | }); | 36 | }); |
35 | 37 |
lib/core-callback/sender.js
1 | const MODULE_NAME = 'CORE-CALLBACK-SENDER'; | 1 | const MODULE_NAME = 'CORE-CALLBACK-SENDER'; |
2 | const MAX_RETRY = 10; | 2 | const MAX_RETRY = 10; |
3 | const SLEEP_BEFORE_RETRY_MS = 60 * 1000; | 3 | const SLEEP_BEFORE_RETRY_MS = 60 * 1000; |
4 | 4 | ||
5 | const axios = require('axios').default; | 5 | const axios = require('axios').default; |
6 | const logger = require('komodo-sdk/logger'); | 6 | const logger = require('komodo-sdk/logger'); |
7 | 7 | ||
8 | const sleep = require('../sleep'); | 8 | const sleep = require('../sleep'); |
9 | const urlConcatQs = require('../url-concat-qs'); | 9 | const urlConcatQs = require('../url-concat-qs'); |
10 | 10 | ||
11 | const sender = async (data, xid, retry) => { | 11 | const sender = async (data, xid, retry) => { |
12 | if (!data.reverse_url) return; | 12 | if (!data.reverse_url) return; |
13 | 13 | ||
14 | const params = { | 14 | const params = { |
15 | httpgetx_xid: xid, | 15 | httpgetx_xid: xid, |
16 | command: data.command, | 16 | command: data.command, |
17 | request_id: data.request_id && data.request_id.toString(), | 17 | request_id: data.request_id && data.request_id.toString(), |
18 | transaction_id: data.transaction_id && data.transaction_id.toString(), | 18 | transaction_id: data.transaction_id && data.transaction_id.toString(), |
19 | transaction_date: data.transaction_date, | 19 | transaction_date: data.transaction_date, |
20 | store_name: data.store_name, | 20 | store_name: data.store_name, |
21 | terminal_name: data.terminal_name, | 21 | terminal_name: data.terminal_name, |
22 | product_name: data.product_name, | 22 | product_name: data.product_name, |
23 | destination: data.destination, | 23 | destination: data.destination, |
24 | rc: data.rc, | 24 | rc: data.rc, |
25 | sn: data.sn, | 25 | sn: data.sn, |
26 | amount: Number(data.amount) || undefined, | ||
27 | ending_balance: Number(data.ending_balance) || undefined, | ||
26 | message: data.message, | 28 | message: data.message, |
27 | amount: data.amount, | ||
28 | ending_balance: data.ending_balance, | ||
29 | }; | 29 | }; |
30 | 30 | ||
31 | if (data.command === 'INQUIRY' && data.amount_to_charge) { | 31 | if (data.command === 'INQUIRY' && Number(data.amount_to_charge)) { |
32 | params.amount_to_charge = data.amount_to_charge; | 32 | params.amount_to_charge = Number(data.amount_to_charge); |
33 | } | 33 | } |
34 | 34 | ||
35 | const fullUrl = urlConcatQs(data.reverse_url, params); | 35 | const fullUrl = urlConcatQs(data.reverse_url, params); |
36 | logger.info(`${MODULE_NAME} 8B6A4CEC: Sending CORE-CALLBACK to PARTNER`, { | 36 | logger.info(`${MODULE_NAME} 8B6A4CEC: Sending CORE-CALLBACK to PARTNER`, { |
37 | xid, retry, params, fullUrl, | 37 | xid, retry, params, fullUrl, |
38 | }); | 38 | }); |
39 | 39 | ||
40 | try { | 40 | try { |
41 | const result = await axios.get(data.reverse_url, { | 41 | const result = await axios.get(data.reverse_url, { |
42 | params, | 42 | params, |
43 | timeout: 60 * 1000, | 43 | timeout: 60 * 1000, |
44 | }); | 44 | }); |
45 | 45 | ||
46 | logger.info(`${MODULE_NAME} 3641FBD7: CORE-CALLBACK has been sent to PARTNER successfully`, { | 46 | logger.info(`${MODULE_NAME} 3641FBD7: CORE-CALLBACK has been sent to PARTNER successfully`, { |
47 | xid, retry, fullUrl, body: result && result.data, | 47 | xid, retry, fullUrl, body: result && result.data, |
48 | }); | 48 | }); |
49 | } catch (e) { | 49 | } catch (e) { |
50 | logger.warn(`${MODULE_NAME} A1EC9E70: Failed on sending CORE-CALLBACK to PARTNER`, { | 50 | logger.warn(`${MODULE_NAME} A1EC9E70: Failed on sending CORE-CALLBACK to PARTNER`, { |
51 | xid, | 51 | xid, |
52 | retry, | 52 | retry, |
53 | maxRetry: MAX_RETRY, | 53 | maxRetry: MAX_RETRY, |
54 | errCode: e.code, | 54 | errCode: e.code, |
55 | errMessage: e.message, | 55 | errMessage: e.message, |
56 | reverseUrl: data.reverse_url, | 56 | reverseUrl: data.reverse_url, |
57 | fullUrl, | 57 | fullUrl, |
58 | httpStatus: e.response && e.response.status, | 58 | httpStatus: e.response && e.response.status, |
59 | body: e.response && e.response.data, | 59 | body: e.response && e.response.data, |
60 | }); | 60 | }); |
61 | 61 | ||
62 | if ((retry || 0) < MAX_RETRY) { | 62 | if ((retry || 0) < MAX_RETRY) { |
63 | await sleep(SLEEP_BEFORE_RETRY_MS); | 63 | await sleep(SLEEP_BEFORE_RETRY_MS); |
64 | logger.verbose(`${MODULE_NAME} D8958695: Going to retry sending CORE-CALLBACK TO PARTNER`, { | 64 | logger.verbose(`${MODULE_NAME} D8958695: Going to retry sending CORE-CALLBACK TO PARTNER`, { |
65 | xid, sleepTime: SLEEP_BEFORE_RETRY_MS, | 65 | xid, sleepTime: SLEEP_BEFORE_RETRY_MS, |
66 | }); | 66 | }); |
67 | sender(data, xid, (retry || 0) + 1); | 67 | sender(data, xid, (retry || 0) + 1); |
68 | } | 68 | } |
69 | } | 69 | } |
70 | }; | 70 | }; |
71 | 71 |