Commit c43b42215495f291a0e86acb635884d160650ebd
0 parents
Exists in
master
initial commit
Showing 5 changed files with 312 additions and 0 deletions Side-by-side Diff
aaa.js
... | ... | @@ -0,0 +1,111 @@ |
1 | +var request = require('request'); | |
2 | +var strftime = require('strftime'); | |
3 | + | |
4 | +var config; | |
5 | +var partner; | |
6 | + | |
7 | +var available_products = []; | |
8 | + | |
9 | +function pullCity() { | |
10 | + var url = config.globals.aaa_host + '/pull_city'; | |
11 | + console.log('Pull cities from AAA - ' + url); | |
12 | + request(url, function (error, response, body) { | |
13 | + if (!error && response.statusCode == 200) { | |
14 | + //console.log('city=' + body); | |
15 | + } else { | |
16 | + console.log('Error in pull city'); | |
17 | + } | |
18 | + }); | |
19 | +} | |
20 | + | |
21 | +function pullProduct() { | |
22 | + var url = config.globals.aaa_host + '/pull_product?opr_name=' + config.globals.operators; | |
23 | + console.log('Pull products from AAA - ' + url); | |
24 | + | |
25 | + request(url, function (error, response, body) { | |
26 | + if (error || response.statusCode != 200) { | |
27 | + console.log('Error in pull products'); | |
28 | + return; | |
29 | + } | |
30 | + | |
31 | + var productsAndOperators = body.split(';'); | |
32 | + var productsCount = productsAndOperators.length; | |
33 | + | |
34 | + for (var i=0; i < productsCount; i++) { | |
35 | + var product = productsAndOperators[i].split(',', 1)[0]; | |
36 | + available_products.push(product); | |
37 | + } | |
38 | + //console.log(available_products); | |
39 | + }); | |
40 | +} | |
41 | + | |
42 | +function pullLoop() { | |
43 | + var url = config.globals.aaa_host | |
44 | + + '/pull?city=ALL&nom=' + config.globals.products | |
45 | + + '&chip_info=' + config.globals.gateway_name; | |
46 | + | |
47 | + //console.log('AAA PULL - ' + url); | |
48 | + request(url, function (error, response, body) { | |
49 | + if (!error && response.statusCode == 200) { | |
50 | + if (body == 'NONE') { | |
51 | + return; | |
52 | + } | |
53 | + console.log(body); | |
54 | + | |
55 | + var result = body.split(';'); | |
56 | + if (result[0] != 'OK') { | |
57 | + return; | |
58 | + } | |
59 | + | |
60 | + var task = []; | |
61 | + task['requestId'] = result[1]; | |
62 | + task['timestamp'] = result[3]; | |
63 | + task['destination'] = result[4]; | |
64 | + task['product'] = result[7]; | |
65 | + | |
66 | + if (config.products[task['product']] !== undefined) { | |
67 | + task['remoteProduct'] = config.products[task['product']]; | |
68 | + } else { | |
69 | + task['remoteProduct'] = task['product']; | |
70 | + } | |
71 | + | |
72 | + partner.topupRequest(task); | |
73 | + | |
74 | + } else { | |
75 | + console.log('Error in pull task'); | |
76 | + return; | |
77 | + } | |
78 | + }); | |
79 | + | |
80 | + setTimeout(pullLoop, config.globals.interval); | |
81 | +} | |
82 | + | |
83 | +function callbackReport(requestId, responseCode, message) { | |
84 | + timestamp = strftime('%Y%m%d%H%M%S'); | |
85 | + var url = config.globals.aaa_host | |
86 | + + '/topup?trans_id=' + requestId | |
87 | + + '&trans_date' + timestamp | |
88 | + + '&resp_code=' + responseCode | |
89 | + + '&ussd_msg=' + config.globals.gateway_name | |
90 | + + '$' + encodeURIComponent(message); | |
91 | + | |
92 | + console.log('Report to AAA - ' + url); | |
93 | + request(url, function (error, response, body) { | |
94 | + if (error || response.statusCode != 200) { | |
95 | + console.log('Error report to AAA'); | |
96 | + } | |
97 | + }); | |
98 | +} | |
99 | + | |
100 | +function start(_config, _partner) { | |
101 | + config = _config; | |
102 | + partner = _partner; | |
103 | + | |
104 | + pullCity(); | |
105 | + pullProduct(); | |
106 | + | |
107 | + setTimeout(pullLoop, 10 * 1000); | |
108 | +} | |
109 | + | |
110 | +exports.start = start; | |
111 | +exports.callbackReport = callbackReport; |
config.sample.ini
... | ... | @@ -0,0 +1,18 @@ |
1 | +[globals] | |
2 | +operators=TEST,XL_ALTERNATIF | |
3 | +products=TST1 | |
4 | +gateway_name=NODEJS-DEV | |
5 | +aaa_host=http://172.23.0.12:4250 | |
6 | +interval=1000 | |
7 | +admin_port=17456 | |
8 | + | |
9 | +[h2h_out] | |
10 | +partner=https://172.23.0.12:6789 | |
11 | +userid=R97DEV | |
12 | +password=czcb | |
13 | +listen_port=13522 | |
14 | +check_interval=2000 | |
15 | + | |
16 | +[products] | |
17 | +TST1=TR1 | |
18 | + |
httpserver.js
... | ... | @@ -0,0 +1,25 @@ |
1 | +var http = require('http'); | |
2 | +var nsr = require('node-simple-router'); | |
3 | +var router = nsr(); | |
4 | + | |
5 | +var config; | |
6 | +var httpServer; | |
7 | + | |
8 | +function start(_config) { | |
9 | + if (_config != undefined) { | |
10 | + config = _config; | |
11 | + } | |
12 | + listenPort = config.globals.admin_port; | |
13 | + | |
14 | + httpServer = http.createServer(router).listen(listenPort); | |
15 | + console.log('HTTP server listens on port ' + listenPort); | |
16 | + | |
17 | + return httpServer; | |
18 | +} | |
19 | + | |
20 | +function setConfig(_config) { | |
21 | + config = _config; | |
22 | +} | |
23 | + | |
24 | +exports.start = start; | |
25 | +exports.setConfig = setConfig; |
index.js
... | ... | @@ -0,0 +1,17 @@ |
1 | +var iniparser = require('iniparser'); | |
2 | +var config = iniparser.parseSync('./config.ini'); | |
3 | + | |
4 | +var aaaHost = config.globals.aaa_host; | |
5 | + | |
6 | +HttpServer = require('./httpserver.js'); | |
7 | +var httpServer = HttpServer.start(config); | |
8 | + | |
9 | +var aaa = require('./aaa.js'); | |
10 | +var xmlout = require('./xmlout.js'); | |
11 | + | |
12 | +xmlout.start(config, aaa.callbackReport); | |
13 | +aaa.start(config, xmlout); | |
14 | + | |
15 | + | |
16 | + | |
17 | + |
xmlout.js
... | ... | @@ -0,0 +1,141 @@ |
1 | +var xmlrpc = require('xmlrpc'); | |
2 | +var url = require('url'); | |
3 | +var config; | |
4 | +var callbackReport; | |
5 | + | |
6 | +process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; | |
7 | + | |
8 | +function topupRequest(task) { | |
9 | + | |
10 | + var partnerUrl = url.parse(config.h2h_out.partner); | |
11 | + var clientOptions = { | |
12 | + host: partnerUrl.hostname | |
13 | + , port: partnerUrl.port | |
14 | + , path: partnerUrl.pathname | |
15 | + }; | |
16 | + console.log('XMLRPC client options:'); | |
17 | + console.log(clientOptions); | |
18 | + | |
19 | + var client; | |
20 | + if (partnerUrl.protocol == 'https:') { | |
21 | + console.log('Use SSL'); | |
22 | + client = xmlrpc.createSecureClient(clientOptions); | |
23 | + } else { | |
24 | + console.log('Not using SSL'); | |
25 | + client = xmlrpc.createClient(clientOptions); | |
26 | + } | |
27 | + | |
28 | + var methodName = 'topUpRequest'; | |
29 | + console.log('methodName: ' + methodName); | |
30 | + | |
31 | + var params = { | |
32 | + MSISDN: config.h2h_out.userid, | |
33 | + REQUESTID: task['requestId'], | |
34 | + PIN: config.h2h_out.password, | |
35 | + NOHP: task['destination'], | |
36 | + NOM: task['remoteProduct'] | |
37 | + }; | |
38 | + console.log('PARAMS:'); | |
39 | + console.log(params); | |
40 | + | |
41 | + | |
42 | + client.methodCall(methodName, [ params ], function (error, value) { | |
43 | + // Results of the method response | |
44 | + if (error) { | |
45 | + console.log('Error: '); | |
46 | + console.log(error); | |
47 | + return; | |
48 | + } | |
49 | + console.log('Method response for \'' + methodName + '\': ') | |
50 | + console.log(value); | |
51 | + | |
52 | + callbackReport(value['REQUESTID'], value['RESPONSECODE'], value['MESSAGE']); | |
53 | + }); | |
54 | +} | |
55 | + | |
56 | +function createServer() { | |
57 | + | |
58 | + console.log('Creating XML-RPC server on port ' + config.h2h_out.listen_port); | |
59 | + var serverOptions = { | |
60 | + port: config.h2h_out.listen_port | |
61 | + }; | |
62 | + | |
63 | + var server = xmlrpc.createServer(serverOptions); | |
64 | + | |
65 | + server.on('NotFound', function (method, params) { | |
66 | + console.log(method + ' is not found on our XML-RPC server'); | |
67 | + console.log('params:'); | |
68 | + console.log(params); | |
69 | + }); | |
70 | + | |
71 | + server.on('topUpReport', function (err, params, callback) { | |
72 | + console.log('RECEIVE topUpReport, params:'); | |
73 | + console.log(params); | |
74 | + | |
75 | + var paramscount = params.length; | |
76 | + for (var i = 0; i < paramscount; i++) { | |
77 | + var value = params[i]; | |
78 | + | |
79 | + callbackReport(value['REQUESTID'], value['RESPONSECODE'], value['MESSAGE']); | |
80 | + } | |
81 | + | |
82 | + callback(null, 'ACK REPORT OK'); | |
83 | + }) | |
84 | + | |
85 | +} | |
86 | + | |
87 | +function checkStatus(task) { | |
88 | + var partnerUrl = url.parse(config.h2h_out.partner); | |
89 | + var clientOptions = { | |
90 | + host: partnerUrl.hostname | |
91 | + , port: partnerUrl.port | |
92 | + , path: partnerUrl.pathname | |
93 | + }; | |
94 | + console.log('XMLRPC client options:'); | |
95 | + console.log(clientOptions); | |
96 | + | |
97 | + var client; | |
98 | + if (partnerUrl.protocol == 'https:') { | |
99 | + console.log('Use SSL'); | |
100 | + client = xmlrpc.createSecureClient(clientOptions); | |
101 | + } else { | |
102 | + console.log('Not using SSL'); | |
103 | + client = xmlrpc.createClient(clientOptions); | |
104 | + } | |
105 | + | |
106 | + var methodName = 'topUpInquiry'; | |
107 | + console.log('methodName: ' + methodName); | |
108 | + | |
109 | + var params = { | |
110 | + MSISDN: config.h2h_out.userid, | |
111 | + REQUESTID: task['requestId'], | |
112 | + PIN: config.h2h_out.password, | |
113 | + NOHP: task['destination'] | |
114 | + }; | |
115 | + console.log('PARAMS:'); | |
116 | + console.log(params); | |
117 | + | |
118 | + client.methodCall(methodName, [ params ], function (error, value) { | |
119 | + // Results of the method response | |
120 | + if (error) { | |
121 | + console.log('Error: '); | |
122 | + console.log(error); | |
123 | + return; | |
124 | + } | |
125 | + console.log('Method response for \'' + methodName + '\': ') | |
126 | + console.log(value); | |
127 | + | |
128 | + callbackReport(value['REQUESTID'], value['RESPONSECODE'], value['MESSAGE']); | |
129 | + }); | |
130 | +} | |
131 | + | |
132 | +function start(_config, _callbackReport) { | |
133 | + config = _config; | |
134 | + callbackReport = _callbackReport | |
135 | + | |
136 | + createServer(); | |
137 | +} | |
138 | + | |
139 | + | |
140 | +exports.start = start; | |
141 | +exports.topupRequest = topupRequest; |