Commit 53d11f9e416354d77f4c63c4ec1d9d8dd7cdfb63
1 parent
365bfd3557
Exists in
master
little bugfix
Showing 1 changed file with 1 additions and 1 deletions Inline Diff
partner-bayarkilat.js
1 | var request = require('request'); | 1 | var request = require('request'); |
2 | var url = require('url'); | 2 | var url = require('url'); |
3 | var winston = require('winston'); | 3 | var winston = require('winston'); |
4 | var xml2jsParser = require('xml2js').parseString; | 4 | var xml2jsParser = require('xml2js').parseString; |
5 | var redis = require('redis'); | 5 | var redis = require('redis'); |
6 | 6 | ||
7 | 7 | ||
8 | var config; | 8 | var config; |
9 | var aaa; | 9 | var aaa; |
10 | var callbackReport; | 10 | var callbackReport; |
11 | var logger; | 11 | var logger; |
12 | var redisClient; | 12 | var redisClient; |
13 | 13 | ||
14 | var maxCheckRetry = 20; | 14 | var maxCheckRetry = 20; |
15 | var delayBeforeCheckRetry = 30 * 1000; | 15 | var delayBeforeCheckRetry = 30 * 1000; |
16 | 16 | ||
17 | process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; | 17 | process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; |
18 | 18 | ||
19 | function createRedisClient(host, port) { | 19 | function createRedisClient(host, port) { |
20 | try { | 20 | try { |
21 | redisClient = redis.createClient(port, host); | 21 | redisClient = redis.createClient(port, host); |
22 | } catch(err) { | 22 | } catch(err) { |
23 | logger.warn("Error creating redis client to " + host + ':' + port); | 23 | logger.warn("Error creating redis client to " + host + ':' + port); |
24 | } | 24 | } |
25 | } | 25 | } |
26 | 26 | ||
27 | function start(_config, _callbackReport, options) { | 27 | function start(_config, _callbackReport, options) { |
28 | config = _config; | 28 | config = _config; |
29 | callbackReport = _callbackReport; | 29 | callbackReport = _callbackReport; |
30 | 30 | ||
31 | if (options && options.aaa) { | 31 | if (options && options.aaa) { |
32 | aaa = options.aaa; | 32 | aaa = options.aaa; |
33 | } | 33 | } |
34 | 34 | ||
35 | if (options && options.logger) { | 35 | if (options && options.logger) { |
36 | logger = options.logger; | 36 | logger = options.logger; |
37 | } else { | 37 | } else { |
38 | logger = new winston.Logger({ | 38 | logger = new winston.Logger({ |
39 | transports: [ | 39 | transports: [ |
40 | new (winston.transports.Console)() | 40 | new (winston.transports.Console)() |
41 | ] | 41 | ] |
42 | }); | 42 | }); |
43 | } | 43 | } |
44 | 44 | ||
45 | createRedisClient(config.globals.redis_host, config.globals.redis_port); | 45 | createRedisClient(config.globals.redis_host, config.globals.redis_port); |
46 | } | 46 | } |
47 | 47 | ||
48 | function createRequestOptions(methodName, task) { | 48 | function createRequestOptions(methodName, task) { |
49 | var partnerUrl = url.parse(config.h2h_out.partner); | 49 | var partnerUrl = url.parse(config.h2h_out.partner); |
50 | var product = prepareRemoteProductCode(task.remoteProduct); | 50 | var product = prepareRemoteProductCode(task.remoteProduct); |
51 | 51 | ||
52 | var destination = task.destination; | 52 | var destination = task.destination; |
53 | if (methodName == 'CHECKING') { | 53 | if (methodName == 'CHECKING') { |
54 | destination = paddingDestination(destination); | 54 | destination = paddingDestination(destination); |
55 | } | 55 | } |
56 | 56 | ||
57 | var options = { | 57 | var options = { |
58 | url: config.h2h_out.partner, | 58 | url: config.h2h_out.partner, |
59 | qs: { | 59 | qs: { |
60 | request: methodName + '*' | 60 | request: methodName + '*' |
61 | + task.requestId + '*' | 61 | + task.requestId + '*' |
62 | + product.product + '*' | 62 | + product.product + '*' |
63 | + product.productDetail + '*' | 63 | + product.productDetail + '*' |
64 | + destination + '*' | 64 | + destination + '*' |
65 | + product.nominal + '*' | 65 | + product.nominal + '*' |
66 | + '0*' | 66 | + '0*' |
67 | + config.h2h_out.noid + '*' | 67 | + config.h2h_out.noid + '*' |
68 | + config.h2h_out.userid + '*' | 68 | + config.h2h_out.userid + '*' |
69 | + config.h2h_out.password | 69 | + config.h2h_out.password |
70 | } | 70 | } |
71 | }; | 71 | }; |
72 | 72 | ||
73 | return options; | 73 | return options; |
74 | } | 74 | } |
75 | 75 | ||
76 | function topupCheck(task, retry) { | 76 | function topupCheck(task, retry) { |
77 | if (retry === null || retry === undefined) { | 77 | if (retry === null || retry === undefined) { |
78 | retry = maxCheckRetry + 1; | 78 | retry = maxCheckRetry + 1; |
79 | } | 79 | } |
80 | 80 | ||
81 | retry = retry - 1; | 81 | retry = retry - 1; |
82 | 82 | ||
83 | if (retry < 0) { | 83 | if (retry < 0) { |
84 | var message = 'Max retry check transaction retry exceeded'; | 84 | var message = 'Max retry check transaction retry exceeded'; |
85 | var response = { | 85 | var response = { |
86 | raw: message, | 86 | raw: message, |
87 | parsed: { | 87 | parsed: { |
88 | MESSAGE: message | 88 | MESSAGE: message |
89 | } | 89 | } |
90 | } | 90 | } |
91 | aaa.pushResponseToMongoDb(task, response, '68'); | 91 | aaa.pushResponseToMongoDb(task, response, '68'); |
92 | callbackReport(task.requestId, '68', message); | 92 | callbackReport(task.requestId, '68', message); |
93 | return; | 93 | return; |
94 | } | 94 | } |
95 | 95 | ||
96 | requestToPartner('CHECKING', task, retry); | 96 | requestToPartner('CHECKING', task, retry); |
97 | } | 97 | } |
98 | 98 | ||
99 | function topupRequest(task) { | 99 | function topupRequest(task) { |
100 | if (!aaa.isTodayTrx(task)) { | 100 | if (!aaa.isTodayTrx(task)) { |
101 | logger.warn('Maaf, transaksi beda hari tidak dapat dilakukan'); | 101 | logger.warn('Maaf, transaksi beda hari tidak dapat dilakukan'); |
102 | callbackReport(task.requestId, '68', 'Maaf, transaksi beda hari tidak dapat dilakukan'); | 102 | callbackReport(task.requestId, '68', 'Maaf, transaksi beda hari tidak dapat dilakukan'); |
103 | return; | 103 | return; |
104 | } | 104 | } |
105 | 105 | ||
106 | getTaskForDupeCheck(task, function(err, dupeTask) { | 106 | getTaskForDupeCheck(task, function(err, dupeTask) { |
107 | if (dupeTask) { | 107 | if (dupeTask) { |
108 | logger.info('Duplicate task detected, requesting topupCheck', {task: task}); | 108 | logger.info('Duplicate task detected, requesting topupCheck', {task: task}); |
109 | topupCheck(task); | 109 | topupCheck(task); |
110 | } else { | 110 | } else { |
111 | saveTaskForDupeCheck(task); | 111 | saveTaskForDupeCheck(task); |
112 | requestToPartner('PURCHASE', task); | 112 | requestToPartner('PURCHASE', task); |
113 | } | 113 | } |
114 | }); | 114 | }); |
115 | 115 | ||
116 | } | 116 | } |
117 | 117 | ||
118 | function requestToPartner(methodName, task, retry) { | 118 | function requestToPartner(methodName, task, retry) { |
119 | aaa.insertTaskToMongoDb(task); | 119 | aaa.insertTaskToMongoDb(task); |
120 | 120 | ||
121 | var options = createRequestOptions(methodName, task); | 121 | var options = createRequestOptions(methodName, task); |
122 | 122 | ||
123 | logger.info('Requesting to partner', {requestOption: options}); | 123 | logger.info('Requesting to partner', {requestOption: options}); |
124 | request(options, function(error, response, body) { | 124 | request(options, function(error, response, body) { |
125 | if (error) { | 125 | if (error) { |
126 | var msg = 'Error requesting to partner. ' + error; | 126 | var msg = 'Error requesting to partner. ' + error; |
127 | var rc = '68'; | 127 | var rc = '68'; |
128 | 128 | ||
129 | try { | 129 | try { |
130 | if (methodName == 'PURCHASE') { | 130 | if (methodName == 'PURCHASE') { |
131 | if (error.connect === true && (error.code === 'ETIMEDOUT' || error.code === 'ECONNREFUSED')) { | 131 | if (error.connect === true && (error.code === 'ETIMEDOUT' || error.code === 'ECONNREFUSED')) { |
132 | rc = '40'; | 132 | rc = '40'; |
133 | } | 133 | } |
134 | } | 134 | } |
135 | } | 135 | } |
136 | catch(e) { | 136 | catch(e) { |
137 | logger.warn('Parsing error exception on requestToPartner()'); | 137 | logger.warn('Parsing error exception on requestToPartner()'); |
138 | } | 138 | } |
139 | 139 | ||
140 | logger.warn(msg, {error: error}); | 140 | logger.warn(msg, {error: error}); |
141 | callbackReport(task.requestId, rc, msg); | 141 | callbackReport(task.requestId, rc, msg); |
142 | 142 | ||
143 | var _response = { | 143 | var _response = { |
144 | raw: JSON.stringify(msg: msg, error: error), | 144 | raw: JSON.stringify({msg: msg, error: error}), |
145 | parsed: { | 145 | parsed: { |
146 | MESSAGE: msg | 146 | MESSAGE: msg |
147 | error: error | 147 | error: error |
148 | } | 148 | } |
149 | } | 149 | } |
150 | aaa.pushResponseToMongoDb(task, _response, rc); | 150 | aaa.pushResponseToMongoDb(task, _response, rc); |
151 | 151 | ||
152 | if (rc == '68') { | 152 | if (rc == '68') { |
153 | setTimeout( | 153 | setTimeout( |
154 | topupCheck, | 154 | topupCheck, |
155 | delayBeforeCheckRetry, | 155 | delayBeforeCheckRetry, |
156 | task, | 156 | task, |
157 | retry | 157 | retry |
158 | ); | 158 | ); |
159 | } | 159 | } |
160 | 160 | ||
161 | return; | 161 | return; |
162 | } | 162 | } |
163 | 163 | ||
164 | if (response.statusCode != 200) { | 164 | if (response.statusCode != 200) { |
165 | var message = 'Partner response with http status code other that 200 (' + response.statusCode + ')'; | 165 | var message = 'Partner response with http status code other that 200 (' + response.statusCode + ')'; |
166 | 166 | ||
167 | logger.warn(message); | 167 | logger.warn(message); |
168 | callbackReport(task.requestId, '68', message); | 168 | callbackReport(task.requestId, '68', message); |
169 | 169 | ||
170 | var _response = { | 170 | var _response = { |
171 | raw: 'Partner response with http status code other that 200 (' + response.statusCode + ')', | 171 | raw: 'Partner response with http status code other that 200 (' + response.statusCode + ')', |
172 | parsed: { | 172 | parsed: { |
173 | MESSAGE: 'Partner response with http status code other that 200 (' + response.statusCode + ')', | 173 | MESSAGE: 'Partner response with http status code other that 200 (' + response.statusCode + ')', |
174 | responseHttpStatus: response.statusCode, | 174 | responseHttpStatus: response.statusCode, |
175 | responseBody: body, | 175 | responseBody: body, |
176 | } | 176 | } |
177 | } | 177 | } |
178 | aaa.pushResponseToMongoDb(task, _response, '68'); | 178 | aaa.pushResponseToMongoDb(task, _response, '68'); |
179 | 179 | ||
180 | setTimeout( | 180 | setTimeout( |
181 | topupCheck, | 181 | topupCheck, |
182 | delayBeforeCheckRetry, | 182 | delayBeforeCheckRetry, |
183 | task, | 183 | task, |
184 | retry | 184 | retry |
185 | ); | 185 | ); |
186 | 186 | ||
187 | return; | 187 | return; |
188 | } | 188 | } |
189 | 189 | ||
190 | logger.verbose('Got respose', {rawBody: body}); | 190 | logger.verbose('Got respose', {rawBody: body}); |
191 | parseResponse(body, task); | 191 | parseResponse(body, task); |
192 | }); | 192 | }); |
193 | } | 193 | } |
194 | 194 | ||
195 | function getSn(response) { | 195 | function getSn(response) { |
196 | try { | 196 | try { |
197 | var sn = response.xml.ket1[0]; | 197 | var sn = response.xml.ket1[0]; |
198 | return sn; | 198 | return sn; |
199 | } | 199 | } |
200 | catch (err) { | 200 | catch (err) { |
201 | return; | 201 | return; |
202 | } | 202 | } |
203 | } | 203 | } |
204 | 204 | ||
205 | function getHarga(response) { | 205 | function getHarga(response) { |
206 | try { | 206 | try { |
207 | var harga = response.xml.amount[0]; | 207 | var harga = response.xml.amount[0]; |
208 | return harga; | 208 | return harga; |
209 | } | 209 | } |
210 | catch (err) { | 210 | catch (err) { |
211 | return; | 211 | return; |
212 | } | 212 | } |
213 | } | 213 | } |
214 | 214 | ||
215 | function getSaldo(response) { | 215 | function getSaldo(response) { |
216 | try { | 216 | try { |
217 | var saldo = response.xml.saldo[0]; | 217 | var saldo = response.xml.saldo[0]; |
218 | return saldo; | 218 | return saldo; |
219 | } | 219 | } |
220 | catch (err) { | 220 | catch (err) { |
221 | return; | 221 | return; |
222 | } | 222 | } |
223 | } | 223 | } |
224 | 224 | ||
225 | function parseResponse(body, task) { | 225 | function parseResponse(body, task) { |
226 | xml2jsParser(body, function(err, response) { | 226 | xml2jsParser(body, function(err, response) { |
227 | if (err) { | 227 | if (err) { |
228 | logger.warn('Error parsing XML', {error: err, task: task, responseBody: body}); | 228 | logger.warn('Error parsing XML', {error: err, task: task, responseBody: body}); |
229 | 229 | ||
230 | var message = 'Error parsing XML. ' + err + '. ' + body; | 230 | var message = 'Error parsing XML. ' + err + '. ' + body; |
231 | 231 | ||
232 | var _response = { | 232 | var _response = { |
233 | raw: body, | 233 | raw: body, |
234 | parsed: { | 234 | parsed: { |
235 | MESSAGE: message | 235 | MESSAGE: message |
236 | } | 236 | } |
237 | } | 237 | } |
238 | aaa.pushResponseToMongoDb(task, _response, '68'); | 238 | aaa.pushResponseToMongoDb(task, _response, '68'); |
239 | 239 | ||
240 | callbackReport(task.requestId, '68', message); | 240 | callbackReport(task.requestId, '68', message); |
241 | return; | 241 | return; |
242 | } | 242 | } |
243 | 243 | ||
244 | logger.info('Got response', {response: response}); | 244 | logger.info('Got response', {response: response}); |
245 | 245 | ||
246 | var responseCode; | 246 | var responseCode; |
247 | var message; | 247 | var message; |
248 | 248 | ||
249 | try { | 249 | try { |
250 | responseCode = response.xml.response_code[0]; | 250 | responseCode = response.xml.response_code[0]; |
251 | message = response.xml.response_message[0]; | 251 | message = response.xml.response_message[0]; |
252 | } | 252 | } |
253 | catch(errGetParam) { | 253 | catch(errGetParam) { |
254 | logger.warn('Exception saat parsing hasil', {error: errGetParam, task: task, responseBody: body}); | 254 | logger.warn('Exception saat parsing hasil', {error: errGetParam, task: task, responseBody: body}); |
255 | 255 | ||
256 | var _response = { | 256 | var _response = { |
257 | raw: body, | 257 | raw: body, |
258 | parsed: { | 258 | parsed: { |
259 | MESSAGE: 'Exception saat parsing hasil. ' + errGetParam, | 259 | MESSAGE: 'Exception saat parsing hasil. ' + errGetParam, |
260 | xml: response.xml | 260 | xml: response.xml |
261 | } | 261 | } |
262 | } | 262 | } |
263 | aaa.pushResponseToMongoDb(task, _response, '68'); | 263 | aaa.pushResponseToMongoDb(task, _response, '68'); |
264 | 264 | ||
265 | callbackReport(task.requestId, '68', 'Exception saat parsing hasil. ' + errGetParam); | 265 | callbackReport(task.requestId, '68', 'Exception saat parsing hasil. ' + errGetParam); |
266 | return; | 266 | return; |
267 | } | 267 | } |
268 | 268 | ||
269 | var st24rc; | 269 | var st24rc; |
270 | if (parseInt(responseCode) == 0) { | 270 | if (parseInt(responseCode) == 0) { |
271 | st24rc = '00'; | 271 | st24rc = '00'; |
272 | } | 272 | } |
273 | else if (parseInt(responseCode) == '99') { | 273 | else if (parseInt(responseCode) == '99') { |
274 | st24rc = '68' | 274 | st24rc = '68' |
275 | } | 275 | } |
276 | else { | 276 | else { |
277 | st24rc = '40'; | 277 | st24rc = '40'; |
278 | } | 278 | } |
279 | 279 | ||
280 | var st24message = message; | 280 | var st24message = message; |
281 | if (responseCode) { | 281 | if (responseCode) { |
282 | st24message = responseCode + ' ' + st24message; | 282 | st24message = responseCode + ' ' + st24message; |
283 | } | 283 | } |
284 | 284 | ||
285 | if (st24rc == '00') { | 285 | if (st24rc == '00') { |
286 | var sn = getSn(response); | 286 | var sn = getSn(response); |
287 | 287 | ||
288 | if (sn) { | 288 | if (sn) { |
289 | st24message = 'SN=' + sn + ';' + st24message + '. SN=' + sn; | 289 | st24message = 'SN=' + sn + ';' + st24message + '. SN=' + sn; |
290 | } | 290 | } |
291 | } | 291 | } |
292 | 292 | ||
293 | var harga = getHarga(response); | 293 | var harga = getHarga(response); |
294 | if (harga) { | 294 | if (harga) { |
295 | st24message = st24message + '. Harga ' + harga; | 295 | st24message = st24message + '. Harga ' + harga; |
296 | } | 296 | } |
297 | 297 | ||
298 | var saldo = getSaldo(response); | 298 | var saldo = getSaldo(response); |
299 | if (saldo) { | 299 | if (saldo) { |
300 | st24message = st24message + '. Saldo ' + saldo; | 300 | st24message = st24message + '. Saldo ' + saldo; |
301 | aaa.updateBalance(saldo); | 301 | aaa.updateBalance(saldo); |
302 | } | 302 | } |
303 | 303 | ||
304 | var _response = { | 304 | var _response = { |
305 | raw: body, | 305 | raw: body, |
306 | parsed: { | 306 | parsed: { |
307 | MESSAGE: st24message, | 307 | MESSAGE: st24message, |
308 | xml: response.xml | 308 | xml: response.xml |
309 | } | 309 | } |
310 | } | 310 | } |
311 | 311 | ||
312 | aaa.pushResponseToMongoDb(task, _response, st24rc); | 312 | aaa.pushResponseToMongoDb(task, _response, st24rc); |
313 | 313 | ||
314 | callbackReport(task.requestId, st24rc, st24message); | 314 | callbackReport(task.requestId, st24rc, st24message); |
315 | }); | 315 | }); |
316 | } | 316 | } |
317 | 317 | ||
318 | function prepareRemoteProductCode(remoteProduct) { | 318 | function prepareRemoteProductCode(remoteProduct) { |
319 | var product = remoteProduct.split(','); | 319 | var product = remoteProduct.split(','); |
320 | 320 | ||
321 | if (product.length != 3) { | 321 | if (product.length != 3) { |
322 | return; | 322 | return; |
323 | } | 323 | } |
324 | 324 | ||
325 | return { | 325 | return { |
326 | product: product[0], | 326 | product: product[0], |
327 | productDetail: product[1], | 327 | productDetail: product[1], |
328 | nominal: product[2] | 328 | nominal: product[2] |
329 | } | 329 | } |
330 | } | 330 | } |
331 | 331 | ||
332 | function paddingDestination(destination, width) { | 332 | function paddingDestination(destination, width) { |
333 | if (!width) { | 333 | if (!width) { |
334 | width = 13; | 334 | width = 13; |
335 | } | 335 | } |
336 | 336 | ||
337 | if (destination.length > width) { | 337 | if (destination.length > width) { |
338 | return destination; | 338 | return destination; |
339 | } | 339 | } |
340 | 340 | ||
341 | var padder = "000000000000000000000000000"; | 341 | var padder = "000000000000000000000000000"; |
342 | 342 | ||
343 | return (padder + destination).slice(-1 * width); | 343 | return (padder + destination).slice(-1 * width); |
344 | } | 344 | } |
345 | 345 | ||
346 | function getTaskKeyForDupeCheck(task) { | 346 | function getTaskKeyForDupeCheck(task) { |
347 | return config.globals.gateway_name + '.dupecheck.reqId:' + task.requestId; | 347 | return config.globals.gateway_name + '.dupecheck.reqId:' + task.requestId; |
348 | } | 348 | } |
349 | 349 | ||
350 | function saveTaskForDupeCheck(task, cb) { | 350 | function saveTaskForDupeCheck(task, cb) { |
351 | var key = getTaskKeyForDupeCheck(task); | 351 | var key = getTaskKeyForDupeCheck(task); |
352 | 352 | ||
353 | logger.verbose('Saving ' + key + ' to redis for dupecheck protection'); | 353 | logger.verbose('Saving ' + key + ' to redis for dupecheck protection'); |
354 | 354 | ||
355 | redisClient.set(key, JSON.stringify(task), function() { | 355 | redisClient.set(key, JSON.stringify(task), function() { |
356 | redisClient.expire(key, 3600*24*7); | 356 | redisClient.expire(key, 3600*24*7); |
357 | if (cb) { | 357 | if (cb) { |
358 | cb(null, task); | 358 | cb(null, task); |
359 | } | 359 | } |
360 | }); | 360 | }); |
361 | } | 361 | } |
362 | 362 | ||
363 | function getTaskForDupeCheck(task, cb) { | 363 | function getTaskForDupeCheck(task, cb) { |
364 | var key = getTaskKeyForDupeCheck(task); | 364 | var key = getTaskKeyForDupeCheck(task); |
365 | 365 | ||
366 | redisClient.get(key, function(err, result) { | 366 | redisClient.get(key, function(err, result) { |
367 | if (err) { | 367 | if (err) { |
368 | cb(err, null); | 368 | cb(err, null); |
369 | return; | 369 | return; |
370 | } | 370 | } |
371 | 371 | ||
372 | var taskOnRedis = null; | 372 | var taskOnRedis = null; |
373 | try { | 373 | try { |
374 | taskOnRedis = JSON.parse(result); | 374 | taskOnRedis = JSON.parse(result); |
375 | } | 375 | } |
376 | catch(err1) { | 376 | catch(err1) { |
377 | cb(err1, null); | 377 | cb(err1, null); |
378 | return; | 378 | return; |
379 | } | 379 | } |
380 | 380 | ||
381 | cb(null, taskOnRedis); | 381 | cb(null, taskOnRedis); |
382 | }); | 382 | }); |
383 | } | 383 | } |
384 | 384 | ||
385 | exports.start = start; | 385 | exports.start = start; |
386 | exports.topupRequest = topupRequest; | 386 | exports.topupRequest = topupRequest; |
387 | exports.prepareRemoteProductCode = prepareRemoteProductCode; | 387 | exports.prepareRemoteProductCode = prepareRemoteProductCode; |
388 | exports.paddingDestination = paddingDestination; | 388 | exports.paddingDestination = paddingDestination; |
389 | 389 |