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