Commit ef1ec70b5612f418616401978a09757829cc9fa9
1 parent
dd7844a895
Exists in
master
reverse url pada request
Showing 1 changed file with 2 additions and 1 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 | /* |
63 | if (!options.task) { | 63 | if (!options.task) { |
64 | return; | 64 | return; |
65 | } | 65 | } |
66 | 66 | ||
67 | if (rc == '68') { | 67 | if (rc == '68') { |
68 | resendDelay.register(options.task); | 68 | resendDelay.register(options.task); |
69 | } else { | 69 | } else { |
70 | resendDelay.cancel(options.task) | 70 | resendDelay.cancel(options.task) |
71 | } | 71 | } |
72 | */ | 72 | */ |
73 | 73 | ||
74 | } | 74 | } |
75 | 75 | ||
76 | function topupRequest(task, pendingOnConnectError) { | 76 | function topupRequest(task, pendingOnConnectError) { |
77 | aaa.insertTaskToMongoDb(task); | 77 | aaa.insertTaskToMongoDb(task); |
78 | 78 | ||
79 | const requestOptions = { | 79 | const requestOptions = { |
80 | url: config.h2h_out.partner, | 80 | url: config.h2h_out.partner, |
81 | qs: { | 81 | qs: { |
82 | terminal_name: config.h2h_out.terminal_name, | 82 | terminal_name: config.h2h_out.terminal_name, |
83 | password: config.h2h_out.password, | 83 | password: config.h2h_out.password, |
84 | product_name: task.remoteProduct, | 84 | product_name: task.remoteProduct, |
85 | destination: task.destination, | 85 | destination: task.destination, |
86 | request_id: task.requestId | 86 | request_id: task.requestId, |
87 | reverse_url: config.h2h_out.reverse_url | ||
87 | } | 88 | } |
88 | } | 89 | } |
89 | 90 | ||
90 | logger.verbose('Requesting to partner', {request_options: requestOptions}); | 91 | logger.verbose('Requesting to partner', {request_options: requestOptions}); |
91 | 92 | ||
92 | request(requestOptions, function(err, response, body) { | 93 | request(requestOptions, function(err, response, body) { |
93 | if (err) { | 94 | if (err) { |
94 | logger.warn('Error requesting to partner', {err: err}); | 95 | logger.warn('Error requesting to partner', {err: err}); |
95 | 96 | ||
96 | let rc = '68'; | 97 | let rc = '68'; |
97 | 98 | ||
98 | if (err.syscall == 'connect' && !pendingOnConnectError) { | 99 | if (err.syscall == 'connect' && !pendingOnConnectError) { |
99 | rc = '91'; | 100 | rc = '91'; |
100 | } | 101 | } |
101 | callbackReport(task.requestId, rc, 'Error requesting to partner: ' + err, {task: task}); | 102 | callbackReport(task.requestId, rc, 'Error requesting to partner: ' + err, {task: task}); |
102 | return; | 103 | return; |
103 | } | 104 | } |
104 | 105 | ||
105 | if (response.statusCode != 200) { | 106 | if (response.statusCode != 200) { |
106 | let rc = '68'; | 107 | let rc = '68'; |
107 | 108 | ||
108 | callbackReport(task.requestId, rc, 'Partner returning HTTP status code ' + response.statusCode + ', not 200', {task: task}); | 109 | callbackReport(task.requestId, rc, 'Partner returning HTTP status code ' + response.statusCode + ', not 200', {task: task}); |
109 | return; | 110 | return; |
110 | } | 111 | } |
111 | 112 | ||
112 | let result = parsePartnerMessage(body); | 113 | let result = parsePartnerMessage(body); |
113 | 114 | ||
114 | if (!result) { | 115 | if (!result) { |
115 | callbackReport(task.requestId, '40', 'Error parsing response from partner. Partner response: ' + body, {task: task}); | 116 | callbackReport(task.requestId, '40', 'Error parsing response from partner. Partner response: ' + body, {task: task}); |
116 | return; | 117 | return; |
117 | } | 118 | } |
118 | 119 | ||
119 | processPartnerResponse(result, task); | 120 | processPartnerResponse(result, task); |
120 | 121 | ||
121 | }) | 122 | }) |
122 | } | 123 | } |
123 | 124 | ||
124 | function topupAdvice(task) { | 125 | function topupAdvice(task) { |
125 | topupRequest(task, true); | 126 | topupRequest(task, true); |
126 | } | 127 | } |
127 | 128 | ||
128 | function processPartnerResponse(resObj, task) { | 129 | function processPartnerResponse(resObj, task) { |
129 | let st24Rc = '68'; | 130 | let st24Rc = '68'; |
130 | 131 | ||
131 | if (komodoRc[resObj.rc]) { | 132 | if (komodoRc[resObj.rc]) { |
132 | st24Rc = komodoRc[resObj.rc]; | 133 | st24Rc = komodoRc[resObj.rc]; |
133 | } | 134 | } |
134 | 135 | ||
135 | let st24Message = resObj.message; | 136 | let st24Message = resObj.message; |
136 | if (resObj.sn) { | 137 | if (resObj.sn) { |
137 | st24Message = 'SN=' + resObj.sn + '; ' + st24Message; | 138 | st24Message = 'SN=' + resObj.sn + '; ' + st24Message; |
138 | } | 139 | } |
139 | 140 | ||
140 | callbackReport(resObj.request_id, st24Rc, st24Message, {task: task}); | 141 | callbackReport(resObj.request_id, st24Rc, st24Message, {task: task}); |
141 | } | 142 | } |
142 | 143 | ||
143 | function parsePartnerMessage(partner_message) { | 144 | function parsePartnerMessage(partner_message) { |
144 | let result; | 145 | let result; |
145 | try { | 146 | try { |
146 | result = JSON.parse(partner_message); | 147 | result = JSON.parse(partner_message); |
147 | 148 | ||
148 | } | 149 | } |
149 | catch(e) { | 150 | catch(e) { |
150 | logger.verbose('Exception on parsing partner message: ' + partner_message); | 151 | logger.verbose('Exception on parsing partner message: ' + partner_message); |
151 | result = null; | 152 | result = null; |
152 | } | 153 | } |
153 | 154 | ||
154 | return result; | 155 | return result; |
155 | } | 156 | } |
156 | 157 | ||
157 | function createReverseHttpServer() { | 158 | function createReverseHttpServer() { |
158 | let listenPort = config.h2h_out.listen_port; | 159 | let listenPort = config.h2h_out.listen_port; |
159 | 160 | ||
160 | http.createServer(onReverseReport).listen(listenPort, function() { | 161 | http.createServer(onReverseReport).listen(listenPort, function() { |
161 | logger.info('HTTP Reverse/Report server listen on port ' + listenPort); | 162 | logger.info('HTTP Reverse/Report server listen on port ' + listenPort); |
162 | }); | 163 | }); |
163 | } | 164 | } |
164 | 165 | ||
165 | function onReverseReport(request, response) { | 166 | function onReverseReport(request, response) { |
166 | response.end('OK'); | 167 | response.end('OK'); |
167 | 168 | ||
168 | const qs = url.parse(request.url, true).query; | 169 | const qs = url.parse(request.url, true).query; |
169 | logger.verbose('Got reverse report from partner', {qs: qs}); | 170 | logger.verbose('Got reverse report from partner', {qs: qs}); |
170 | processPartnerResponse(qs); | 171 | processPartnerResponse(qs); |
171 | } | 172 | } |
172 | 173 | ||
173 | 174 | ||
174 | exports.start = start; | 175 | exports.start = start; |
175 | exports.topupRequest = topupRequest; | 176 | exports.topupRequest = topupRequest; |
176 | exports.topupAdvice = topupAdvice; | 177 | exports.topupAdvice = topupAdvice; |
177 | 178 |