aaa.js
3.07 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
var request = require('request');
var strftime = require('strftime');
var config;
var partner;
var available_products = [];
function pullCity() {
var url = config.globals.aaa_host + '/pull_city';
console.log('Pull cities from AAA - ' + url);
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
//console.log('city=' + body);
} else {
console.log('Error in pull city');
}
});
}
function pullProduct() {
var url = config.globals.aaa_host + '/pull_product?opr_name=' + config.globals.operators;
console.log('Pull products from AAA - ' + url);
request(url, function (error, response, body) {
if (error || response.statusCode != 200) {
console.log('Error in pull products');
return;
}
var productsAndOperators = body.split(';');
var productsCount = productsAndOperators.length;
for (var i=0; i < productsCount; i++) {
var product = productsAndOperators[i].split(',', 1)[0];
available_products.push(product);
}
//console.log(available_products);
});
}
function pullLoop() {
var url = config.globals.aaa_host
+ '/pull?city=ALL&nom=' + config.globals.products
+ '&chip_info=' + config.globals.gateway_name;
//console.log('AAA PULL - ' + url);
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
if (body == 'NONE') {
return;
}
console.log(body);
var result = body.split(';');
if (result[0] != 'OK') {
return;
}
var task = [];
task['requestId'] = result[1];
task['timestamp'] = result[3];
task['destination'] = result[4];
task['product'] = result[7];
if (config.products[task['product']] !== undefined) {
task['remoteProduct'] = config.products[task['product']];
} else {
task['remoteProduct'] = task['product'];
}
partner.topupRequest(task);
} else {
console.log('Error in pull task');
return;
}
});
setTimeout(pullLoop, config.globals.interval);
}
function callbackReport(requestId, responseCode, message) {
timestamp = strftime('%Y%m%d%H%M%S');
var url = config.globals.aaa_host
+ '/topup?trans_id=' + requestId
+ '&trans_date' + timestamp
+ '&resp_code=' + responseCode
+ '&ussd_msg=' + config.globals.gateway_name
+ '$' + encodeURIComponent(message);
console.log('Report to AAA - ' + url);
request(url, function (error, response, body) {
if (error || response.statusCode != 200) {
console.log('Error report to AAA');
}
});
}
function start(_config, _partner) {
config = _config;
partner = _partner;
pullCity();
pullProduct();
setTimeout(pullLoop, 10 * 1000);
}
exports.start = start;
exports.callbackReport = callbackReport;