partner-komodo.js
4.54 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
"use strict";
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
const http = require('http');
const url = require('url');
const request = require('request');
const resendDelay = require('sate24/resend-delay')
const komodoRc = {
'00': '00',
'03': '40',
'13': '13',
'14': '14',
'30': '40',
'40': '40',
'68': '68',
'55': '55',
'91': '91',
'92': '40',
'96': '68'
}
var config;
var aaa;
var logger;
function start(options) {
if (!options) {
console.log('Undefined options, terminating....');
process.exit(1);
}
if (options.config) {
config = options.config;
} else {
console.log('Undefined options.config, terminating....')
process.exit(1);
}
if (options.aaa) {
aaa = options.aaa;
} else {
console.log('Undefined options.aaa, terminating....')
process.exit(1);
}
if (options && options.logger) {
logger = options.logger;
} else {
console.log('Undefined options.logger, terminating....')
process.exit(1);
}
resendDelay.init({config: config, logger: logger, topupRequest: topupAdvice});
createReverseHttpServer();
}
function callbackReport(requestId, rc, message, options) {
aaa.callbackReportWithPushToMongoDb(requestId, rc, message);
if (rc === '68') {
if (options && options.task) resendDelay.register(options.task);
} else {
resendDelay.cancel(requestId);
}
}
function topupRequest(task, pendingOnConnectError) {
aaa.insertTaskToMongoDb(task);
const requestOptions = {
url: config.h2h_out.partner,
qs: {
terminal_name: config.h2h_out.terminal_name,
password: config.h2h_out.password,
product_name: task.remoteProduct,
destination: task.destination,
request_id: task.requestId,
reverse_url: config.h2h_out.reverse_url
}
}
logger.verbose('Requesting to partner', {request_options: requestOptions});
request(requestOptions, function(err, response, body) {
if (err) {
logger.warn('Error requesting to partner', {err: err});
let rc = '68';
if (err.syscall == 'connect' && !pendingOnConnectError) {
rc = '91';
}
callbackReport(task.requestId, rc, 'Error requesting to partner: ' + err, {task: task});
return;
}
if (response.statusCode != 200) {
let rc = '68';
callbackReport(task.requestId, rc, 'Partner returning HTTP status code ' + response.statusCode + ', not 200', {task: task});
return;
}
logger.verbose('Got response from partner', {task: task, body: body});
let result = parsePartnerMessage(body);
if (!result) {
callbackReport(task.requestId, '40', 'Error parsing response from partner. Partner response: ' + body, {task: task});
return;
}
processPartnerResponse(result, task);
})
}
function topupAdvice(task) {
topupRequest(task, true);
}
function processPartnerResponse(resObj, task) {
let st24Rc = '40';
if (!resObj || !resObj.rc) {
st24Rc = '68';
}
else if (komodoRc[resObj.rc]) {
st24Rc = komodoRc[resObj.rc];
}
let st24Message = resObj.message;
if (resObj && resObj.rc) {
st24Message += '| KOMODORC: ' + resObj.rc;
}
if (resObj.sn) {
let sn = resObj.sn;
if (sn.indexOf('/') >= 0) {
sn = sn.trim().replace(/[\s-]+/g, '-');
}
st24Message = 'SN=' + sn + '; ' + st24Message;
}
callbackReport(resObj.request_id, st24Rc, st24Message, {task: task});
}
function parsePartnerMessage(partner_message) {
let result;
try {
result = JSON.parse(partner_message);
}
catch(e) {
logger.verbose('Exception on parsing partner message: ' + partner_message);
result = null;
}
return result;
}
function createReverseHttpServer() {
let listenPort = config.h2h_out.listen_port;
http.createServer(onReverseReport).listen(listenPort, function() {
logger.info('HTTP Reverse/Report server listen on port ' + listenPort);
});
}
function onReverseReport(request, response) {
response.end('OK');
const qs = url.parse(request.url, true).query;
qs.message = 'REVERSE REPORT: ' + qs.message;
logger.verbose('Got reverse report from partner', {qs: qs});
processPartnerResponse(qs);
}
exports.start = start;
exports.topupRequest = topupRequest;
exports.topupAdvice = topupAdvice;