Blame view
partner-kospinjasa.js
14.2 KB
1f837fd31
|
1 2 |
var winston = require('winston'); var soap = require('soap'); |
5a947e400
|
3 4 |
var crypto = require('crypto'); var strftime = require('strftime'); |
97991a1dc
|
5 |
var fs = require("fs"); |
97991a1dc
|
6 |
var http = require("http"); |
e7d59da25
|
7 |
var url = require("url"); |
1caba187c
|
8 9 |
var mongoClient = require('mongodb').MongoClient; var moment = require('moment'); |
97991a1dc
|
10 11 |
process.chdir(__dirname); |
1f837fd31
|
12 13 14 15 16 |
var max_retry = 10; var sleep_before_retry = 5000; var config; |
bc5bcfea8
|
17 |
var matrix; |
1f837fd31
|
18 19 20 21 |
var callbackReport; var aaa; var logger; var options; |
1caba187c
|
22 |
var mongodb; |
1f837fd31
|
23 |
|
bc5bcfea8
|
24 |
|
1f837fd31
|
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
function start(_config, _callbackReport, options) { config = _config; callbackReport = _callbackReport if (options && options.aaa) { aaa = options.aaa; } if (options && options.logger) { logger = options.logger; } else { logger = new winston.Logger({ transports: [ new (winston.transports.Console)() ] }); } |
1caba187c
|
42 |
|
bc5bcfea8
|
43 44 45 |
if (options && options.matrix) { matrix = options.matrix; } |
1caba187c
|
46 47 |
initMongoClient(); } |
bc5bcfea8
|
48 49 50 51 52 53 54 55 56 57 58 59 60 |
function callbackReportWrapper(requestId, responseCode, message, dontIncrement) { callbackReport(requestId, responseCode, message); if (dontIncrement) { return; } try { aaa.incrementStrikeStatus(responseCode); } catch(err) { logger.warn("Gagal aaa.incrementStrikeStatus: " + err); } } |
1caba187c
|
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 |
function initMongoClient() { if (!config || !config.mongodb || !config.mongodb.url) { return; } try { var url = config.mongodb.url; mongoClient.connect(url, function(err, db) { if (err) { logger.warn('Failed to connect to mongodb', {err: err}); return; } mongodb = db; logger.info('MongoDB connected'); }); } catch(err) { logger.warn('Exception when connecting to mongodb', {err: err, url: url}); } } function insertTaskToMongoDb(task) { if (!isMongoReady()) { return; } task.supplier = config.globals.gateway_name; task.rc = '68'; try { mongodb.collection(config.mongodb.collection).insertOne(task); } catch(err) { //logger.warn('Exception when inserting document to mongodb', {err: err, task: task}); } } function pushResponseToMongoDb(task, response, rc) { if (!isMongoReady()) { return; } |
2af28a398
|
99 |
|
1caba187c
|
100 |
try { |
2af28a398
|
101 102 103 |
if (!response.ts) { response.ts = strftime('%Y-%m-%d %H:%M:%S', new Date()); } |
1caba187c
|
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 |
mongodb.collection(config.mongodb.collection).updateOne( {requestId: task.requestId}, { $set: { lastResponse: response, supplier: config.globals.gateway_name, rc: rc }, $push: { responses: response } }, function(err, result) { if (err) { logger.warn('Error when pushing response to mongodb', {err: err, task: task, response: response}); return; } } ); } catch(err) { logger.warn('Exception when pushing response to mongodb', {err: err, task: task, response: response}); } } function isMongoReady() { if (!config.mongodb) { return; } if (!config.mongodb.collection) { return; } if (!mongodb) { return; } return true; |
1f837fd31
|
135 136 137 |
} function topupRequest(task, retry) { |
bc5bcfea8
|
138 139 140 141 142 143 |
if (config && config.globals && config.globals.reject_on_pending_count && matrix && matrix.strikeStatus && matrix.strikeStatus.pending) { if (parseInt(config.globals.reject_on_pending_count) <= matrix.strikeStatus.pending) { callbackReport(task.requestId, '13', 'Reject trx karena pending terlalu banyak'); return; } } |
1caba187c
|
144 145 146 147 |
task.ts = moment(task.timestamp, 'YYYYMMDDHHmmss').format('YYYY-MM-DD HH:mm:ss'); task.ts_date = moment(task.timestamp, 'YYYYMMDDHHmmss').format('YYYY-MM-DD'); insertTaskToMongoDb(task); |
9e047c122
|
148 149 150 151 152 153 154 155 156 157 |
saldoCheck(billpayment, task); } function saldoCheck(callback, task) { var params = { userName: config.h2h_out.userid, productCode: '00000' , terminal: 'H2HIPN10', transactionType: '61', |
1c0291c88
|
158 |
reff: Math.ceil( Math.random() * 99999999 ), |
9e047c122
|
159 |
timeStamp: strftime('%Y-%m-%d %H:%M:%S', new Date()) |
1f837fd31
|
160 |
} |
9e047c122
|
161 |
params.signature = createSignatureForSaldoCheck(params, config.h2h_out.password); |
9b569c12b
|
162 |
soap.createClient(config.h2h_out.partner, function(err, soapClient) { |
9e047c122
|
163 164 |
if (err) { |
1caba187c
|
165 166 167 168 |
var errorMessage = 'Error creating soap client for saldoCheck: ' + err; logger.warn(errorMessage, {err: err}); |
bc5bcfea8
|
169 |
callbackReportWrapper(task.requestId, '40', errorMessage); |
2af28a398
|
170 |
pushResponseToMongoDb(task, {supplier: config.globals.gateway_name, raw: errorMessage}, '40'); |
1caba187c
|
171 |
|
9b569c12b
|
172 |
return; |
9e047c122
|
173 |
} |
9b569c12b
|
174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
logger.info('Requesting to service', {url: config.h2h_out.partner, params: params}); soapClient.apih2h.apih2hPort.saldoCheck({ inputSaldo: params }, function(err, result) { logger.verbose( 'Got saldoCheck response', { lastEndpoint: soapClient.lastEndpoint, lastRequest: soapClient.lastRequest, lastMessage: soapClient.lastMessage, lastResponse: soapClient.lastResponse, } ); if (err) { |
1caba187c
|
188 189 190 |
var errorMessage = 'Error requesting saldoCheck: ' + err; logger.warn(errorMessage, {err: err}); |
bc5bcfea8
|
191 |
callbackReportWrapper(task.requestId, '40', errorMessage); |
2af28a398
|
192 |
pushResponseToMongoDb(task, {supplier: config.globals.gateway_name, raw: errorMessage}, '40'); |
9b569c12b
|
193 |
} |
9e047c122
|
194 |
|
9b569c12b
|
195 196 |
var balance; logger.verbose('saldoCheck result', {result: result}); |
9e047c122
|
197 |
|
d309c00e0
|
198 199 200 201 202 203 |
try { balance = result.outputParameter.bit61.$value; } catch(e) { balance = 'UNKNOWN'; } |
9b569c12b
|
204 205 206 207 208 |
if (task) { callback(task, balance); } }); |
7a475ed37
|
209 |
}); |
9e047c122
|
210 211 212 213 |
} function billpayment(task, balance) { |
ba873b76b
|
214 |
var terminalSuffix = Math.ceil( Math.random() * 20 ) + 10; |
5a947e400
|
215 |
var remoteProduct = task.remoteProduct.split(','); |
ba873b76b
|
216 |
|
b6b185202
|
217 |
var params = { |
7f796c813
|
218 219 |
userName: config.h2h_out.userid, productCode: remoteProduct[0] , |
ba873b76b
|
220 |
terminal: 'H2HIPN' + terminalSuffix, |
7f796c813
|
221 222 223 224 225 |
transactionType: '50', billNumber: createBillNumber(task.destination), amount: remoteProduct[1], bit61: createBillNumber(task.destination), reff: task.requestId, |
342d268ac
|
226 |
timeStamp: strftime('%Y-%m-%d %H:%M:%S', new Date()) |
7f796c813
|
227 |
} |
b6b185202
|
228 229 |
var signature = createSignature(params, config.h2h_out.password); params.signature = signature; |
7f796c813
|
230 |
|
777c49c56
|
231 |
soap.createClient(config.h2h_out.partner, function(err, soapClient) { |
35d3e5057
|
232 |
|
699851497
|
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
var _params = { userName: params.userName, signature: params.signature, productCode: params.productCode, terminal: params.terminal, transactionType: params.transactionType, billNumber: params.billNumber, amount: params.amount, bit61: params.bit61, reff: params.reff, timeStamp: params.timeStamp } logger.info('Requesting to service', {url: config.h2h_out.partner, params: _params}); soapClient.apih2h.apih2hPort.billpayment({ inputCheck: _params }, function(err, result) { |
4252c2818
|
249 250 251 252 253 254 255 256 257 |
logger.verbose( 'Got response', { lastEndpoint: soapClient.lastEndpoint, lastRequest: soapClient.lastRequest, lastMessage: soapClient.lastMessage, lastResponse: soapClient.lastResponse, } ); |
3899d9816
|
258 |
|
5a947e400
|
259 |
if (err) { |
1caba187c
|
260 261 262 |
var errorMessage = 'Error requesting service: ' + err; logger.warn(errorMessage, {err: err}); |
bc5bcfea8
|
263 |
callbackReportWrapper(task.requestId, '68', errorMessage); |
64e3e4b8c
|
264 |
pushResponseToMongoDb(task, {supplier: config.globals.gateway_name, raw: soapClient.lastResponse}, '68'); |
1caba187c
|
265 |
|
5a947e400
|
266 267 |
return; } |
1caba187c
|
268 |
topupResponseHandler(task, result, balance, soapClient.lastResponse); |
1f837fd31
|
269 270 271 |
}); }); } |
1caba187c
|
272 |
function topupResponseHandler(task, response, balance, rawResponse) { |
4252c2818
|
273 274 |
var st24rc = '68'; var st24message = response.outputParameter.resultDesc.$value; |
97991a1dc
|
275 |
|
aa561d430
|
276 277 |
var resultCode = parseInt(response.outputParameter.resultCode.$value); var bit39 = parseInt(response.outputParameter.bit39.$value); |
064e7c879
|
278 |
var sn = ''; |
aa561d430
|
279 |
if ( resultCode == 1 ) { |
4252c2818
|
280 281 282 |
// product disabled st24rc = '13'; } |
aa561d430
|
283 |
else if ( resultCode == 2 ) { |
4252c2818
|
284 285 286 |
// prodcode disable st24rc = '13'; } |
aa561d430
|
287 |
else if ( resultCode == 3 ) { |
4252c2818
|
288 289 290 |
// duplicate reff st24rc = '55'; } |
aa561d430
|
291 |
else if ( resultCode == 4 ) { |
4252c2818
|
292 293 294 |
// not enough balance st24rc = '40'; } |
aa561d430
|
295 |
else if ( resultCode == 5 ) { |
4252c2818
|
296 297 298 |
// username blocked st24rc = '40'; } |
aa561d430
|
299 |
else if ( resultCode == 6 ) { |
4252c2818
|
300 301 302 |
// not exists username st24rc = '40'; } |
aa561d430
|
303 |
else if ( resultCode == 11 ) { |
4252c2818
|
304 305 306 |
// invalid request st24rc = '40' } |
aa561d430
|
307 |
else if ( resultCode == 12 ) { |
4252c2818
|
308 309 310 |
// no route to host st24rc = '40' } |
aa561d430
|
311 |
else if ( resultCode == 13 ) { |
4252c2818
|
312 313 314 |
// invalid signature st24rc = '40' } |
aa561d430
|
315 |
else if ( bit39 == 6 ) { |
4252c2818
|
316 317 318 |
st24rc = '40'; st24message = 'Error Transaksi ditolak karena terjadi error di H2H dengan response code diluar daftar ini. Silahkan hubungi H2H'; } |
aa561d430
|
319 |
else if ( bit39 == 12 ) { |
4252c2818
|
320 321 322 |
st24rc = '40'; st24message = 'Invalid Transaction Transaksi ditolak karena flow transaksi tidak valid'; } |
aa561d430
|
323 |
else if ( bit39 == 13 ) { |
4252c2818
|
324 325 326 |
st24rc = '13'; st24message = 'Invalid voucher nominal'; } |
aa561d430
|
327 |
else if ( bit39 == 14 ) { |
4252c2818
|
328 329 330 |
st24rc = '14'; st24message = 'MSISDN tidak ditemukan'; } |
aa561d430
|
331 |
else if ( bit39 == 30 ) { |
4252c2818
|
332 333 334 |
st24rc = '40'; st24message = 'Format Error'; } |
aa561d430
|
335 |
else if ( bit39 == 31 ) { |
4252c2818
|
336 337 338 |
st24rc = '40'; st24message = 'Kode bank tidak terdaftar'; } |
aa561d430
|
339 |
else if ( bit39 == 63 ) { |
4252c2818
|
340 341 342 |
st24rc = '40'; st24message = 'Reversal denied'; } |
aa561d430
|
343 |
else if ( bit39 == 68 ) { |
4252c2818
|
344 345 346 |
st24rc = '68'; st24message = 'Transaksi sedang dalam proses'; } |
aa561d430
|
347 |
else if ( bit39 == 69 ) { |
4252c2818
|
348 349 350 |
st24rc = '68'; st24message = 'Respon Ok lebih dari 24 detik'; } |
aa561d430
|
351 |
else if ( bit39 == 70 ) { |
4252c2818
|
352 353 354 |
st24rc = '13'; st24message = 'Voucher out of stock'; } |
aa561d430
|
355 |
else if ( bit39 == 79 ) { |
4252c2818
|
356 357 358 |
st24rc = '14'; st24message = 'Phone number is blocked by Telkomsel'; } |
aa561d430
|
359 |
else if ( bit39 == 81 ) { |
4252c2818
|
360 361 362 |
st24rc = '14'; st24message = 'Phone number is expired'; } |
aa561d430
|
363 |
else if ( bit39 == 89 ) { |
4252c2818
|
364 365 366 |
st24rc = '40'; st24message = 'Link to billing provider is down'; } |
aa561d430
|
367 |
else if ( bit39 == 91 ) { |
4252c2818
|
368 369 370 |
st24rc = '40'; st24message = 'Database problem'; } |
aa561d430
|
371 |
else if ( bit39 == 92 ) { |
4252c2818
|
372 373 374 |
st24rc = '40'; st24message = 'Unable to route transaction'; } |
aa561d430
|
375 |
else if ( bit39 == 94 ) { |
4252c2818
|
376 377 378 |
st24rc = '40'; st24message = 'Duplicate reversal request'; } |
aa561d430
|
379 |
else if ( resultCode == 0 && bit39 == 0) { |
aa561d430
|
380 381 382 |
try { sn = response.outputParameter.bit61.$value.substring(43); } |
e2e928e50
|
383 |
catch(e) { |
aa561d430
|
384 385 386 387 388 389 |
sn = ''; } st24message = 'SN=' + sn + '; ' + st24message; st24rc = '00'; } |
97991a1dc
|
390 |
|
4252c2818
|
391 392 393 |
var message = response.outputParameter.resultCode.$value + " " + response.outputParameter.resultDesc.$value |
aa561d430
|
394 |
+ "; BIT39: " + response.outputParameter.bit39.$value |
4252c2818
|
395 |
; |
97991a1dc
|
396 |
|
4252c2818
|
397 398 399 |
if (response.outputParameter.resultDesc.$value != st24message) { var message = message + " " + st24message; } |
97991a1dc
|
400 |
|
d309c00e0
|
401 |
message = message + ' -- Prev Balance: ' + balance; |
64e3e4b8c
|
402 |
var parsedResponse = { |
a5668f44f
|
403 404 405 406 407 408 409 410 411 412 413 |
productCode: response.outputParameter.productCode.$value, terminal: response.outputParameter.terminal.$value, transactionType: response.outputParameter.transactionType.$value, billNumber: response.outputParameter.billNumber.$value, amount: response.outputParameter.amount.$value, bit61: response.outputParameter.bit61.$value, reff: response.outputParameter.reff.$value, timeStamp: response.outputParameter.timeStamp.$value, resultCode: response.outputParameter.resultCode.$value, resultDesc: response.outputParameter.resultDesc.$value, bit39: response.outputParameter.bit39.$value, |
872fa65c3
|
414 |
prevBalance: balance, |
064e7c879
|
415 416 |
sn: sn, st24message: st24message, |
64e3e4b8c
|
417 |
} |
872fa65c3
|
418 419 420 |
var combinedMessage = ''; Object.keys(parsedResponse).forEach(function(key,index) { |
064e7c879
|
421 |
combinedMessage += key + ': ' + parsedResponse[key] + '; ' |
872fa65c3
|
422 423 424 425 |
}); combinedMessage = combinedMessage.trim(); parsedResponse.MESSAGE = combinedMessage; |
13e965f4e
|
426 |
logger.info('Got result: ' + message, {response: response}); |
bc5bcfea8
|
427 |
callbackReportWrapper(task.requestId, st24rc, st24message + ' -- Prev Balance: ' + balance); |
64e3e4b8c
|
428 |
pushResponseToMongoDb(task, {supplier: config.globals.gateway_name, raw: rawResponse, parsed: parsedResponse}, st24rc); |
97991a1dc
|
429 |
} |
b6b185202
|
430 |
function createSignature(params, password) { |
5a947e400
|
431 |
var passwordHash = crypto.createHash('sha256').update(password).digest().toString('hex'); |
dfcc535b5
|
432 |
var plain = |
b6b185202
|
433 |
params.userName |
5a947e400
|
434 |
+ passwordHash |
b6b185202
|
435 436 437 438 439 440 441 |
+ params.productCode + params.terminal + params.transactionType + params.billNumber + params.amount + params.reff + params.timeStamp; |
dfcc535b5
|
442 |
|
4252c2818
|
443 |
return crypto.createHash('sha1').update(plain).digest().toString('hex'); |
5a947e400
|
444 |
} |
9e047c122
|
445 446 447 448 449 450 451 452 453 454 455 456 457 |
function createSignatureForSaldoCheck(params, password) { var passwordHash = crypto.createHash('sha256').update(password).digest().toString('hex'); var plain = params.userName + passwordHash + params.productCode + params.terminal + params.transactionType + params.reff + params.timeStamp; return crypto.createHash('sha1').update(plain).digest().toString('hex'); } |
5a947e400
|
458 459 |
function createBillNumber(destination) { return ("0000000000000" + destination).slice(-13); |
1f837fd31
|
460 461 462 463 |
} exports.start = start; exports.topupRequest = topupRequest; |
5a947e400
|
464 465 |
exports.createSignature = createSignature; exports.createBillNumber = createBillNumber; |