Commit 6cef2ccead04743c425d8901d19942c42e507f85
1 parent
9f96df237a
Exists in
master
topUpInquiry
Showing 1 changed file with 88 additions and 8 deletions Side-by-side Diff
xmlrpc-server.js
... | ... | @@ -102,13 +102,26 @@ function createDiyHttpXmlRpcServer() { |
102 | 102 | params = getXmlRpcParam(_params); |
103 | 103 | logger.verbose('Got XMLRPC request', {method: method, params: params}); |
104 | 104 | |
105 | - sendToMaster(params, req.connection.remoteAddress, function(forwardError) { | |
106 | - if (forwardError) { | |
107 | - immediateReply(params, res, '40', forwardError.toString()); | |
108 | - } else { | |
109 | - immediateReply(params, res, '68'); | |
110 | - } | |
111 | - }); | |
105 | + if (method == 'topUpRequest') { | |
106 | + | |
107 | + sendTopUpRequestToMaster(params, req.connection.remoteAddress, function(forwardError) { | |
108 | + if (forwardError) { | |
109 | + immediateReply(params, res, '40', forwardError.toString()); | |
110 | + } else { | |
111 | + immediateReply(params, res, '68'); | |
112 | + } | |
113 | + }); | |
114 | + | |
115 | + } | |
116 | + else if (method == 'topUpInquiry') { | |
117 | + | |
118 | + sendTopUpInquryToMaster(params, req.connection.remoteAddress, res); | |
119 | + | |
120 | + } | |
121 | + else { | |
122 | + res.end('Invalid method'); | |
123 | + } | |
124 | + | |
112 | 125 | |
113 | 126 | }) |
114 | 127 | }); |
... | ... | @@ -205,7 +218,74 @@ function composeMessage(params, remoteAddress) { |
205 | 218 | |
206 | 219 | } |
207 | 220 | |
208 | -function sendToMaster(param, remoteAddress, callback) { | |
221 | +function inquiryReply(responseCode, message, responseToClient) { | |
222 | + var responseData = { | |
223 | + 'RESPONSECODE': responseCode, | |
224 | + 'MESSAGE': message | |
225 | + } | |
226 | + | |
227 | + var responseBody = composeXmlRpcResponse(responseData) | |
228 | + logger.info("Sending inquiry response", {responseData: responseData}); | |
229 | + | |
230 | + responseToClient.writeHead(200, {'Content-Type': 'text/xml'}); | |
231 | + responseToClient.end(responseBody); | |
232 | +} | |
233 | + | |
234 | +function parseTopUpInquiryResponse(raw) { | |
235 | + var tokens = raw.split('$'); | |
236 | + if (tokens.length < 2) { | |
237 | + return { | |
238 | + responseCode: '68', | |
239 | + message: 'Gagal parsing hasil inquiry' | |
240 | + } | |
241 | + } | |
242 | + | |
243 | + var responseCode = tokens[0]; | |
244 | + var message = ''; | |
245 | + | |
246 | + for (var i = 1; i < tokens.length; i++) { | |
247 | + message += '$' + tokens[i]; | |
248 | + } | |
249 | + | |
250 | + return { | |
251 | + responseCode: responseCode, | |
252 | + message: message.replace('$', '') | |
253 | + } | |
254 | +} | |
255 | + | |
256 | +function sendTopUpInquryToMaster(params, remoteAddress, responseToClient) { | |
257 | + if (!config.inquiry_url) { | |
258 | + inquiryReply('68', 'Inquiry server tidak terdaftar'); | |
259 | + return; | |
260 | + } | |
261 | + | |
262 | + var requestOpts = { | |
263 | + url: config.inquiry_url, | |
264 | + qs: { | |
265 | + REQUESTID: params.REQUESTID, | |
266 | + MSISDN: param.MSISDN, | |
267 | + PIN: params.PIN, | |
268 | + NO_HP: params.NO_HP, | |
269 | + TRUST: 'NO_TRUST', | |
270 | + IP_ADDR: remoteAddress | |
271 | + } | |
272 | + }; | |
273 | + | |
274 | + request(requestOpts, function(requestError, response, body) { | |
275 | + | |
276 | + if (requestError) { | |
277 | + logger.warn('Error requesting to inquiry server: ' + requestError); | |
278 | + inquiryReply('68', 'Gagal konek ke inquiry server', responseToClient); | |
279 | + return; | |
280 | + } | |
281 | + | |
282 | + logger.info('Got response from inquiry server', {body: body, requestOpts: requestOpts}); | |
283 | + var params = parseTopUpInquiryResponse(body); | |
284 | + inquiryReply(params.responseCode, params.message, responseToClient); | |
285 | + }) | |
286 | +} | |
287 | + | |
288 | +function sendTopUpRequestToMaster(param, remoteAddress, callback) { | |
209 | 289 | |
210 | 290 | /* |
211 | 291 | var smscidSuffix = '99999999999999' + String(Math.round(Math.random() * 99999999999999)); |