Commit 205954bc7cfd2d6bd7cd29221d687baea00c2ad5

Authored by Adhidarma Hadiwinoto
1 parent e7f5d2a50d
Exists in master and in 1 other branch dev

SENDER: callback forward customer_data

Showing 1 changed file with 1 additions and 0 deletions Inline Diff

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