Commit e1cd1755a67945254342c15d45849bc92d5e6fab

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

Skip retry callback on http status exists

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