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