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