cekstatus.js
5.29 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
var request = require('request');
var xpath = require('xpath');
var parse5 = require('parse5');
var xmlser = require('xmlserializer');
var dom = require('xmldom').DOMParser;
var xml2js = require('xml2js').parseString;
var argv = require('minimist')(process.argv.slice(2), {string: ['requestid', 'destination']});
var strftime = require('strftime');
var fs = require('fs');
var ini = require('ini');
var winston = require('winston');
var config;
var logger = new winston.Logger({
transports: [
new (winston.transports.Console)()
]
});
function setLogger(_logger) {
logger = _logger;
}
function advice(data, callback) {
if (!data.trxid && !data.destination) {
logger.warn('Webreport advice request without data. Canceling', {data: data});
return;
}
if (data.config) {
config = data.config;
} else {
try {
config = ini.parse(fs.readFileSync(__dirname + '/config.ini', 'utf-8'));
}
catch(err) {
logger.warn('Error loading config file');
return;
}
}
login(data, callback);
};
function login(data, callback) {
var jar = request.jar();
var options = {
url: 'http://103.11.75.142:9009/dealer/index.php/admin/login',
jar: jar,
followAllRedirects: true,
form: {
username: config.webreport.username,
password: config.webreport.password,
Submit: 'Login',
},
};
//console.log('Requesting', options);
request.post(options, function(error, httpResponse, body) {
if (error) {
logger.warn('Error retrieving login', {options: options, error: error, body: body});
return;
}
if (body.search('Incorrect username') >= 0) {
logger.warn('Salah username / password', {options: options});
return;
}
getTrxStatusPage(data, jar, callback);
});
}
function getTrxStatusPage(data, jar, callback) {
var options = {
url: 'http://103.11.75.142:9009/dealer/index.php/transaction/index',
jar: jar,
followAllRedirects: true,
form: {
startdate: '1970-01-01',
enddate: strftime('%F'),
pdate: '',
trxid: '',
destmisdn: '',
type: 0,
ts: '',
submit: 'Find',
},
};
if (data.trxid) {
options.form.trxid = data.trxid;
}
if (data.destination) {
options.form.destmisdn = data.destination;
}
//console.log(options);
request.post(options, function(error, httpResponse, body) {
if (error) {
logger.warn('Error retrieving trx status page', {data: data, options: options, err: error});
return;
}
body = body.replace(/<\/thead>\s<\/tbody>/, "</thead>\n<tbody>");
var document = parse5.parse(body);
var xhtml = xmlser.serializeToString(document);
var doc = new dom().parseFromString(xhtml);
var select = xpath.useNamespaces({"x": "http://www.w3.org/1999/xhtml"});
var nodes = select('//x:*[@id="dirlist"]/x:tbody/x:tr/x:td', doc);
var status = {};
try {
status = {
trxId: nodes[0].firstChild.data,
trxDate: nodes[1].firstChild.data,
updateDate: nodes[2].firstChild.data,
product: nodes[3].firstChild.data,
amount: nodes[4].firstChild.data,
msisdn: nodes[5].firstChild.data,
reffId: nodes[6].firstChild.data,
status: nodes[7].firstChild.data,
response: nodes[8].toString(),
}
}
catch(errStatus) {
logger.warn('Error parsing status', {data: data, status: status});
if (callback) {
callback();
}
return;
}
status.response = status.response.replace('<td>', '');
status.response = status.response.replace('</td>', '');
status.response = status.response.replace(/\n/g, '');
status.response = status.response.trim();
//status.response_raw = status.response;
xml2js(status.response, function (err, result) {
if (err) {
logger.warn('Gagal parsing XML', {err: err, status: status, doc: doc});
if (callback) {
status.response = {};
callback(status);
}
return;
}
try {
status.response = result.result;
}
catch(e) {
logger.warn('Exception on get status.response', {e: e, result: result});
}
if (!callback) {
logger.warn('Invalid callback on checkstatus', {status: status});
} else {
logger.verbose('Calling callbackFromWebReport', {data: data, status: status, body: body})
callback(status);
}
});
});
}
if (require.main === module) {
var data = {
trxid: argv.trxid,
destination: argv.destination,
}
if (argv.trxid) {
data.trxid = argv.trxid;
}
if (argv.destination) {
data.destinaton = argv.destination;
}
advice(data);
}
exports.setLogger = setLogger;
exports.advice = advice;