gentong.js
11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
var fs = require('fs');
var https = require('https');
var http = require('http');
var url = require('url');
var request = require('request');
var xml2js = require('xml2js').parseString;
var strftime = require('strftime');
var redis = require('redis');
var Router = require('node-simple-router');
var winston = require('winston');
var logger;
var config;
var httpServer;
var redisClient;
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
var aaa;
var logTag = __filename.split('/').reverse()[0];
function getRedisKey(timestamp) {
var prefix = config.globals.gateway_name;
if (config.globals.redis_prefix) {
prefix = config.globals.redis_prefix;
}
return prefix + '.ts:' + timestamp + '.rid';
}
function generateTimestamp(request_id) {
var ts = strftime('%F %T', new Date());
var key = getRedisKey(ts);
redisClient.set(key, request_id);
redisClient.expire(key, 3600*48);
return ts;
}
function topupRequest(task) {
aaa.insertTaskToMongoDb(task);
var ts = strftime('%F %T', new Date());
ts = generateTimestamp(task['requestId']);
var options = {
url: config.h2h_out.partner,
qs: {
code: task['remoteProduct'],
msisdn: task['destination'],
user_id: config.h2h_out.userid,
password: config.h2h_out.password,
ts: ts,
ref_id: task.requestId
}
};
logger.info('Creating http request to gateway', {options: options});
if (aaa) {
aaa.incrementTrxCount();
aaa.incrementActiveTrxCount();
}
request(options, function (error, response, body) {
aaa.decrementActiveTrxCount();
if (error) {
var error_message = 'Error on http connection to gateway: ' + error;
logger.warn(error_message);
callbackReport(task['requestId'], '91', error_message);
return;
}
if (response.statusCode != 200) {
var error_message = 'Gateway error, http response code: ' + response.statusCode;
logger.warn(error_message);
callbackReport(task['requestId'], '91', error_message);
return;
}
var responseCode = 40;
var responseMessage;
xml2js(body, function (err, result) {
if (err) {
logger.warn('Error parsing XML', {response_error: err, response_body: body});
callbackReport(task['requestId'], '40', body);
return;
}
logger.info('Got direct response from request', {result: result});
try {
responseMessage = result.direct_ack.info[0];
if (result.direct_ack.request_status[0] == 'OK') {
responseCode = '68';
} else {
responseCode = '40';
var new_response_code = responseCodeFromMessage(responseMessage);
if (new_response_code) {
responseCode = new_response_code;
}
}
}
catch(err) {
logger.warn('Exception on parsing xml response');
responseCode = 40;
responseMessage = 'Invalid xml response from gateway';
}
callbackReport(task['requestId'], responseCode, responseMessage);
});
});
}
function createRedisClient() {
redisClient = redis.createClient(config.globals.redis_port, config.globals.redis_host);
}
function paddingSN(sn, _config) {
if (!_config) {
_config = config;
}
if (_config.h2h_out.sn_min_length && (sn.length < Number(_config.h2h_out.sn_min_length))) {
sn = '0000000000000000' + sn;
sn = sn.slice(Number(_config.h2h_out.sn_min_length) * -1);
}
return sn;
}
function parseSN(message, _config) {
if (!_config) {
_config = config;
}
var sn_regex = new RegExp(_config.h2h_out.sn_pattern);
var sn_match = message.match(sn_regex);
if (sn_match <= 0) {
logger.info('SN Not found: ' + message);
return '';
}
var match_index = 0;
if (_config.h2h_out.sn_match_index) {
match_index = Number(_config.h2h_out.sn_match_index);
}
var sn = sn_match[match_index];
if (_config.h2h_out.sn_remove_whitespace) {
sn = sn.replace(/\s/g, '');
}
var sn_remove_patterns = _config.h2h_out.sn_remove_patterns.split(_config.h2h_out.sn_remove_patterns_separator);
var count = sn_remove_patterns.length;
for(var i = 0; i < count; i++) {
//sn = sn.replace(sn_remove_patterns[i], '');
var re = new RegExp(sn_remove_patterns[i], 'g');
sn = sn.replace(re, '');
}
sn = paddingSN(sn, _config);
return sn.trim();
}
function createServer() {
var httpServer = http.createServer(function(request, response) {
var response_code = '68';
var sn = '';
var qs = url.parse(request.url, true).query;
logger.info('Got reverse report from gateway', {qs: qs});
if (qs.topup_status == 'S') {
response_code = '00';
if (qs.sn && !config.h2h_out.force_parse_sn && !Number(config.h2h_out.force_parse_sn)) {
sn = qs.sn;
} else {
logger.warn('Missing SN from query string. Trying to get SN from message');
sn = parseSN(qs.info);
}
if (config.h2h_out.sn_split_index) {
sn = splitSN(sn, config);
}
if (sn) {
sn = paddingSN(sn, config);
}
sn = sn.trim();
} else if (qs.topup_status == 'R') {
response_code = '40';
}
try {
var new_response_code = responseCodeFromMessage(qs.info);
if (new_response_code) {
response_code = new_response_code;
}
}
catch(err) {
logger.warn('Exception on parsing reverse report', {exception: err} );
response_code = '40';
}
message = qs.info;
//updateBalance(message);
if (sn) {
message = 'SN=' + sn + '; ' + message;
}
response.end('OK');
if (qs.ref_id) {
callbackReport(qs.ref_id, response_code, message);
} else {
reverseHandlerByTs(qs.ts, response_code, message);
}
});
httpServer.listen(config.h2h_out.listen_port, function() {
logger.info('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port);
});
}
function reverseHandlerByTs(ts, responseCode, message) {
var key = getRedisKey(ts);
redisClient.get(key, function(err, request_id) {
if (err) {
logger.warn('Error when requesting request id for ts:' + ts + ' (' + key + ')', {redis_error: err});
return;
}
callbackReport(request_id, responseCode, message);
});
}
function splitSN(sn, _config) {
var sn_pieces = sn.split(' ');
if (sn_pieces.length <= 0) {
logger.info('Returning original SN because SN only has one element');
return sn;
}
if (!_config.h2h_out.sn_split_index) {
logger.info('Returning original SN because config.h2h_out.sn_split_index undefined');
return sn;
}
var sn_indexes = _config.h2h_out.sn_split_index.split(',');
logger.info('Split SN', {sn_pieces: sn_pieces, sn_indexes: sn_indexes});
var _sn = '';
var id_count = sn_indexes.length;
for(var i = 0; i < id_count; i++) {
var sn_index = sn_indexes[i];
var sn_piece = sn_pieces[sn_index];
if (sn_pieces[i]) {
_sn = _sn + sn_piece;
} else {
logger.warn('Undefined value on sn piece ' + sn_index);
}
}
_sn = _sn.trim();
if (_sn) {
sn = _sn;
logger.info('Got new SN: ' + sn);
} else {
logger.warn('Got empty SN when using split SN. Use original SN');
}
return sn;
}
function responseCodeFromMessage(message) {
if (message.toLowerCase().indexOf('nomor salah/tidak terdaftar') >= 0) {
return '14';
}
else if (message.toLowerCase().indexOf('nomor tidak di temukan/tidak aktif') >= 0) {
return '14';
}
else if (message.toLowerCase().indexOf('kode produk tidak sesuai nomor tujuan') >= 0) {
return '14';
}
else if (message.toLowerCase().indexOf('nomor yang anda masukan salah') >= 0) {
return '14';
}
else if (message.toLowerCase().indexOf('nomor yang anda masukkan salah') >= 0) {
return '14';
}
else if (message.toLowerCase().indexOf('nomor telepon seluler salah') >= 0) {
return '14';
}
else if (message.indexOf('MSISDN not found') >= 0) {
return '14';
}
else if (message.indexOf('UNKNOWN_NUMBER') >= 0) {
return '14';
}
else if (message.toLowerCase().indexOf('bulk or forbidden request') >= 0) {
return '55';
}
else if (message.toLowerCase().indexOf('sudah pernah dilakukan') >= 0) {
return '55';
}
else if (message.toLowerCase().indexOf('sudah pernah di lakukan') >= 0) {
return '55';
}
else if (message.toLowerCase().indexOf('transaksi yg sama sudah pernah dilakukan tunggu dlm') >= 0) {
return '55';
}
else if (message.toLowerCase().indexOf('transaksi tsb sudah pernah dilakukan') >= 0) {
return '55';
}
else if (message.toLowerCase().indexOf('mohon maaf saat ini stock belum tersedia') >= 0) {
return '13';
}
else if (message.toLowerCase().indexOf('stock tidak tersedia') >= 0) {
return '13';
}
else if (message.toLowerCase().indexOf('tidak tersedia atau nonaktif') >= 0) {
return '13';
}
else if (message.toLowerCase().indexOf('saldo di account anda saat ini tidak mencukupi') >= 0) {
return '40';
}
else if (message.toLowerCase().indexOf('tidak mencukupi untuk melakukan transaksi') >= 0) {
return '40';
}
else if (message.toLowerCase().indexOf('saldo anda tidak mencukupi') >= 0) {
return '40';
}
else if (message.search(/stok .* tidak tersedia ke nomor .*/i) >= 0) {
return '13';
}
else if (message.search(/stok produk .* gangguan ke no tujuan/i) >= 0) {
return '13';
}
else if (message.toLowerCase().indexOf('stok tidak tersedia dari pilihan2 yg tersedia') >= 0) {
return '13';
}
else if (message.search(/system sedang.* maintenance/i) >= 0) {
return '91';
}
else if (message.search(/transaksi gagal di proses, ulangi beberapa saat lagi/i) >= 0) {
return '40';
}
else if (message.toLowerCase().indexOf('gagal') >= 0) {
return '40';
}
else if (message.toLowerCase().indexOf('ulangi beberapa saat lagi') >= 0) {
return '40';
}
else if (message.toLowerCase().indexOf('harga belum diset') >= 0) {
return '40';
}
else if (message.toLowerCase().indexOf('stok sementara tidak tersedia') == 0) {
return '13';
}
return;
}
function start(options) {
if (!options) {
console.log('Undefined options, terminating....');
process.exit(1);
}
if (options.config) {
config = options.config;
} else {
console.log('Undefined options.config, terminating....')
process.exit(1);
}
if (options.aaa) {
aaa = options.aaa;
callbackReport = options.aaa.callbackReportWithPushToMongoDb;
} else {
console.log('Undefined options.aaa, terminating....')
process.exit(1);
}
if (options.logger) {
logger = options.logger;
} else {
logger = new winston.Logger({
transports: [
new (winston.transports.Console)()
]
});
}
createRedisClient();
createServer();
}
exports.start = start;
exports.topupRequest = topupRequest;
exports.parseSN = parseSN;