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