Commit c187523945d1296ae82a89a7effed6fb6133eaf8
1 parent
4daddde925
Exists in
master
insertTaskToMongoDb dan callbackReportWithPushToMongoDb
Showing 2 changed files with 47 additions and 33 deletions Side-by-side Diff
index.js
partner-trugee.js
... | ... | @@ -22,13 +22,13 @@ var trx_prices = {}; |
22 | 22 | function calculateSignature(password, msisdn, timestamp) { |
23 | 23 | var a = timestamp + msisdn.substr(msisdn.length - 4); |
24 | 24 | var b = msisdn.substr(msisdn.length - 4).split('').reverse().join('') + password; |
25 | - | |
25 | + | |
26 | 26 | return xor.encode(a,b); |
27 | 27 | } |
28 | 28 | |
29 | 29 | function createPayload(task) { |
30 | 30 | var timestamp = strftime('%H%M%S'); |
31 | - | |
31 | + | |
32 | 32 | var payload = { |
33 | 33 | pulsamatic: [ |
34 | 34 | {command: 'TOPUP'}, |
... | ... | @@ -40,16 +40,18 @@ function createPayload(task) { |
40 | 40 | {sign: calculateSignature(config.h2h_out.password, task['destination'], timestamp)} |
41 | 41 | ] |
42 | 42 | }; |
43 | - | |
43 | + | |
44 | 44 | //console.log(payload); |
45 | 45 | return "<?xml version=\"1.0\" ?>\n" + xml(payload); |
46 | 46 | } |
47 | 47 | |
48 | 48 | function topupRequest(task, retry) { |
49 | - | |
50 | - var payload_xml = createPayload(task); | |
49 | + | |
50 | + aaa.insertTaskToMongoDb(task); | |
51 | + | |
52 | + var payload_xml = createPayload(task); | |
51 | 53 | var partner = url.parse(config.h2h_out.partner); |
52 | - | |
54 | + | |
53 | 55 | var request_options = { |
54 | 56 | host: partner.hostname, |
55 | 57 | path: partner.path, |
... | ... | @@ -60,17 +62,17 @@ function topupRequest(task, retry) { |
60 | 62 | 'Content-Length': Buffer.byteLength(payload_xml) |
61 | 63 | } |
62 | 64 | }; |
63 | - | |
65 | + | |
64 | 66 | var buffer = ""; |
65 | - | |
67 | + | |
66 | 68 | logger.info('Requesting to partner', {request_options: request_options}); |
67 | - | |
69 | + | |
68 | 70 | var req = http.request(request_options, function( res ) { |
69 | 71 | |
70 | 72 | logger.info('Status code: ' + res.statusCode ); |
71 | 73 | var buffer = ""; |
72 | 74 | res.on( "data", function( data ) { buffer = buffer + data; } ); |
73 | - res.on( "end", function( data ) { | |
75 | + res.on( "end", function( data ) { | |
74 | 76 | directResponseHandler(buffer, task['requestId']); |
75 | 77 | }); |
76 | 78 | |
... | ... | @@ -94,25 +96,25 @@ function directResponseHandler(body, request_id) { |
94 | 96 | callbackReport(request_id, '40', buffer); |
95 | 97 | return; |
96 | 98 | } |
97 | - | |
99 | + | |
98 | 100 | logger.info('Direct response parsed', {result: result}); |
99 | - | |
101 | + | |
100 | 102 | var response_code = '68'; |
101 | - | |
103 | + | |
102 | 104 | var request_id = result.pulsamatic.partner_trxid[0].trim(); |
103 | 105 | var message = result.pulsamatic.message[0].trim(); |
104 | 106 | var status = result.pulsamatic.result[0].trim(); |
105 | - | |
107 | + | |
106 | 108 | if (status === 'failed') { |
107 | 109 | response_code = '40'; |
108 | - | |
110 | + | |
109 | 111 | var new_response_code = responseCodeFromMessage(message); |
110 | 112 | if (new_response_code) { |
111 | 113 | response_code = new_response_code; |
112 | 114 | } |
113 | 115 | |
114 | 116 | } |
115 | - | |
117 | + | |
116 | 118 | callbackReport(request_id, response_code, message); |
117 | 119 | }); |
118 | 120 | } |
... | ... | @@ -134,16 +136,16 @@ function createServer() { |
134 | 136 | |
135 | 137 | var httpServer = http.createServer(function(req, res) { |
136 | 138 | //console.log('Got request from partner ("' + req.url + '")'); |
137 | - | |
139 | + | |
138 | 140 | res.end('OK'); |
139 | - | |
141 | + | |
140 | 142 | var qs = url.parse(req.url, true).query; |
141 | 143 | logger.info('Reverse Report', {qs: qs}); |
142 | - | |
144 | + | |
143 | 145 | var response_code = '68'; |
144 | 146 | var request_id = qs.pid; |
145 | 147 | var message = qs.msg; |
146 | - | |
148 | + | |
147 | 149 | if (qs.code == 2) { |
148 | 150 | // gagal |
149 | 151 | response_code = '40'; |
... | ... | @@ -154,30 +156,43 @@ function createServer() { |
154 | 156 | response_code = '00'; |
155 | 157 | message = 'SN=' + qs.sn + ';' + message; |
156 | 158 | } |
157 | - | |
159 | + | |
158 | 160 | if (response_code == '40') { |
159 | 161 | var new_response_code = responseCodeFromMessage(message); |
160 | 162 | if (new_response_code) { |
161 | 163 | response_code = new_response_code; |
162 | 164 | } |
163 | 165 | } |
164 | - | |
166 | + | |
165 | 167 | callbackReport(request_id, response_code, message); |
166 | 168 | }); |
167 | - | |
169 | + | |
168 | 170 | httpServer.listen(config.h2h_out.listen_port, function() { |
169 | 171 | logger.info('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); |
170 | 172 | }); |
171 | 173 | } |
172 | 174 | |
173 | -function start(_config, _callbackReport, options) { | |
174 | - config = _config; | |
175 | - callbackReport = _callbackReport | |
176 | - | |
177 | - if (options && options.aaa) { | |
178 | - aaa = options.aaa; | |
175 | +function start(options) { | |
176 | + if (!options) { | |
177 | + console.log('Undefined options, terminating....'); | |
178 | + process.exit(1); | |
179 | 179 | } |
180 | - | |
180 | + | |
181 | + if (options.config) { | |
182 | + config = options.config; | |
183 | + } else { | |
184 | + console.log('Undefined options.config, terminating....') | |
185 | + process.exit(1); | |
186 | + } | |
187 | + | |
188 | + if (options.aaa) { | |
189 | + aaa = options.aaa; | |
190 | + callbackReport = options.aaa.callbackReportWithPushToMongoDb; | |
191 | + } else { | |
192 | + console.log('Undefined options.aaa, terminating....') | |
193 | + process.exit(1); | |
194 | + } | |
195 | + | |
181 | 196 | if (options && options.logger) { |
182 | 197 | logger = options.logger; |
183 | 198 | } else { |
... | ... | @@ -187,7 +202,7 @@ function start(_config, _callbackReport, options) { |
187 | 202 | ] |
188 | 203 | }); |
189 | 204 | } |
190 | - | |
205 | + | |
191 | 206 | createServer(); |
192 | 207 | } |
193 | 208 |