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