partner-bayarkilat.js
3.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
var request = require('request');
var url = require('url');
var winston = require('winston');
var xml2jsParser = require('xml2js').parseString;
var config;
var aaa;
var callbackReport;
var logger;
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
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)()
]
});
}
}
function topupRequest(task, retry) {
aaa.insertTaskToMongoDb(task);
var partnerUrl = url.parse(config.h2h_out.partner);
var product = prepareRemoteProductCode(task.remoteProduct);
var options = {
url: config.h2h_out.partner,
qs: {
request: 'PURCHASE*'
+ task.requestId + '*'
+ product.product + '*'
+ product.productDetail + '*'
+ task.destination + '*'
+ product.nominal + '*'
+ '0*'
+ config.h2h_out.noid + '*'
+ config.h2h_out.userid + '*'
+ config.h2h_out.password
}
};
logger.verbose('Requesting to partner', {requestOption: options});
request(options, function(error, response, body) {
if (error) {
logger.warn('Error requesting to partner', {error: error});
callbackReport(task.requestId, '68', 'Error requesting to partner. ' + error);
return;
}
if (response.statusCode != 200) {
var message = 'Partner response with http status code other that 200 (' + response.statusCode +')';
logger.warn(message);
callbackReport(task.requestId, '68', message);
return;
}
/*
logger.verbose('Got response', {requestId: task.requestId, responseBody: body});
callbackReport(task.requestId, '68', body);
*/
parseResponse(body, task);
});
}
function parseResponse(body, task) {
xml2jsParser(body, function(err, response) {
if (err) {
logger.warn('Error parsing XML', {error: err, task: task, responseBody: body});
var message = 'Error parsing XML. ' + err + '. ' + body;
callbackReport(task.requestId, '68', message);
return;
}
logger.info('Got response', {response: response});
var responseCode = '68';
var message = '';
try {
responseCode = response.xml.response_code[0];
saldo = response.xml.saldo[0];
message = response.xml.response_message[0];
var st24rc = '40';
if (responseCode == '00') {
st24rc = '00';
}
else if (responseCode == '99') {
st24rc = '68'
}
else {
st24rc = '40';
}
var st24message = responseCode + ' ' + message + '. Saldo ' + saldo;
callbackReport(task.requestId, st24rc, st24message);
}
catch(errGetParam) {
logger.warn('Exception saat parsing hasil', {error: errGetParam, task: task, responseBody: body});
callbackReport(task.requestId, '68', 'Exception saat parsing hasil. ' + errGetParam);
}
});
}
function prepareRemoteProductCode(remoteProduct) {
var product = remoteProduct.split(',');
if (product.length != 3) {
return;
}
return {
product: product[0],
productDetail: product[1],
nominal: product[2]
}
}
exports.start = start;
exports.topupRequest = topupRequest;
exports.prepareRemoteProductCode = prepareRemoteProductCode;