Commit 93ea90c3e74bcad946be24858cc194a50a136809
1 parent
af040c0dd0
Exists in
master
and in
1 other branch
CALLBACK-SENDER: data for postpaid trx
Showing 1 changed file with 52 additions and 14 deletions Inline Diff
lib/core-callback/sender.js
1 | const MODULE_NAME = 'CORE-CALLBACK-SENDER'; | 1 | const MODULE_NAME = 'CORE-CALLBACK.SENDER'; |
2 | const MAX_RETRY = 10; | ||
3 | const SLEEP_BEFORE_RETRY_MS = 60 * 1000; | ||
4 | 2 | ||
5 | const axios = require('axios').default; | 3 | const axios = require('axios').default; |
4 | const config = require('komodo-sdk/config'); | ||
6 | const logger = require('komodo-sdk/logger'); | 5 | const logger = require('komodo-sdk/logger'); |
7 | 6 | ||
7 | const HTTP_TIMEOUT = Number( | ||
8 | config.callback_sender && config.callback_sender.http_timeout_ms, | ||
9 | ) || 30 * 1000; | ||
10 | |||
11 | const SLEEP_BEFORE_RETRY_MS = Number( | ||
12 | config.callback_sender && config.callback_sender.sleep_before_retry_ms, | ||
13 | ) || 10 * 1000; | ||
14 | |||
15 | const MAX_RETRY = Number( | ||
16 | config.callback_sender && config.callback_sender.max_retry, | ||
17 | ) || 10; | ||
18 | |||
19 | logger.verbose(`${MODULE_NAME} 848B9104: Initialized`, { | ||
20 | HTTP_TIMEOUT, | ||
21 | SLEEP_BEFORE_RETRY_MS, | ||
22 | MAX_RETRY, | ||
23 | }); | ||
24 | |||
8 | const sleep = require('../sleep'); | 25 | const sleep = require('../sleep'); |
9 | const urlConcatQs = require('../url-concat-qs'); | 26 | const urlConcatQs = require('../url-concat-qs'); |
10 | 27 | ||
11 | const sender = async (data, xid, retry) => { | 28 | const sender = async (data, xid, retry) => { |
12 | if (!data.reverse_url) return; | 29 | if (!data.reverse_url) { |
30 | logger.verbose(`${MODULE_NAME} C4FF18FB: Ignoring missing reverse url`, { | ||
31 | xid, | ||
32 | dataFromCore: data, | ||
33 | }); | ||
34 | |||
35 | return; | ||
36 | } | ||
13 | 37 | ||
14 | const params = { | 38 | const params = { |
15 | httpgetx_xid: xid, | 39 | httpgetx_xid: xid, |
16 | command: data.command, | 40 | command: data.command, |
17 | request_id: data.request_id && data.request_id.toString(), | 41 | request_id: data.request_id && data.request_id.toString(), |
18 | transaction_id: data.transaction_id && data.transaction_id.toString(), | 42 | transaction_id: data.transaction_id && data.transaction_id.toString(), |
19 | transaction_date: data.transaction_date, | 43 | transaction_date: data.transaction_date, |
20 | store_name: data.store_name, | 44 | store_name: data.store_name, |
21 | terminal_name: data.terminal_name, | 45 | terminal_name: data.terminal_name, |
22 | product_name: data.product_name, | 46 | product_name: data.product_name, |
23 | destination: data.destination, | 47 | destination: data.destination, |
48 | |||
24 | rc: data.rc, | 49 | rc: data.rc, |
25 | sn: data.sn || undefined, | 50 | sn: data.sn || undefined, |
26 | amount: Number(data.amount) || undefined, | 51 | amount: Number(data.amount) || undefined, |
27 | ending_balance: Number(data.ending_balance) || undefined, | 52 | ending_balance: Number(data.ending_balance) || undefined, |
53 | |||
28 | message: data.message, | 54 | message: data.message, |
55 | |||
56 | bill_count: Number(data.bill_count) || undefined, | ||
57 | bill_amount: Number(data.bill_amount) || undefined, | ||
58 | fee_per_bill: Number(data.fee) || undefined, | ||
59 | fee_total: Number(data.fee_total) || undefined, | ||
60 | |||
61 | bill_detail: data.bill_detail || undefined, | ||
29 | }; | 62 | }; |
30 | 63 | ||
31 | if (data.command === 'INQUIRY' && Number(data.amount_to_charge)) { | 64 | if (data.command === 'INQUIRY' && data.amount_to_charge) { |
32 | params.amount_to_charge = Number(data.amount_to_charge); | 65 | params.amount_to_charge = data.amount_to_charge; |
33 | } | 66 | } |
34 | 67 | ||
35 | const fullUrl = urlConcatQs(data.reverse_url, params); | 68 | const fullUrl = urlConcatQs(data.reverse_url, params); |
36 | logger.info(`${MODULE_NAME} 8B6A4CEC: Sending CORE-CALLBACK to PARTNER`, { | 69 | logger.info(`${MODULE_NAME} 8B6A4CEC: Sending to PARTNER`, { |
37 | xid, retry, params, fullUrl, | 70 | xid, |
71 | retry, | ||
72 | fullUrl, | ||
38 | }); | 73 | }); |
39 | 74 | ||
40 | try { | 75 | try { |
41 | const result = await axios.get(data.reverse_url, { | 76 | const response = await axios.get(data.reverse_url, { |
42 | params, | 77 | params, |
43 | timeout: 60 * 1000, | 78 | timeout: HTTP_TIMEOUT, |
44 | }); | 79 | }); |
45 | 80 | ||
46 | logger.info(`${MODULE_NAME} 3641FBD7: CORE-CALLBACK has been sent to PARTNER successfully`, { | 81 | logger.info(`${MODULE_NAME} 3641FBD7: Has been sent to PARTNER successfully`, { |
47 | xid, retry, fullUrl, body: result && result.data, | 82 | xid, |
83 | retry, | ||
84 | httpStatus: response.status, | ||
85 | responseBody: response && response.data, | ||
48 | }); | 86 | }); |
49 | } catch (e) { | 87 | } catch (e) { |
50 | logger.warn(`${MODULE_NAME} A1EC9E70: Failed on sending CORE-CALLBACK to PARTNER`, { | 88 | logger.warn(`${MODULE_NAME} A1EC9E70: Failed on sending to PARTNER`, { |
51 | xid, | 89 | xid, |
52 | retry, | 90 | retry, |
53 | maxRetry: MAX_RETRY, | 91 | maxRetry: MAX_RETRY, |
54 | errCode: e.code, | 92 | errCode: e.code, |
55 | errMessage: e.message, | 93 | errMessage: e.message, |
56 | reverseUrl: data.reverse_url, | 94 | reverseUrl: data.reverse_url, |
57 | fullUrl, | 95 | fullUrl, |
58 | httpStatus: e.response && e.response.status, | 96 | httpStatus: e.response && e.response.status, |
59 | body: e.response && e.response.data, | 97 | responseBody: e.response && e.response.data, |
60 | }); | 98 | }); |
61 | 99 | ||
62 | if ((retry || 0) < MAX_RETRY) { | 100 | if ((retry || 0) < MAX_RETRY) { |
63 | await sleep(SLEEP_BEFORE_RETRY_MS); | 101 | await sleep(SLEEP_BEFORE_RETRY_MS); |
64 | logger.verbose(`${MODULE_NAME} D8958695: Going to retry sending CORE-CALLBACK TO PARTNER`, { | 102 | logger.verbose(`${MODULE_NAME} D8958695: Going to retry sending CORE-CALLBACK TO PARTNER`, { |
65 | xid, sleepTime: SLEEP_BEFORE_RETRY_MS, | 103 | xid, sleepTime: SLEEP_BEFORE_RETRY_MS, |
66 | }); | 104 | }); |
67 | sender(data, xid, (retry || 0) + 1); | 105 | sender(data, xid, (retry || 0) + 1); |
68 | } | 106 | } |
69 | } | 107 | } |
70 | }; | 108 | }; |
71 | 109 |