Commit 0696ad0aed472bd4c07fb41e5efaed4a0984dca8
1 parent
c03ef90c43
Exists in
master
penanganan saldo tidak cukup di SS
Showing 1 changed file with 67 additions and 64 deletions Side-by-side Diff
gentong.js
... | ... | @@ -32,18 +32,18 @@ function getRedisKey(timestamp) { |
32 | 32 | |
33 | 33 | function generateTimestamp(request_id) { |
34 | 34 | var ts = strftime('%F %T', new Date()); |
35 | - | |
35 | + | |
36 | 36 | var key = getRedisKey(ts); |
37 | 37 | redisClient.set(key, request_id); |
38 | 38 | redisClient.expire(key, 3600*48); |
39 | - | |
39 | + | |
40 | 40 | return ts; |
41 | 41 | } |
42 | 42 | |
43 | 43 | function topupRequest(task) { |
44 | 44 | var ts = strftime('%F %T', new Date()); |
45 | 45 | ts = generateTimestamp(task['requestId']); |
46 | - | |
46 | + | |
47 | 47 | var options = { |
48 | 48 | url: config.h2h_out.partner, |
49 | 49 | qs: { |
... | ... | @@ -61,52 +61,52 @@ function topupRequest(task) { |
61 | 61 | aaa.incrementTrxCount(); |
62 | 62 | aaa.incrementActiveTrxCount(); |
63 | 63 | } |
64 | - | |
64 | + | |
65 | 65 | request(options, function (error, response, body) { |
66 | 66 | aaa.decrementActiveTrxCount(); |
67 | - | |
67 | + | |
68 | 68 | if (error) { |
69 | - | |
69 | + | |
70 | 70 | var error_message = 'Error on http connection to gateway: ' + error; |
71 | 71 | logger.warn(error_message); |
72 | 72 | callbackReport(task['requestId'], '91', error_message); |
73 | 73 | return; |
74 | 74 | |
75 | - } | |
76 | - | |
75 | + } | |
76 | + | |
77 | 77 | if (response.statusCode != 200) { |
78 | - | |
78 | + | |
79 | 79 | var error_message = 'Gateway error, http response code: ' + response.statusCode; |
80 | 80 | logger.warn(error_message); |
81 | 81 | callbackReport(task['requestId'], '91', error_message); |
82 | 82 | return; |
83 | 83 | } |
84 | - | |
84 | + | |
85 | 85 | var responseCode = 40; |
86 | 86 | var responseMessage; |
87 | - | |
87 | + | |
88 | 88 | xml2js(body, function (err, result) { |
89 | 89 | if (err) { |
90 | 90 | logger.warn('Error parsing XML', {response_error: err, response_body: body}); |
91 | 91 | callbackReport(task['requestId'], '40', body); |
92 | 92 | return; |
93 | 93 | } |
94 | - | |
94 | + | |
95 | 95 | logger.info('Got direct response from request', {result: result}); |
96 | - | |
96 | + | |
97 | 97 | try { |
98 | 98 | responseMessage = result.direct_ack.info[0]; |
99 | - | |
99 | + | |
100 | 100 | if (result.direct_ack.request_status[0] == 'OK') { |
101 | 101 | responseCode = 68; |
102 | 102 | } else { |
103 | 103 | responseCode = 40; |
104 | - | |
104 | + | |
105 | 105 | var new_response_code = responseCodeFromMessage(responseMessage); |
106 | 106 | if (new_response_code) { |
107 | 107 | responseCode = new_response_code; |
108 | 108 | } |
109 | - | |
109 | + | |
110 | 110 | } |
111 | 111 | |
112 | 112 | } |
... | ... | @@ -115,11 +115,11 @@ function topupRequest(task) { |
115 | 115 | responseCode = 40; |
116 | 116 | responseMessage = 'Invalid xml response from gateway'; |
117 | 117 | } |
118 | - | |
118 | + | |
119 | 119 | callbackReport(task['requestId'], responseCode, responseMessage); |
120 | - | |
120 | + | |
121 | 121 | }); |
122 | - | |
122 | + | |
123 | 123 | }); |
124 | 124 | } |
125 | 125 | |
... | ... | @@ -128,11 +128,11 @@ function createRedisClient() { |
128 | 128 | } |
129 | 129 | |
130 | 130 | function paddingSN(sn, _config) { |
131 | - | |
131 | + | |
132 | 132 | if (!_config) { |
133 | 133 | _config = config; |
134 | 134 | } |
135 | - | |
135 | + | |
136 | 136 | if (_config.h2h_out.sn_min_length && (sn.length < Number(_config.h2h_out.sn_min_length))) { |
137 | 137 | sn = '0000000000000000' + sn; |
138 | 138 | sn = sn.slice(Number(_config.h2h_out.sn_min_length) * -1); |
... | ... | @@ -141,57 +141,57 @@ function paddingSN(sn, _config) { |
141 | 141 | } |
142 | 142 | |
143 | 143 | function parseSN(message, _config) { |
144 | - | |
144 | + | |
145 | 145 | if (!_config) { |
146 | 146 | _config = config; |
147 | 147 | } |
148 | - | |
148 | + | |
149 | 149 | var sn_regex = new RegExp(_config.h2h_out.sn_pattern); |
150 | 150 | var sn_match = message.match(sn_regex); |
151 | - | |
151 | + | |
152 | 152 | if (sn_match <= 0) { |
153 | 153 | logger.info('SN Not found: ' + message); |
154 | 154 | return ''; |
155 | 155 | } |
156 | - | |
156 | + | |
157 | 157 | var match_index = 0; |
158 | 158 | if (_config.h2h_out.sn_match_index) { |
159 | 159 | match_index = Number(_config.h2h_out.sn_match_index) |
160 | 160 | } |
161 | - | |
161 | + | |
162 | 162 | var sn = sn_match[match_index]; |
163 | - | |
163 | + | |
164 | 164 | if (_config.h2h_out.sn_remove_whitespace) { |
165 | 165 | sn = sn.replace(/\s/g, ''); |
166 | 166 | } |
167 | - | |
167 | + | |
168 | 168 | var sn_remove_patterns = _config.h2h_out.sn_remove_patterns.split(_config.h2h_out.sn_remove_patterns_separator); |
169 | - | |
169 | + | |
170 | 170 | var count = sn_remove_patterns.length; |
171 | - | |
171 | + | |
172 | 172 | for(var i = 0; i < count; i++) { |
173 | - | |
173 | + | |
174 | 174 | //sn = sn.replace(sn_remove_patterns[i], ''); |
175 | - | |
175 | + | |
176 | 176 | var re = new RegExp(sn_remove_patterns[i], 'g'); |
177 | 177 | sn = sn.replace(re, ''); |
178 | 178 | } |
179 | - | |
179 | + | |
180 | 180 | sn = paddingSN(sn, _config); |
181 | - | |
181 | + | |
182 | 182 | return sn.trim(); |
183 | 183 | } |
184 | 184 | |
185 | 185 | function createServer() { |
186 | 186 | var httpServer = http.createServer(function(request, response) { |
187 | - | |
187 | + | |
188 | 188 | var response_code = '68'; |
189 | 189 | var sn = ''; |
190 | - | |
190 | + | |
191 | 191 | var qs = url.parse(request.url, true).query; |
192 | - | |
192 | + | |
193 | 193 | logger.info('Got reverse report from gateway', {qs: qs}); |
194 | - | |
194 | + | |
195 | 195 | if (qs.topup_status == 'S') { |
196 | 196 | response_code = '00'; |
197 | 197 | if (qs.sn && !config.h2h_out.force_parse_sn && !Number(config.h2h_out.force_parse_sn)) { |
... | ... | @@ -204,94 +204,94 @@ function createServer() { |
204 | 204 | if (config.h2h_out.sn_split_index) { |
205 | 205 | sn = splitSN(sn, config); |
206 | 206 | } |
207 | - | |
207 | + | |
208 | 208 | if (sn) { |
209 | 209 | sn = paddingSN(sn, config); |
210 | 210 | } |
211 | - | |
211 | + | |
212 | 212 | } else if (qs.topup_status == 'R') { |
213 | - | |
213 | + | |
214 | 214 | response_code = '40'; |
215 | 215 | |
216 | 216 | } |
217 | - | |
217 | + | |
218 | 218 | try { |
219 | - | |
219 | + | |
220 | 220 | var new_response_code = responseCodeFromMessage(qs.info); |
221 | 221 | if (new_response_code) { |
222 | 222 | response_code = new_response_code; |
223 | 223 | } |
224 | - | |
224 | + | |
225 | 225 | } |
226 | 226 | catch(err) { |
227 | 227 | logger.warn('Exception on parsing reverse report', {exception: err} ); |
228 | 228 | response_code = '40'; |
229 | 229 | } |
230 | - | |
230 | + | |
231 | 231 | message = qs.info; |
232 | 232 | //updateBalance(message); |
233 | 233 | if (sn) { |
234 | 234 | message = 'SN=' + sn + '; ' + message; |
235 | 235 | } |
236 | - | |
236 | + | |
237 | 237 | response.end('OK'); |
238 | - | |
238 | + | |
239 | 239 | var key = getRedisKey(qs.ts); |
240 | 240 | redisClient.get(key, function(err, request_id) { |
241 | 241 | if (err) { |
242 | 242 | logger.warn('Error when requesting request id for ts:' + qs.ts + ' (' + key + ')', {redis_error: err}); |
243 | 243 | return; |
244 | 244 | } |
245 | - | |
245 | + | |
246 | 246 | callbackReport(request_id, response_code, message); |
247 | 247 | }); |
248 | 248 | }); |
249 | - | |
249 | + | |
250 | 250 | httpServer.listen(config.h2h_out.listen_port, function() { |
251 | 251 | logger.info('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); |
252 | 252 | }); |
253 | 253 | } |
254 | 254 | |
255 | -function splitSN(sn, _config) { | |
255 | +function splitSN(sn, _config) { | |
256 | 256 | var sn_pieces = sn.split(' '); |
257 | - | |
257 | + | |
258 | 258 | if (sn_pieces.length <= 0) { |
259 | 259 | logger.info('Returning original SN because SN only has one element'); |
260 | 260 | return sn; |
261 | 261 | } |
262 | - | |
262 | + | |
263 | 263 | if (!_config.h2h_out.sn_split_index) { |
264 | 264 | logger.info('Returning original SN because config.h2h_out.sn_split_index undefined'); |
265 | 265 | return sn; |
266 | 266 | } |
267 | 267 | var sn_indexes = _config.h2h_out.sn_split_index.split(','); |
268 | - | |
268 | + | |
269 | 269 | logger.info('Split SN', {sn_pieces: sn_pieces, sn_indexes: sn_indexes}); |
270 | - | |
270 | + | |
271 | 271 | var _sn = ''; |
272 | - | |
272 | + | |
273 | 273 | var id_count = sn_indexes.length; |
274 | 274 | for(var i = 0; i < id_count; i++) { |
275 | - | |
275 | + | |
276 | 276 | var sn_index = sn_indexes[i]; |
277 | 277 | var sn_piece = sn_pieces[sn_index]; |
278 | - | |
278 | + | |
279 | 279 | if (sn_pieces[i]) { |
280 | 280 | _sn = _sn + sn_piece; |
281 | 281 | } else { |
282 | 282 | logger.warn('Undefined value on sn piece ' + sn_index); |
283 | 283 | } |
284 | 284 | } |
285 | - | |
285 | + | |
286 | 286 | _sn = _sn.trim(); |
287 | - | |
287 | + | |
288 | 288 | if (_sn) { |
289 | 289 | sn = _sn; |
290 | 290 | logger.info('Got new SN: ' + sn); |
291 | 291 | } else { |
292 | 292 | logger.warn('Got empty SN when using split SN. Use original SN'); |
293 | 293 | } |
294 | - | |
294 | + | |
295 | 295 | return sn; |
296 | 296 | } |
297 | 297 | |
... | ... | @@ -338,24 +338,27 @@ function responseCodeFromMessage(message) { |
338 | 338 | else if (message.toLowerCase().indexOf('saldo di account anda saat ini tidak mencukupi') >= 0) { |
339 | 339 | return '40'; |
340 | 340 | } |
341 | + else if (message.toLowerCase().indexOf('tidak mencukupi untuk melakukan transaksi') >= 0) { | |
342 | + return '40'; | |
343 | + } | |
341 | 344 | else if (message.search(/stok product .* tidak tersedia ke nomor .* dari pilihan2 yg tersedia/i) >= 0) { |
342 | 345 | return '13'; |
343 | 346 | } |
344 | 347 | else if (message.search(/stok produk .* gangguan ke no tujuan/i) >= 0) { |
345 | 348 | return '13'; |
346 | 349 | } |
347 | - | |
350 | + | |
348 | 351 | return; |
349 | 352 | } |
350 | 353 | |
351 | 354 | function start(_config, _callbackReport, options) { |
352 | 355 | config = _config; |
353 | 356 | callbackReport = _callbackReport |
354 | - | |
357 | + | |
355 | 358 | if (options && options.aaa) { |
356 | 359 | aaa = options.aaa; |
357 | 360 | } |
358 | - | |
361 | + | |
359 | 362 | if (options && options.logger) { |
360 | 363 | logger = options.logger; |
361 | 364 | } else { |
... | ... | @@ -365,7 +368,7 @@ function start(_config, _callbackReport, options) { |
365 | 368 | ] |
366 | 369 | }); |
367 | 370 | } |
368 | - | |
371 | + | |
369 | 372 | createRedisClient(); |
370 | 373 | createServer(); |
371 | 374 | } |