Commit 974c2aabac6327dd6609312c68dc15b66d19a26d
1 parent
ef1ec70b56
Exists in
master
rc 00
Showing 1 changed file with 1 additions and 0 deletions Inline Diff
partner-komodo.js
1 | "use strict"; | 1 | "use strict"; |
2 | 2 | ||
3 | process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; | 3 | process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; |
4 | 4 | ||
5 | const http = require('http'); | 5 | const http = require('http'); |
6 | const url = require('url'); | 6 | const url = require('url'); |
7 | const request = require('request'); | 7 | const request = require('request'); |
8 | const resendDelay = require('sate24/resend-delay') | 8 | const resendDelay = require('sate24/resend-delay') |
9 | 9 | ||
10 | const komodoRc = { | 10 | const komodoRc = { |
11 | '00': '00', | ||
11 | '03': '40', | 12 | '03': '40', |
12 | '13': '13', | 13 | '13': '13', |
13 | '14': '14', | 14 | '14': '14', |
14 | '30': '40', | 15 | '30': '40', |
15 | '68': '68', | 16 | '68': '68', |
16 | '55': '55', | 17 | '55': '55', |
17 | '91': '91', | 18 | '91': '91', |
18 | '92': '40', | 19 | '92': '40', |
19 | '96': '68' | 20 | '96': '68' |
20 | } | 21 | } |
21 | 22 | ||
22 | var config; | 23 | var config; |
23 | var aaa; | 24 | var aaa; |
24 | var logger; | 25 | var logger; |
25 | 26 | ||
26 | function start(options) { | 27 | function start(options) { |
27 | if (!options) { | 28 | if (!options) { |
28 | console.log('Undefined options, terminating....'); | 29 | console.log('Undefined options, terminating....'); |
29 | process.exit(1); | 30 | process.exit(1); |
30 | } | 31 | } |
31 | 32 | ||
32 | if (options.config) { | 33 | if (options.config) { |
33 | config = options.config; | 34 | config = options.config; |
34 | } else { | 35 | } else { |
35 | console.log('Undefined options.config, terminating....') | 36 | console.log('Undefined options.config, terminating....') |
36 | process.exit(1); | 37 | process.exit(1); |
37 | } | 38 | } |
38 | 39 | ||
39 | if (options.aaa) { | 40 | if (options.aaa) { |
40 | aaa = options.aaa; | 41 | aaa = options.aaa; |
41 | } else { | 42 | } else { |
42 | console.log('Undefined options.aaa, terminating....') | 43 | console.log('Undefined options.aaa, terminating....') |
43 | process.exit(1); | 44 | process.exit(1); |
44 | } | 45 | } |
45 | 46 | ||
46 | if (options && options.logger) { | 47 | if (options && options.logger) { |
47 | logger = options.logger; | 48 | logger = options.logger; |
48 | } else { | 49 | } else { |
49 | console.log('Undefined options.logger, terminating....') | 50 | console.log('Undefined options.logger, terminating....') |
50 | process.exit(1); | 51 | process.exit(1); |
51 | } | 52 | } |
52 | 53 | ||
53 | resendDelay.init({config: config, logger: logger, topupRequest: topupAdvice}); | 54 | resendDelay.init({config: config, logger: logger, topupRequest: topupAdvice}); |
54 | 55 | ||
55 | createReverseHttpServer(); | 56 | createReverseHttpServer(); |
56 | 57 | ||
57 | } | 58 | } |
58 | 59 | ||
59 | function callbackReport(requestId, rc, message, options) { | 60 | function callbackReport(requestId, rc, message, options) { |
60 | aaa.callbackReportWithPushToMongoDb(requestId, rc, message); | 61 | aaa.callbackReportWithPushToMongoDb(requestId, rc, message); |
61 | 62 | ||
62 | /* | 63 | /* |
63 | if (!options.task) { | 64 | if (!options.task) { |
64 | return; | 65 | return; |
65 | } | 66 | } |
66 | 67 | ||
67 | if (rc == '68') { | 68 | if (rc == '68') { |
68 | resendDelay.register(options.task); | 69 | resendDelay.register(options.task); |
69 | } else { | 70 | } else { |
70 | resendDelay.cancel(options.task) | 71 | resendDelay.cancel(options.task) |
71 | } | 72 | } |
72 | */ | 73 | */ |
73 | 74 | ||
74 | } | 75 | } |
75 | 76 | ||
76 | function topupRequest(task, pendingOnConnectError) { | 77 | function topupRequest(task, pendingOnConnectError) { |
77 | aaa.insertTaskToMongoDb(task); | 78 | aaa.insertTaskToMongoDb(task); |
78 | 79 | ||
79 | const requestOptions = { | 80 | const requestOptions = { |
80 | url: config.h2h_out.partner, | 81 | url: config.h2h_out.partner, |
81 | qs: { | 82 | qs: { |
82 | terminal_name: config.h2h_out.terminal_name, | 83 | terminal_name: config.h2h_out.terminal_name, |
83 | password: config.h2h_out.password, | 84 | password: config.h2h_out.password, |
84 | product_name: task.remoteProduct, | 85 | product_name: task.remoteProduct, |
85 | destination: task.destination, | 86 | destination: task.destination, |
86 | request_id: task.requestId, | 87 | request_id: task.requestId, |
87 | reverse_url: config.h2h_out.reverse_url | 88 | reverse_url: config.h2h_out.reverse_url |
88 | } | 89 | } |
89 | } | 90 | } |
90 | 91 | ||
91 | logger.verbose('Requesting to partner', {request_options: requestOptions}); | 92 | logger.verbose('Requesting to partner', {request_options: requestOptions}); |
92 | 93 | ||
93 | request(requestOptions, function(err, response, body) { | 94 | request(requestOptions, function(err, response, body) { |
94 | if (err) { | 95 | if (err) { |
95 | logger.warn('Error requesting to partner', {err: err}); | 96 | logger.warn('Error requesting to partner', {err: err}); |
96 | 97 | ||
97 | let rc = '68'; | 98 | let rc = '68'; |
98 | 99 | ||
99 | if (err.syscall == 'connect' && !pendingOnConnectError) { | 100 | if (err.syscall == 'connect' && !pendingOnConnectError) { |
100 | rc = '91'; | 101 | rc = '91'; |
101 | } | 102 | } |
102 | callbackReport(task.requestId, rc, 'Error requesting to partner: ' + err, {task: task}); | 103 | callbackReport(task.requestId, rc, 'Error requesting to partner: ' + err, {task: task}); |
103 | return; | 104 | return; |
104 | } | 105 | } |
105 | 106 | ||
106 | if (response.statusCode != 200) { | 107 | if (response.statusCode != 200) { |
107 | let rc = '68'; | 108 | let rc = '68'; |
108 | 109 | ||
109 | callbackReport(task.requestId, rc, 'Partner returning HTTP status code ' + response.statusCode + ', not 200', {task: task}); | 110 | callbackReport(task.requestId, rc, 'Partner returning HTTP status code ' + response.statusCode + ', not 200', {task: task}); |
110 | return; | 111 | return; |
111 | } | 112 | } |
112 | 113 | ||
113 | let result = parsePartnerMessage(body); | 114 | let result = parsePartnerMessage(body); |
114 | 115 | ||
115 | if (!result) { | 116 | if (!result) { |
116 | callbackReport(task.requestId, '40', 'Error parsing response from partner. Partner response: ' + body, {task: task}); | 117 | callbackReport(task.requestId, '40', 'Error parsing response from partner. Partner response: ' + body, {task: task}); |
117 | return; | 118 | return; |
118 | } | 119 | } |
119 | 120 | ||
120 | processPartnerResponse(result, task); | 121 | processPartnerResponse(result, task); |
121 | 122 | ||
122 | }) | 123 | }) |
123 | } | 124 | } |
124 | 125 | ||
125 | function topupAdvice(task) { | 126 | function topupAdvice(task) { |
126 | topupRequest(task, true); | 127 | topupRequest(task, true); |
127 | } | 128 | } |
128 | 129 | ||
129 | function processPartnerResponse(resObj, task) { | 130 | function processPartnerResponse(resObj, task) { |
130 | let st24Rc = '68'; | 131 | let st24Rc = '68'; |
131 | 132 | ||
132 | if (komodoRc[resObj.rc]) { | 133 | if (komodoRc[resObj.rc]) { |
133 | st24Rc = komodoRc[resObj.rc]; | 134 | st24Rc = komodoRc[resObj.rc]; |
134 | } | 135 | } |
135 | 136 | ||
136 | let st24Message = resObj.message; | 137 | let st24Message = resObj.message; |
137 | if (resObj.sn) { | 138 | if (resObj.sn) { |
138 | st24Message = 'SN=' + resObj.sn + '; ' + st24Message; | 139 | st24Message = 'SN=' + resObj.sn + '; ' + st24Message; |
139 | } | 140 | } |
140 | 141 | ||
141 | callbackReport(resObj.request_id, st24Rc, st24Message, {task: task}); | 142 | callbackReport(resObj.request_id, st24Rc, st24Message, {task: task}); |
142 | } | 143 | } |
143 | 144 | ||
144 | function parsePartnerMessage(partner_message) { | 145 | function parsePartnerMessage(partner_message) { |
145 | let result; | 146 | let result; |
146 | try { | 147 | try { |
147 | result = JSON.parse(partner_message); | 148 | result = JSON.parse(partner_message); |
148 | 149 | ||
149 | } | 150 | } |
150 | catch(e) { | 151 | catch(e) { |
151 | logger.verbose('Exception on parsing partner message: ' + partner_message); | 152 | logger.verbose('Exception on parsing partner message: ' + partner_message); |
152 | result = null; | 153 | result = null; |
153 | } | 154 | } |
154 | 155 | ||
155 | return result; | 156 | return result; |
156 | } | 157 | } |
157 | 158 | ||
158 | function createReverseHttpServer() { | 159 | function createReverseHttpServer() { |
159 | let listenPort = config.h2h_out.listen_port; | 160 | let listenPort = config.h2h_out.listen_port; |
160 | 161 | ||
161 | http.createServer(onReverseReport).listen(listenPort, function() { | 162 | http.createServer(onReverseReport).listen(listenPort, function() { |
162 | logger.info('HTTP Reverse/Report server listen on port ' + listenPort); | 163 | logger.info('HTTP Reverse/Report server listen on port ' + listenPort); |
163 | }); | 164 | }); |
164 | } | 165 | } |
165 | 166 | ||
166 | function onReverseReport(request, response) { | 167 | function onReverseReport(request, response) { |
167 | response.end('OK'); | 168 | response.end('OK'); |
168 | 169 | ||
169 | const qs = url.parse(request.url, true).query; | 170 | const qs = url.parse(request.url, true).query; |
170 | logger.verbose('Got reverse report from partner', {qs: qs}); | 171 | logger.verbose('Got reverse report from partner', {qs: qs}); |
171 | processPartnerResponse(qs); | 172 | processPartnerResponse(qs); |
172 | } | 173 | } |
173 | 174 | ||
174 | 175 | ||
175 | exports.start = start; | 176 | exports.start = start; |
176 | exports.topupRequest = topupRequest; | 177 | exports.topupRequest = topupRequest; |
177 | exports.topupAdvice = topupAdvice; | 178 | exports.topupAdvice = topupAdvice; |
178 | 179 |