Commit 0809da8c6adf7c113e3ab14911b37668d63df010
1 parent
120c8a3574
Exists in
master
typo
Showing 1 changed file with 1 additions and 1 deletions Inline Diff
cekstatus.js
1 | var request = require('request'); | 1 | var request = require('request'); |
2 | var xpath = require('xpath'); | 2 | var xpath = require('xpath'); |
3 | var parse5 = require('parse5'); | 3 | var parse5 = require('parse5'); |
4 | var xmlser = require('xmlserializer'); | 4 | var xmlser = require('xmlserializer'); |
5 | var dom = require('xmldom').DOMParser; | 5 | var dom = require('xmldom').DOMParser; |
6 | var xml2js = require('xml2js').parseString; | 6 | var xml2js = require('xml2js').parseString; |
7 | var argv = require('minimist')(process.argv.slice(2), {string: ['requestid', 'destination']}); | 7 | var argv = require('minimist')(process.argv.slice(2), {string: ['requestid', 'destination']}); |
8 | var strftime = require('strftime'); | 8 | var strftime = require('strftime'); |
9 | var fs = require('fs'); | 9 | var fs = require('fs'); |
10 | var ini = require('ini'); | 10 | var ini = require('ini'); |
11 | var winston = require('winston'); | 11 | var winston = require('winston'); |
12 | 12 | ||
13 | var config; | 13 | var config; |
14 | var logger = new winston.Logger({ | 14 | var logger = new winston.Logger({ |
15 | transports: [ | 15 | transports: [ |
16 | new (winston.transports.Console)() | 16 | new (winston.transports.Console)() |
17 | ] | 17 | ] |
18 | }); | 18 | }); |
19 | 19 | ||
20 | function setLogger(_logger) { | 20 | function setLogger(_logger) { |
21 | logger = _logger; | 21 | logger = _logger; |
22 | } | 22 | } |
23 | 23 | ||
24 | function advice(data, callback) { | 24 | function advice(data, callback) { |
25 | if (!data.trxid && !data.destination) { | 25 | if (!data.trxid && !data.destination) { |
26 | logger.warn('Webreport advice request without data. Canceling', {data: data}); | 26 | logger.warn('Webreport advice request without data. Canceling', {data: data}); |
27 | return; | 27 | return; |
28 | } | 28 | } |
29 | 29 | ||
30 | if (data.config) { | 30 | if (data.config) { |
31 | config = data.config; | 31 | config = data.config; |
32 | } else { | 32 | } else { |
33 | try { | 33 | try { |
34 | config = ini.parse(fs.readFileSync(__dirname + '/config.ini', 'utf-8')); | 34 | config = ini.parse(fs.readFileSync(__dirname + '/config.ini', 'utf-8')); |
35 | } | 35 | } |
36 | catch(err) { | 36 | catch(err) { |
37 | logger.warn('Error loading config file'); | 37 | logger.warn('Error loading config file'); |
38 | return; | 38 | return; |
39 | } | 39 | } |
40 | } | 40 | } |
41 | 41 | ||
42 | login(data, callback); | 42 | login(data, callback); |
43 | }; | 43 | }; |
44 | 44 | ||
45 | function login(data, callback) { | 45 | function login(data, callback) { |
46 | var jar = request.jar(); | 46 | var jar = request.jar(); |
47 | 47 | ||
48 | var options = { | 48 | var options = { |
49 | url: 'http://103.11.75.142:9009/dealer/index.php/admin/login', | 49 | url: 'http://103.11.75.142:9009/dealer/index.php/admin/login', |
50 | jar: jar, | 50 | jar: jar, |
51 | followAllRedirects: true, | 51 | followAllRedirects: true, |
52 | form: { | 52 | form: { |
53 | username: config.webreport.username, | 53 | username: config.webreport.username, |
54 | password: config.webreport.password, | 54 | password: config.webreport.password, |
55 | Submit: 'Login', | 55 | Submit: 'Login', |
56 | }, | 56 | }, |
57 | }; | 57 | }; |
58 | 58 | ||
59 | //console.log('Requesting', options); | 59 | //console.log('Requesting', options); |
60 | request.post(options, function(error, httpResponse, body) { | 60 | request.post(options, function(error, httpResponse, body) { |
61 | if (error) { | 61 | if (error) { |
62 | logger.warn('Error retrieving login', {options: options, error: error, body: body}); | 62 | logger.warn('Error retrieving login', {options: options, error: error, body: body}); |
63 | return; | 63 | return; |
64 | } | 64 | } |
65 | 65 | ||
66 | if (body.search('Incorrect username') >= 0) { | 66 | if (body.search('Incorrect username') >= 0) { |
67 | logger.warn('Salah username / password', {options = options}); | 67 | logger.warn('Salah username / password', {options: options}); |
68 | return; | 68 | return; |
69 | } | 69 | } |
70 | 70 | ||
71 | getTrxStatusPage(data, jar, callback); | 71 | getTrxStatusPage(data, jar, callback); |
72 | 72 | ||
73 | }); | 73 | }); |
74 | } | 74 | } |
75 | 75 | ||
76 | function getTrxStatusPage(data, jar, callback) { | 76 | function getTrxStatusPage(data, jar, callback) { |
77 | var options = { | 77 | var options = { |
78 | url: 'http://103.11.75.142:9009/dealer/index.php/transaction/index', | 78 | url: 'http://103.11.75.142:9009/dealer/index.php/transaction/index', |
79 | jar: jar, | 79 | jar: jar, |
80 | followAllRedirects: true, | 80 | followAllRedirects: true, |
81 | form: { | 81 | form: { |
82 | startdate: '1970-01-01', | 82 | startdate: '1970-01-01', |
83 | enddate: strftime('%F'), | 83 | enddate: strftime('%F'), |
84 | pdate: '', | 84 | pdate: '', |
85 | trxid: '', | 85 | trxid: '', |
86 | destmisdn: '', | 86 | destmisdn: '', |
87 | type: 0, | 87 | type: 0, |
88 | ts: '', | 88 | ts: '', |
89 | submit: 'Find', | 89 | submit: 'Find', |
90 | }, | 90 | }, |
91 | }; | 91 | }; |
92 | 92 | ||
93 | if (data.trxid) { | 93 | if (data.trxid) { |
94 | options.form.trxid = data.trxid; | 94 | options.form.trxid = data.trxid; |
95 | } | 95 | } |
96 | 96 | ||
97 | if (data.destination) { | 97 | if (data.destination) { |
98 | options.form.destmisdn = data.destination; | 98 | options.form.destmisdn = data.destination; |
99 | } | 99 | } |
100 | 100 | ||
101 | //console.log(options); | 101 | //console.log(options); |
102 | request.post(options, function(error, httpResponse, body) { | 102 | request.post(options, function(error, httpResponse, body) { |
103 | if (error) { | 103 | if (error) { |
104 | logger.warn('Error retrieving trx status page', {data: data, options: options, err: error}); | 104 | logger.warn('Error retrieving trx status page', {data: data, options: options, err: error}); |
105 | return; | 105 | return; |
106 | } | 106 | } |
107 | 107 | ||
108 | body = body.replace(/<\/thead>\s<\/tbody>/, "</thead>\n<tbody>"); | 108 | body = body.replace(/<\/thead>\s<\/tbody>/, "</thead>\n<tbody>"); |
109 | 109 | ||
110 | var document = parse5.parse(body); | 110 | var document = parse5.parse(body); |
111 | var xhtml = xmlser.serializeToString(document); | 111 | var xhtml = xmlser.serializeToString(document); |
112 | var doc = new dom().parseFromString(xhtml); | 112 | var doc = new dom().parseFromString(xhtml); |
113 | var select = xpath.useNamespaces({"x": "http://www.w3.org/1999/xhtml"}); | 113 | var select = xpath.useNamespaces({"x": "http://www.w3.org/1999/xhtml"}); |
114 | 114 | ||
115 | var nodes = select('//x:*[@id="dirlist"]/x:tbody/x:tr/x:td', doc); | 115 | var nodes = select('//x:*[@id="dirlist"]/x:tbody/x:tr/x:td', doc); |
116 | var status = {}; | 116 | var status = {}; |
117 | 117 | ||
118 | try { | 118 | try { |
119 | status = { | 119 | status = { |
120 | trxId: nodes[0].firstChild.data, | 120 | trxId: nodes[0].firstChild.data, |
121 | trxDate: nodes[1].firstChild.data, | 121 | trxDate: nodes[1].firstChild.data, |
122 | updateDate: nodes[2].firstChild.data, | 122 | updateDate: nodes[2].firstChild.data, |
123 | product: nodes[3].firstChild.data, | 123 | product: nodes[3].firstChild.data, |
124 | amount: nodes[4].firstChild.data, | 124 | amount: nodes[4].firstChild.data, |
125 | msisdn: nodes[5].firstChild.data, | 125 | msisdn: nodes[5].firstChild.data, |
126 | reffId: nodes[6].firstChild.data, | 126 | reffId: nodes[6].firstChild.data, |
127 | status: nodes[7].firstChild.data, | 127 | status: nodes[7].firstChild.data, |
128 | response: nodes[8].toString(), | 128 | response: nodes[8].toString(), |
129 | } | 129 | } |
130 | } | 130 | } |
131 | catch(errStatus) { | 131 | catch(errStatus) { |
132 | logger.warn('Error parsing status', {data: data, status: status}); | 132 | logger.warn('Error parsing status', {data: data, status: status}); |
133 | if (callback) { | 133 | if (callback) { |
134 | callback(); | 134 | callback(); |
135 | } | 135 | } |
136 | return; | 136 | return; |
137 | } | 137 | } |
138 | 138 | ||
139 | status.response = status.response.replace('<td>', ''); | 139 | status.response = status.response.replace('<td>', ''); |
140 | status.response = status.response.replace('</td>', ''); | 140 | status.response = status.response.replace('</td>', ''); |
141 | status.response = status.response.replace(/\n/g, ''); | 141 | status.response = status.response.replace(/\n/g, ''); |
142 | status.response = status.response.trim(); | 142 | status.response = status.response.trim(); |
143 | //status.response_raw = status.response; | 143 | //status.response_raw = status.response; |
144 | 144 | ||
145 | xml2js(status.response, function (err, result) { | 145 | xml2js(status.response, function (err, result) { |
146 | if (err) { | 146 | if (err) { |
147 | logger.warn('Gagal parsing XML', {err: err, status: status, doc: doc}); | 147 | logger.warn('Gagal parsing XML', {err: err, status: status, doc: doc}); |
148 | if (callback) { | 148 | if (callback) { |
149 | status.response = {}; | 149 | status.response = {}; |
150 | callback(status); | 150 | callback(status); |
151 | } | 151 | } |
152 | return; | 152 | return; |
153 | } | 153 | } |
154 | 154 | ||
155 | try { | 155 | try { |
156 | status.response = result.result; | 156 | status.response = result.result; |
157 | } | 157 | } |
158 | catch(e) { | 158 | catch(e) { |
159 | logger.warn('Exception on get status.response', {e: e, result: result}); | 159 | logger.warn('Exception on get status.response', {e: e, result: result}); |
160 | } | 160 | } |
161 | 161 | ||
162 | 162 | ||
163 | 163 | ||
164 | if (!callback) { | 164 | if (!callback) { |
165 | logger.warn('Invalid callback on checkstatus', {status: status}); | 165 | logger.warn('Invalid callback on checkstatus', {status: status}); |
166 | } else { | 166 | } else { |
167 | callback(status); | 167 | callback(status); |
168 | } | 168 | } |
169 | }); | 169 | }); |
170 | }); | 170 | }); |
171 | 171 | ||
172 | } | 172 | } |
173 | 173 | ||
174 | if (require.main === module) { | 174 | if (require.main === module) { |
175 | var data = { | 175 | var data = { |
176 | trxid: argv.trxid, | 176 | trxid: argv.trxid, |
177 | destination: argv.destination, | 177 | destination: argv.destination, |
178 | } | 178 | } |
179 | 179 | ||
180 | if (argv.trxid) { | 180 | if (argv.trxid) { |
181 | data.trxid = argv.trxid; | 181 | data.trxid = argv.trxid; |
182 | } | 182 | } |
183 | 183 | ||
184 | if (argv.destination) { | 184 | if (argv.destination) { |
185 | data.destinaton = argv.destination; | 185 | data.destinaton = argv.destination; |
186 | } | 186 | } |
187 | 187 | ||
188 | advice(data); | 188 | advice(data); |
189 | } | 189 | } |
190 | 190 | ||
191 | exports.setLogger = setLogger; | 191 | exports.setLogger = setLogger; |
192 | exports.advice = advice; | 192 | exports.advice = advice; |
193 | 193 |