Commit b2d7e7957de1dfea07e2825f9f1626b2cfb0e8e0
1 parent
452ab95796
Exists in
master
task pada callbackReport
Showing 1 changed file with 8 additions and 3 deletions Inline Diff
partner-bnisp.js
1 | "use strict"; | 1 | "use strict"; |
2 | 2 | ||
3 | const http = require('http'); | 3 | const http = require('http'); |
4 | http.globalAgent.maxSockets = Infinity; | 4 | http.globalAgent.maxSockets = Infinity; |
5 | 5 | ||
6 | const request = require('request'); | 6 | const request = require('request'); |
7 | const resendDelay = require('sate24/resend-delay') | 7 | const resendDelay = require('sate24/resend-delay') |
8 | 8 | ||
9 | var config; | 9 | var config; |
10 | var aaa; | 10 | var aaa; |
11 | var logger; | 11 | var logger; |
12 | 12 | ||
13 | function start(options) { | 13 | function start(options) { |
14 | if (!options) { | 14 | if (!options) { |
15 | console.log('Undefined options, terminating....'); | 15 | console.log('Undefined options, terminating....'); |
16 | process.exit(1); | 16 | process.exit(1); |
17 | } | 17 | } |
18 | 18 | ||
19 | if (options.config) { | 19 | if (options.config) { |
20 | config = options.config; | 20 | config = options.config; |
21 | } else { | 21 | } else { |
22 | console.log('Undefined options.config, terminating....') | 22 | console.log('Undefined options.config, terminating....') |
23 | process.exit(1); | 23 | process.exit(1); |
24 | } | 24 | } |
25 | 25 | ||
26 | if (options.aaa) { | 26 | if (options.aaa) { |
27 | aaa = options.aaa; | 27 | aaa = options.aaa; |
28 | } else { | 28 | } else { |
29 | console.log('Undefined options.aaa, terminating....') | 29 | console.log('Undefined options.aaa, terminating....') |
30 | process.exit(1); | 30 | process.exit(1); |
31 | } | 31 | } |
32 | 32 | ||
33 | if (options && options.logger) { | 33 | if (options && options.logger) { |
34 | logger = options.logger; | 34 | logger = options.logger; |
35 | } else { | 35 | } else { |
36 | console.log('Undefined options.logger, terminating....') | 36 | console.log('Undefined options.logger, terminating....') |
37 | process.exit(1); | 37 | process.exit(1); |
38 | } | 38 | } |
39 | 39 | ||
40 | resendDelay.init({config: config, logger: logger, topupRequest: topupAdvice}); | 40 | resendDelay.init({config: config, logger: logger, topupRequest: topupAdvice}); |
41 | } | 41 | } |
42 | 42 | ||
43 | function callbackReport(requestId, rc, message, options) { | 43 | function callbackReport(requestId, rc, message, options) { |
44 | aaa.callbackReportWithPushToMongoDb(requestId, rc, message); | ||
45 | |||
46 | if (!options.task) { | ||
47 | return; | ||
48 | } | ||
49 | |||
44 | if (rc == '68') { | 50 | if (rc == '68') { |
45 | resendDelay.register(options.task); | 51 | resendDelay.register(options.task); |
46 | } else { | 52 | } else { |
47 | resendDelay.cancel(options.task) | 53 | resendDelay.cancel(options.task) |
48 | } | 54 | } |
49 | 55 | ||
50 | aaa.callbackReportWithPushToMongoDb(requestId, rc, message); | ||
51 | } | 56 | } |
52 | 57 | ||
53 | function splitRemoteProduct(remoteProduct) { | 58 | function splitRemoteProduct(remoteProduct) { |
54 | return remoteProduct.replace(/^\s+|\s+$/gm,'').split(/[\/\W]+/); | 59 | return remoteProduct.replace(/^\s+|\s+$/gm,'').split(/[\/\W]+/); |
55 | } | 60 | } |
56 | 61 | ||
57 | function topupRequest(task) { | 62 | function topupRequest(task) { |
58 | aaa.insertTaskToMongoDb(task); | 63 | aaa.insertTaskToMongoDb(task); |
59 | 64 | ||
60 | const remoteProduct = splitRemoteProduct(task.remoteProduct); | 65 | const remoteProduct = splitRemoteProduct(task.remoteProduct); |
61 | 66 | ||
62 | if (remoteProduct.length < 4) { | 67 | if (remoteProduct.length < 4) { |
63 | callbackReport(task.requestId, '40', 'INTERNAL_MSG: Invalid remoteProduct', task) | 68 | callbackReport(task.requestId, '40', 'INTERNAL_MSG: Invalid remoteProduct', task) |
64 | return; | 69 | return; |
65 | } | 70 | } |
66 | 71 | ||
67 | let pathParams = { | 72 | let pathParams = { |
68 | request_type: "purchase", | 73 | request_type: "purchase", |
69 | noid: config.h2h_out.noid, | 74 | noid: config.h2h_out.noid, |
70 | token: config.h2h_out.token, | 75 | token: config.h2h_out.token, |
71 | product: remoteProduct[0], | 76 | product: remoteProduct[0], |
72 | product_type: remoteProduct[1], | 77 | product_type: remoteProduct[1], |
73 | id_pel: task.destination, | 78 | id_pel: task.destination, |
74 | nominal: remoteProduct[2], | 79 | nominal: remoteProduct[2], |
75 | admin: remoteProduct[3], | 80 | admin: remoteProduct[3], |
76 | trace_id: task.requestId | 81 | trace_id: task.requestId |
77 | } | 82 | } |
78 | 83 | ||
79 | const requestOptions = { | 84 | const requestOptions = { |
80 | url: config.h2h_out.partner.replace(/\/+$/, '') + '/' + createUrlPath(pathParams).replace(/^\/+/, '') | 85 | url: config.h2h_out.partner.replace(/\/+$/, '') + '/' + createUrlPath(pathParams).replace(/^\/+/, '') |
81 | } | 86 | } |
82 | 87 | ||
83 | logger.verbose('Requeting to partner', {requestOptions: requestOptions}); | 88 | logger.verbose('Requeting to partner', {requestOptions: requestOptions}); |
84 | request(requestOptions, function(error, response, body) { | 89 | request(requestOptions, function(error, response, body) { |
85 | if (error) { | 90 | if (error) { |
86 | let rc = '68'; | 91 | let rc = '68'; |
87 | 92 | ||
88 | if (!error.syscall == 'connect') { | 93 | if (!error.syscall == 'connect') { |
89 | rc = '91'; | 94 | rc = '91'; |
90 | } | 95 | } |
91 | 96 | ||
92 | logger.warn('Error requesting to partner', {task: task, rc: rc, error: error}); | 97 | logger.warn('Error requesting to partner', {task: task, rc: rc, error: error}); |
93 | callbackReport(task.requestId, rc, 'INTERNAL_MSG: Error requesting to partner. ' + error, {task: task}); | 98 | callbackReport(task.requestId, rc, 'INTERNAL_MSG: Error requesting to partner. ' + error, {task: task}); |
94 | return; | 99 | return; |
95 | } | 100 | } |
96 | 101 | ||
97 | if (response.statusCode != 200) { | 102 | if (response.statusCode != 200) { |
98 | let rc = '68'; | 103 | let rc = '68'; |
99 | 104 | ||
100 | logger.warn('HTTP status code is not 200', {task: task, http_status_code: response.statusCode}); | 105 | logger.warn('HTTP status code is not 200', {task: task, http_status_code: response.statusCode}); |
101 | callbackReport(task.requestId, rc, 'INTERNAL_MSG: HTTP status code ' + response.statusCode, {task: task}); | 106 | callbackReport(task.requestId, rc, 'INTERNAL_MSG: HTTP status code ' + response.statusCode, {task: task}); |
102 | return; | 107 | return; |
103 | } | 108 | } |
104 | 109 | ||
105 | if (!body) { | 110 | if (!body) { |
106 | let rc = '68'; | 111 | let rc = '68'; |
107 | 112 | ||
108 | logger.warn('Missing response body', {task: task, responseBody: body}); | 113 | logger.warn('Missing response body', {task: task, responseBody: body}); |
109 | callbackReport(task.requestId, rc, 'INTERNAL_MSG: Missing response body', {task: task}); | 114 | callbackReport(task.requestId, rc, 'INTERNAL_MSG: Missing response body', {task: task}); |
110 | return; | 115 | return; |
111 | } | 116 | } |
112 | 117 | ||
113 | if (body.trim() == 'invalid specs') { | 118 | if (body.trim() == 'invalid specs') { |
114 | let rc = '40'; | 119 | let rc = '40'; |
115 | 120 | ||
116 | logger.warn('Invalid specs', {task: task, responseBody: body}); | 121 | logger.warn('Invalid specs', {task: task, responseBody: body}); |
117 | callbackReport(task.requestId, rc, body); | 122 | callbackReport(task.requestId, rc, body); |
118 | return; | 123 | return; |
119 | } | 124 | } |
120 | 125 | ||
121 | logger.verbose('Got response from partner', {task: task, responseBody: body}); | 126 | logger.verbose('Got response from partner', {task: task, responseBody: body}); |
122 | 127 | ||
123 | const responseData = parseResponseBody(body); | 128 | const responseData = parseResponseBody(body); |
124 | logger.verbose('Response body parsed as json value', {responseData: responseData}); | 129 | logger.verbose('Response body parsed as json value', {responseData: responseData}); |
125 | const data = responseDataProcessor(responseData); | 130 | const data = responseDataProcessor(responseData); |
126 | 131 | ||
127 | if (data.balance && aaa.updateBalance) { | 132 | if (data.balance && aaa.updateBalance) { |
128 | aaa.updateBalance(data.balance); | 133 | aaa.updateBalance(data.balance); |
129 | } | 134 | } |
130 | 135 | ||
131 | callbackReport(task.requestId, data.rc, data.combinedMessage, {task: task}); | 136 | callbackReport(task.requestId, data.rc, data.combinedMessage, {task: task}); |
132 | }) | 137 | }) |
133 | 138 | ||
134 | } | 139 | } |
135 | 140 | ||
136 | function topupAdvice(task) { | 141 | function topupAdvice(task) { |
137 | 142 | ||
138 | let pathParams = { | 143 | let pathParams = { |
139 | noid: config.h2h_out.noid, | 144 | noid: config.h2h_out.noid, |
140 | token: config.h2h_out.token, | 145 | token: config.h2h_out.token, |
141 | trace_id: task.requestId | 146 | trace_id: task.requestId |
142 | } | 147 | } |
143 | 148 | ||
144 | const requestOptions = { | 149 | const requestOptions = { |
145 | url: config.h2h_out.partner.replace(/\/+$/, '') + '/' + createUrlPathAdvice(pathParams).replace(/^\/+/, '') | 150 | url: config.h2h_out.partner.replace(/\/+$/, '') + '/' + createUrlPathAdvice(pathParams).replace(/^\/+/, '') |
146 | } | 151 | } |
147 | 152 | ||
148 | logger.verbose('Requeting advice to partner', {requestOptions: requestOptions}); | 153 | logger.verbose('Requeting advice to partner', {requestOptions: requestOptions}); |
149 | request(requestOptions, function(error, response, body) { | 154 | request(requestOptions, function(error, response, body) { |
150 | if (error) { | 155 | if (error) { |
151 | let rc = '68'; | 156 | let rc = '68'; |
152 | 157 | ||
153 | logger.warn('Error requesting to advice partner', {task: task, rc: rc, error: error}); | 158 | logger.warn('Error requesting to advice partner', {task: task, rc: rc, error: error}); |
154 | callbackReport(task.requestId, rc, 'INTERNAL_MSG: Error requesting advice to partner. ' + error, {task: task}); | 159 | callbackReport(task.requestId, rc, 'INTERNAL_MSG: Error requesting advice to partner. ' + error, {task: task}); |
155 | return; | 160 | return; |
156 | } | 161 | } |
157 | 162 | ||
158 | if (response.statusCode != 200) { | 163 | if (response.statusCode != 200) { |
159 | let rc = '68'; | 164 | let rc = '68'; |
160 | 165 | ||
161 | logger.warn('Advice HTTP status code is not 200', {task: task, http_status_code: response.statusCode}); | 166 | logger.warn('Advice HTTP status code is not 200', {task: task, http_status_code: response.statusCode}); |
162 | callbackReport(task.requestId, rc, 'INTERNAL_MSG: Advice HTTP status code ' + response.statusCode, {task: task}); | 167 | callbackReport(task.requestId, rc, 'INTERNAL_MSG: Advice HTTP status code ' + response.statusCode, {task: task}); |
163 | return; | 168 | return; |
164 | } | 169 | } |
165 | 170 | ||
166 | if (!body) { | 171 | if (!body) { |
167 | let rc = '68'; | 172 | let rc = '68'; |
168 | 173 | ||
169 | logger.warn('Missing advice response body', {task: task, responseBody: body}); | 174 | logger.warn('Missing advice response body', {task: task, responseBody: body}); |
170 | callbackReport(task.requestId, rc, 'INTERNAL_MSG: Missing advice response body', {task: task}); | 175 | callbackReport(task.requestId, rc, 'INTERNAL_MSG: Missing advice response body', {task: task}); |
171 | return; | 176 | return; |
172 | } | 177 | } |
173 | 178 | ||
174 | if (body.trim() == 'invalid specs') { | 179 | if (body.trim() == 'invalid specs') { |
175 | logger.warn('Invalid specs', {task: task, responseBody: body}); | 180 | logger.warn('Invalid specs', {task: task, responseBody: body}); |
176 | callbackReport(task.requestId, '68', body); | 181 | callbackReport(task.requestId, '68', body, {task: task}); |
177 | return; | 182 | return; |
178 | } | 183 | } |
179 | else if (body.trim() == 'data tidak ditemukan') { | 184 | else if (body.trim() == 'data tidak ditemukan') { |
180 | logger.warn(body, {task: task, responseBody: body}); | 185 | logger.warn(body, {task: task, responseBody: body}); |
181 | callbackReport(task.requestId, '40', body); | 186 | callbackReport(task.requestId, '40', body, {task: task}); |
182 | return; | 187 | return; |
183 | } | 188 | } |
184 | 189 | ||
185 | logger.verbose('Got advice response from partner', {task: task, responseBody: body}); | 190 | logger.verbose('Got advice response from partner', {task: task, responseBody: body}); |
186 | 191 | ||
187 | const responseData = parseResponseBody(body); | 192 | const responseData = parseResponseBody(body); |
188 | logger.verbose('Advice response body parsed as json value', {responseData: responseData}); | 193 | logger.verbose('Advice response body parsed as json value', {responseData: responseData}); |
189 | const data = responseDataProcessor(responseData); | 194 | const data = responseDataProcessor(responseData); |
190 | 195 | ||
191 | if (data.balance && aaa.updateBalance) { | 196 | if (data.balance && aaa.updateBalance) { |
192 | aaa.updateBalance(data.balance); | 197 | aaa.updateBalance(data.balance); |
193 | } | 198 | } |
194 | 199 | ||
195 | callbackReport(task.requestId, data.rc, data.combinedMessage, {task: task}); | 200 | callbackReport(task.requestId, data.rc, data.combinedMessage, {task: task}); |
196 | }) | 201 | }) |
197 | 202 | ||
198 | } | 203 | } |
199 | 204 | ||
200 | function createUrlPath(options) { | 205 | function createUrlPath(options) { |
201 | let urlPath = [ | 206 | let urlPath = [ |
202 | "get", | 207 | "get", |
203 | options.request_type || "purchase", | 208 | options.request_type || "purchase", |
204 | "json", | 209 | "json", |
205 | options.noid, | 210 | options.noid, |
206 | options.token, | 211 | options.token, |
207 | options.product, | 212 | options.product, |
208 | options.product_type, | 213 | options.product_type, |
209 | options.id_pel, | 214 | options.id_pel, |
210 | options.nominal || 0, | 215 | options.nominal || 0, |
211 | options.admin || 0, | 216 | options.admin || 0, |
212 | options.trace_id | 217 | options.trace_id |
213 | ].join('/'); | 218 | ].join('/'); |
214 | 219 | ||
215 | return '/' + urlPath; | 220 | return '/' + urlPath; |
216 | } | 221 | } |
217 | 222 | ||
218 | function createUrlPathAdvice(options) { | 223 | function createUrlPathAdvice(options) { |
219 | // pattern: /get/advice/json/[noid]/[token]/[traceid] | 224 | // pattern: /get/advice/json/[noid]/[token]/[traceid] |
220 | 225 | ||
221 | let urlPath = [ | 226 | let urlPath = [ |
222 | "get", | 227 | "get", |
223 | "advice", | 228 | "advice", |
224 | "json", | 229 | "json", |
225 | options.noid, | 230 | options.noid, |
226 | options.token, | 231 | options.token, |
227 | options.trace_id | 232 | options.trace_id |
228 | ].join('/'); | 233 | ].join('/'); |
229 | 234 | ||
230 | return '/' + urlPath; | 235 | return '/' + urlPath; |
231 | } | 236 | } |
232 | 237 | ||
233 | function parseResponseBody(body) { | 238 | function parseResponseBody(body) { |
234 | let data; | 239 | let data; |
235 | 240 | ||
236 | try { | 241 | try { |
237 | data = JSON.parse(body); | 242 | data = JSON.parse(body); |
238 | } | 243 | } |
239 | catch(e) { | 244 | catch(e) { |
240 | if (logger) { | 245 | if (logger) { |
241 | logger.warn('Exception on parsing result body: ' + e); | 246 | logger.warn('Exception on parsing result body: ' + e); |
242 | return; | 247 | return; |
243 | } | 248 | } |
244 | } | 249 | } |
245 | 250 | ||
246 | return data; | 251 | return data; |
247 | } | 252 | } |
248 | 253 | ||
249 | function responseDataProcessor(responseData) { | 254 | function responseDataProcessor(responseData) { |
250 | let retval = { | 255 | let retval = { |
251 | rc: '68', | 256 | rc: '68', |
252 | sn: '', | 257 | sn: '', |
253 | responseMessage: '', | 258 | responseMessage: '', |
254 | combinedMessage: '', | 259 | combinedMessage: '', |
255 | amount: 0, | 260 | amount: 0, |
256 | balance: 0, | 261 | balance: 0, |
257 | saldo: 0, | 262 | saldo: 0, |
258 | ts: '', | 263 | ts: '', |
259 | loadTime: 0, | 264 | loadTime: 0, |
260 | product: '', | 265 | product: '', |
261 | productType: '' | 266 | productType: '' |
262 | } | 267 | } |
263 | 268 | ||
264 | let combinedMessage = []; | 269 | let combinedMessage = []; |
265 | 270 | ||
266 | if (responseData.response_code == '0000') { | 271 | if (responseData.response_code == '0000') { |
267 | retval.rc = '00'; | 272 | retval.rc = '00'; |
268 | } | 273 | } |
269 | else if (responseData.response_code == '0010') { | 274 | else if (responseData.response_code == '0010') { |
270 | retval.rc = '14'; | 275 | retval.rc = '14'; |
271 | } | 276 | } |
272 | else if (responseData.response_code == '0021') { | 277 | else if (responseData.response_code == '0021') { |
273 | retval.rc = '14'; | 278 | retval.rc = '14'; |
274 | } | 279 | } |
275 | else if (['9990','0099','0068','0063','0005','9997','9999','9998','9990','0600','1003'].indexOf(responseData.response_code) >= 0) { | 280 | else if (['9990','0099','0068','0063','0005','9997','9999','9998','9990','0600','1003'].indexOf(responseData.response_code) >= 0) { |
276 | retval.rc = '68'; | 281 | retval.rc = '68'; |
277 | } | 282 | } |
278 | else { | 283 | else { |
279 | retval.rc = '40'; | 284 | retval.rc = '40'; |
280 | } | 285 | } |
281 | 286 | ||
282 | if (responseData.response_code) { | 287 | if (responseData.response_code) { |
283 | combinedMessage.push(responseData.response_code); | 288 | combinedMessage.push(responseData.response_code); |
284 | } | 289 | } |
285 | 290 | ||
286 | if (responseData.product) { | 291 | if (responseData.product) { |
287 | combinedMessage.push(responseData.product) | 292 | combinedMessage.push(responseData.product) |
288 | } | 293 | } |
289 | 294 | ||
290 | if (responseData.produk_tipe) { | 295 | if (responseData.produk_tipe) { |
291 | combinedMessage.push(responseData.produk_tipe) | 296 | combinedMessage.push(responseData.produk_tipe) |
292 | } | 297 | } |
293 | 298 | ||
294 | if (responseData.idpel) { | 299 | if (responseData.idpel) { |
295 | combinedMessage.push(responseData.idpel) | 300 | combinedMessage.push(responseData.idpel) |
296 | } | 301 | } |
297 | 302 | ||
298 | retval.responseMessage = responseData.response_message || ''; | 303 | retval.responseMessage = responseData.response_message || ''; |
299 | if (retval.responseMessage) { | 304 | if (retval.responseMessage) { |
300 | combinedMessage.push(retval.responseMessage); | 305 | combinedMessage.push(retval.responseMessage); |
301 | } | 306 | } |
302 | 307 | ||
303 | if (responseData.detail) { | 308 | if (responseData.detail) { |
304 | retval.sn = responseData.detail.voucherSerialNumber || ''; | 309 | retval.sn = responseData.detail.voucherSerialNumber || ''; |
305 | } | 310 | } |
306 | 311 | ||
307 | retval.amount = responseData.amount || 0; | 312 | retval.amount = responseData.amount || 0; |
308 | combinedMessage.push('Amount: ' + retval.amount); | 313 | combinedMessage.push('Amount: ' + retval.amount); |
309 | 314 | ||
310 | retval.balance = responseData.saldo || 0; | 315 | retval.balance = responseData.saldo || 0; |
311 | retval.saldo = retval.balance; | 316 | retval.saldo = retval.balance; |
312 | combinedMessage.push('Balance: ' + retval.balance); | 317 | combinedMessage.push('Balance: ' + retval.balance); |
313 | 318 | ||
314 | retval.ts = responseData.waktu || ''; | 319 | retval.ts = responseData.waktu || ''; |
315 | combinedMessage.push(retval.ts); | 320 | combinedMessage.push(retval.ts); |
316 | 321 | ||
317 | retval.loadTime = responseData.loadTime || ''; | 322 | retval.loadTime = responseData.loadTime || ''; |
318 | combinedMessage.push('Load time: ' + retval.loadTime); | 323 | combinedMessage.push('Load time: ' + retval.loadTime); |
319 | 324 | ||
320 | retval.combinedMessage = combinedMessage.join(' | '); | 325 | retval.combinedMessage = combinedMessage.join(' | '); |
321 | 326 | ||
322 | if (retval.sn) { | 327 | if (retval.sn) { |
323 | retval.combinedMessage = 'SN=' + retval.sn + '; ' + retval.combinedMessage; | 328 | retval.combinedMessage = 'SN=' + retval.sn + '; ' + retval.combinedMessage; |
324 | } | 329 | } |
325 | 330 | ||
326 | return retval; | 331 | return retval; |
327 | } | 332 | } |
328 | 333 | ||
329 | exports.start = start; | 334 | exports.start = start; |
330 | exports.topupRequest = topupRequest; | 335 | exports.topupRequest = topupRequest; |
331 | exports.topupAdvice = topupAdvice; | 336 | exports.topupAdvice = topupAdvice; |
332 | exports.createUrlPath = createUrlPath; | 337 | exports.createUrlPath = createUrlPath; |
333 | exports.createUrlPathAdvice = createUrlPathAdvice; | 338 | exports.createUrlPathAdvice = createUrlPathAdvice; |
334 | exports.parseResponseBody = parseResponseBody; | 339 | exports.parseResponseBody = parseResponseBody; |
335 | exports.responseDataProcessor = responseDataProcessor; | 340 | exports.responseDataProcessor = responseDataProcessor; |
336 | exports.splitRemoteProduct = splitRemoteProduct; | 341 | exports.splitRemoteProduct = splitRemoteProduct; |