Commit d077430dfbd32c4f90f13f67e0e8fc68dc9a9a94
1 parent
0e90ae10a3
Exists in
master
perbaikan mongo
Showing 1 changed file with 16 additions and 7 deletions Inline Diff
partner-cjk.js
1 | var winston = require('winston'); | 1 | var winston = require('winston'); |
2 | var crypto = require('crypto'); | 2 | var crypto = require('crypto'); |
3 | var xml = require('xml'); | 3 | var xml = require('xml'); |
4 | var url = require('url'); | 4 | var url = require('url'); |
5 | var http = require('http'); | 5 | var http = require('http'); |
6 | var xml2js = require('xml2js').parseString; | 6 | var xml2js = require('xml2js').parseString; |
7 | var mongoClient = require('mongodb').MongoClient; | 7 | var mongoClient = require('mongodb').MongoClient; |
8 | var strftime = require('strftime'); | 8 | var strftime = require('strftime'); |
9 | var moment = require('moment'); | 9 | var moment = require('moment'); |
10 | 10 | ||
11 | var config; | 11 | var config; |
12 | var callbackReport; | 12 | var callbackReport; |
13 | var aaa; | 13 | var aaa; |
14 | var logger; | 14 | var logger; |
15 | var options; | 15 | var options; |
16 | var mongodb; | 16 | var mongodb; |
17 | 17 | ||
18 | function initMongoClient() { | 18 | function initMongoClient() { |
19 | if (!config.mongodb || !config.mongodb.url) { | 19 | if (!config.mongodb || !config.mongodb.url) { |
20 | return; | 20 | return; |
21 | } | 21 | } |
22 | 22 | ||
23 | try { | 23 | try { |
24 | var url = config.mongodb.url; | 24 | var url = config.mongodb.url; |
25 | 25 | ||
26 | mongoClient.connect(url, function(err, db) { | 26 | mongoClient.connect(url, function(err, db) { |
27 | if (err) { | 27 | if (err) { |
28 | logger.warn('Failed to connect to mongodb', {err: err}); | 28 | logger.warn('Failed to connect to mongodb', {err: err}); |
29 | return; | 29 | return; |
30 | } | 30 | } |
31 | mongodb = db; | 31 | mongodb = db; |
32 | logger.info('MongoDB connected'); | 32 | logger.info('MongoDB connected'); |
33 | }); | 33 | }); |
34 | } | 34 | } |
35 | catch(err) { | 35 | catch(err) { |
36 | logger.warn('Exception when connecting to mongodb', {err: err, url: url}); | 36 | logger.warn('Exception when connecting to mongodb', {err: err, url: url}); |
37 | } | 37 | } |
38 | } | 38 | } |
39 | 39 | ||
40 | var maxRetry = 10; | 40 | var maxRetry = 10; |
41 | var sleepBeforeRetry = 30; | 41 | var sleepBeforeRetry = 30; |
42 | 42 | ||
43 | function start(_config, _callbackReport, options) { | 43 | function start(_config, _callbackReport, options) { |
44 | config = _config; | 44 | config = _config; |
45 | callbackReport = _callbackReport | 45 | callbackReport = _callbackReport |
46 | 46 | ||
47 | if (options && options.aaa) { | 47 | if (options && options.aaa) { |
48 | aaa = options.aaa; | 48 | aaa = options.aaa; |
49 | } | 49 | } |
50 | 50 | ||
51 | if (options && options.logger) { | 51 | if (options && options.logger) { |
52 | logger = options.logger; | 52 | logger = options.logger; |
53 | } else { | 53 | } else { |
54 | logger = new winston.Logger({ | 54 | logger = new winston.Logger({ |
55 | transports: [ | 55 | transports: [ |
56 | new (winston.transports.Console)() | 56 | new (winston.transports.Console)() |
57 | ] | 57 | ] |
58 | }); | 58 | }); |
59 | } | 59 | } |
60 | 60 | ||
61 | initMongoClient(); | 61 | initMongoClient(); |
62 | } | 62 | } |
63 | 63 | ||
64 | function calculateSignature(params) { | 64 | function calculateSignature(params) { |
65 | var passwordHash = crypto.createHash('sha1').update(params.password).digest().toString('hex'); | 65 | var passwordHash = crypto.createHash('sha1').update(params.password).digest().toString('hex'); |
66 | var plain = params.trxtype + params.prdcode + params.value + params.msisdn + params.trxid + params.uid + passwordHash; | 66 | var plain = params.trxtype + params.prdcode + params.value + params.msisdn + params.trxid + params.uid + passwordHash; |
67 | var signature = crypto.createHash('sha256').update(plain).digest().toString('hex'); | 67 | var signature = crypto.createHash('sha256').update(plain).digest().toString('hex'); |
68 | 68 | ||
69 | try { | 69 | try { |
70 | logger.verbose('Signature calculated', {plain: plain, signature: signature}); | 70 | logger.verbose('Signature calculated', {plain: plain, signature: signature}); |
71 | } | 71 | } |
72 | catch(err) {} | 72 | catch(err) {} |
73 | 73 | ||
74 | return signature; | 74 | return signature; |
75 | } | 75 | } |
76 | 76 | ||
77 | function createXmlPayload(params) { | 77 | function createXmlPayload(params) { |
78 | var payload = "<?xml version=\"1.0\" ?>\n" + xml({ | 78 | var payload = "<?xml version=\"1.0\" ?>\n" + xml({ |
79 | ciwaru: [ | 79 | ciwaru: [ |
80 | {trxtype: params.trxtype}, | 80 | {trxtype: params.trxtype}, |
81 | {prdcode: params.prdcode}, | 81 | {prdcode: params.prdcode}, |
82 | {value: params.value}, | 82 | {value: params.value}, |
83 | {msisdn: params.msisdn}, | 83 | {msisdn: params.msisdn}, |
84 | {trxid: params.trxid}, | 84 | {trxid: params.trxid}, |
85 | {uid: params.uid}, | 85 | {uid: params.uid}, |
86 | {hash: calculateSignature(params)} | 86 | {hash: calculateSignature(params)} |
87 | ] | 87 | ] |
88 | }); | 88 | }); |
89 | 89 | ||
90 | try { logger.verbose("Payload: " + payload); } | 90 | try { logger.verbose("Payload: " + payload); } |
91 | catch(errLog) {} | 91 | catch(errLog) {} |
92 | 92 | ||
93 | return payload; | 93 | return payload; |
94 | } | 94 | } |
95 | 95 | ||
96 | function insertTaskToMongoDb(task) { | 96 | function insertTaskToMongoDb(task) { |
97 | if (!isMongoReady()) { return; } | 97 | if (!isMongoReady()) { return; } |
98 | 98 | ||
99 | try { | 99 | try { |
100 | task.suppliers = []; | 100 | task.suppliers = []; |
101 | mongodb.collection(config.mongodb.collection).insertOne(task, function(err, result) { | 101 | mongodb.collection(config.mongodb.collection).insertOne(task, function(err, result) { |
102 | mongodb.collection(config.mongodb.collection).updateOne( | 102 | mongodb.collection(config.mongodb.collection).updateOne( |
103 | {requestId: task.requestId}, | 103 | {requestId: task.requestId}, |
104 | { | 104 | { |
105 | $push: {suppliers: config.globals.gateway_name} | 105 | $push: {suppliers: config.globals.gateway_name} |
106 | } | 106 | } |
107 | 107 | ||
108 | ) | 108 | ) |
109 | }); | 109 | }); |
110 | } | 110 | } |
111 | catch(err) { | 111 | catch(err) { |
112 | //logger.warn('Exception when inserting document to mongodb', {err: err, task: task}); | 112 | //logger.warn('Exception when inserting document to mongodb', {err: err, task: task}); |
113 | } | 113 | } |
114 | } | 114 | } |
115 | 115 | ||
116 | function pushResponseToMongoDb(task, response) { | 116 | function pushResponseToMongoDb(task, response) { |
117 | if (!isMongoReady()) { return; } | 117 | if (!isMongoReady()) { return; } |
118 | 118 | ||
119 | try { | 119 | try { |
120 | var lastResponse = {}; | ||
121 | |||
122 | if (response.parsed && response.parsed.ciwaru) { | ||
123 | lastResponse = response.parsed.ciwaru; | ||
124 | } | ||
125 | |||
126 | mongodb.collection(config.mongodb.collection).updateOne( | 120 | mongodb.collection(config.mongodb.collection).updateOne( |
127 | {requestId: task.requestId}, | 121 | {requestId: task.requestId}, |
128 | { | 122 | { |
129 | lastResponse: lastResponse, | ||
130 | $push: {responses: response} | 123 | $push: {responses: response} |
131 | }, | 124 | }, |
132 | function(err, result) { | 125 | function(err, result) { |
133 | if (err) { | 126 | if (err) { |
134 | logger.warn('Error when pushing response to mongodb', {err: err, task: task, response: response}); | 127 | logger.warn('Error when pushing response to mongodb', {err: err, task: task, response: response}); |
135 | return; | 128 | return; |
136 | } | 129 | } |
137 | } | 130 | } |
138 | ); | 131 | ); |
132 | |||
133 | if (response.parsed && response.parsed.ciwaru) { | ||
134 | mongodb.collection(config.mongodb.collection).updateOne( | ||
135 | {requestId: task.requestId}, | ||
136 | { | ||
137 | lastResponse: response.parsed.ciwaru | ||
138 | }, | ||
139 | function(err, result) { | ||
140 | if (err) { | ||
141 | logger.warn('Error when updating last response to mongodb', {err: err, task: task, response: response}); | ||
142 | return; | ||
143 | } | ||
144 | } | ||
145 | ); | ||
146 | } | ||
147 | |||
139 | } | 148 | } |
140 | catch(err) { | 149 | catch(err) { |
141 | logger.warn('Exception when pushing response to mongodb', {err: err, task: task, response: response}); | 150 | logger.warn('Exception when pushing response to mongodb', {err: err, task: task, response: response}); |
142 | } | 151 | } |
143 | } | 152 | } |
144 | 153 | ||
145 | function isMongoReady() { | 154 | function isMongoReady() { |
146 | if (!config.mongodb) { return; } | 155 | if (!config.mongodb) { return; } |
147 | if (!config.mongodb.collection) { return; } | 156 | if (!config.mongodb.collection) { return; } |
148 | if (!mongodb) { return; } | 157 | if (!mongodb) { return; } |
149 | 158 | ||
150 | return true; | 159 | return true; |
151 | } | 160 | } |
152 | 161 | ||
153 | function getSNFromMessage(message) { | 162 | function getSNFromMessage(message) { |
154 | try { | 163 | try { |
155 | var sn_match = message.match(/SN: (\w+)/); | 164 | var sn_match = message.match(/SN: (\w+)/); |
156 | return sn_match[1].trim(); | 165 | return sn_match[1].trim(); |
157 | } | 166 | } |
158 | catch(err) { | 167 | catch(err) { |
159 | if (logger) { | 168 | if (logger) { |
160 | logger.verbose('Exception on getting sn from message', {err: err}); | 169 | logger.verbose('Exception on getting sn from message', {err: err}); |
161 | } | 170 | } |
162 | return ''; | 171 | return ''; |
163 | } | 172 | } |
164 | } | 173 | } |
165 | 174 | ||
166 | function hasSuccessKeywords(message) { | 175 | function hasSuccessKeywords(message) { |
167 | var keywords = ['SUKSES', 'Finish']; | 176 | var keywords = ['SUKSES', 'Finish']; |
168 | 177 | ||
169 | var count = keywords.length; | 178 | var count = keywords.length; |
170 | for (var i=0; i < count; i++) { | 179 | for (var i=0; i < count; i++) { |
171 | if (message.indexOf(keywords[i]) >= 0) { | 180 | if (message.indexOf(keywords[i]) >= 0) { |
172 | return true; | 181 | return true; |
173 | } | 182 | } |
174 | } | 183 | } |
175 | return false; | 184 | return false; |
176 | } | 185 | } |
177 | 186 | ||
178 | function supplierRcToST24Rc(rc) { | 187 | function supplierRcToST24Rc(rc) { |
179 | var rcs = { | 188 | var rcs = { |
180 | '0001': '40', | 189 | '0001': '40', |
181 | '0012': '40', // hash data tidak sesuai | 190 | '0012': '40', // hash data tidak sesuai |
182 | '0019': '13', // produk tidak tersedia | 191 | '0019': '13', // produk tidak tersedia |
183 | } | 192 | } |
184 | 193 | ||
185 | if (rcs[rc]) { | 194 | if (rcs[rc]) { |
186 | return rcs[rc]; | 195 | return rcs[rc]; |
187 | } else { | 196 | } else { |
188 | return; | 197 | return; |
189 | } | 198 | } |
190 | } | 199 | } |
191 | 200 | ||
192 | function getSNFromResponseObject(respObj) { | 201 | function getSNFromResponseObject(respObj) { |
193 | try { | 202 | try { |
194 | return respObj.ciwaru.sn[0].trim(); | 203 | return respObj.ciwaru.sn[0].trim(); |
195 | } | 204 | } |
196 | catch(err) { | 205 | catch(err) { |
197 | return; | 206 | return; |
198 | } | 207 | } |
199 | } | 208 | } |
200 | 209 | ||
201 | function topupResponseHandler(body, task) { | 210 | function topupResponseHandler(body, task) { |
202 | 211 | ||
203 | //logger.info('Got reply from partner', {body: body}); | 212 | //logger.info('Got reply from partner', {body: body}); |
204 | 213 | ||
205 | xml2js(body, function(err, result) { | 214 | xml2js(body, function(err, result) { |
206 | var ts = strftime('%Y-%m-%d %H:%M:%S', new Date()); | 215 | var ts = strftime('%Y-%m-%d %H:%M:%S', new Date()); |
207 | 216 | ||
208 | if (err) { | 217 | if (err) { |
209 | logger.warn('Got invalid XML from partner', {err: err, body: body, task: task}); | 218 | logger.warn('Got invalid XML from partner', {err: err, body: body, task: task}); |
210 | callbackReport(task.requestId, '68', body); | 219 | callbackReport(task.requestId, '68', body); |
211 | 220 | ||
212 | pushResponseToMongoDb(task, {ts: ts, raw: body}); | 221 | pushResponseToMongoDb(task, {ts: ts, raw: body}); |
213 | return; | 222 | return; |
214 | } | 223 | } |
215 | logger.info('XML message from partner', {result: result}); | 224 | logger.info('XML message from partner', {result: result}); |
216 | pushResponseToMongoDb(task, {ts: ts, raw: body, parsed: result}); | 225 | pushResponseToMongoDb(task, {ts: ts, raw: body, parsed: result}); |
217 | 226 | ||
218 | var rc = '68'; | 227 | var rc = '68'; |
219 | var message = result.ciwaru.msg[0]; | 228 | var message = result.ciwaru.msg[0]; |
220 | 229 | ||
221 | if (message.toUpperCase().indexOf('PENDING') >= 0) { | 230 | if (message.toUpperCase().indexOf('PENDING') >= 0) { |
222 | rc = '68'; | 231 | rc = '68'; |
223 | } | 232 | } |
224 | else if (hasSuccessKeywords(message)) { | 233 | else if (hasSuccessKeywords(message)) { |
225 | 234 | ||
226 | var sn = getSNFromResponseObject(result); | 235 | var sn = getSNFromResponseObject(result); |
227 | 236 | ||
228 | if (!sn) { | 237 | if (!sn) { |
229 | sn = getSNFromMessage(message); | 238 | sn = getSNFromMessage(message); |
230 | } | 239 | } |
231 | 240 | ||
232 | message = 'SN=' + sn + '; ' + message; | 241 | message = 'SN=' + sn + '; ' + message; |
233 | rc = '00'; | 242 | rc = '00'; |
234 | 243 | ||
235 | } | 244 | } |
236 | else if (message.indexOf('Nomor Pelanggan Salah') >= 0) { | 245 | else if (message.indexOf('Nomor Pelanggan Salah') >= 0) { |
237 | 246 | ||
238 | rc = '14'; | 247 | rc = '14'; |
239 | 248 | ||
240 | } else { | 249 | } else { |
241 | rc = supplierRcToST24Rc(result.ciwaru.rc[0]); | 250 | rc = supplierRcToST24Rc(result.ciwaru.rc[0]); |
242 | if (!rc) { | 251 | if (!rc) { |
243 | rc = '68'; | 252 | rc = '68'; |
244 | } | 253 | } |
245 | } | 254 | } |
246 | 255 | ||
247 | if ((task.retry == maxRetry) || (rc != '68')) { | 256 | if ((task.retry == maxRetry) || (rc != '68')) { |
248 | callbackReport(task.requestId, rc, message); | 257 | callbackReport(task.requestId, rc, message); |
249 | } else { | 258 | } else { |
250 | logger.info('Not reporting to AAA for duplicate 68', {task: task}); | 259 | logger.info('Not reporting to AAA for duplicate 68', {task: task}); |
251 | } | 260 | } |
252 | 261 | ||
253 | if (rc == '68') { | 262 | if (rc == '68') { |
254 | topupRequestRetry(task); | 263 | topupRequestRetry(task); |
255 | } | 264 | } |
256 | }); | 265 | }); |
257 | } | 266 | } |
258 | 267 | ||
259 | function topupRequestRetry(task) { | 268 | function topupRequestRetry(task) { |
260 | task.retry--; | 269 | task.retry--; |
261 | 270 | ||
262 | if (task.retry > 0) { | 271 | if (task.retry > 0) { |
263 | logger.info('Retrying in ' + sleepBeforeRetry + 's'); | 272 | logger.info('Retrying in ' + sleepBeforeRetry + 's'); |
264 | setTimeout(topupRequest, sleepBeforeRetry * 1000, task, task.retry); | 273 | setTimeout(topupRequest, sleepBeforeRetry * 1000, task, task.retry); |
265 | } | 274 | } |
266 | else { | 275 | else { |
267 | logger.warn('Maximum retry for pending status exceeded', {task: task}); | 276 | logger.warn('Maximum retry for pending status exceeded', {task: task}); |
268 | } | 277 | } |
269 | } | 278 | } |
270 | 279 | ||
271 | function topupRequest(task, retry) { | 280 | function topupRequest(task, retry) { |
272 | 281 | ||
273 | if (retry === undefined) { | 282 | if (retry === undefined) { |
274 | 283 | ||
275 | task.ts = moment(task.timestamp, 'YYYYMMDDHHmmss').format('YYYY-MM-DD HH:mm:ss'); | 284 | task.ts = moment(task.timestamp, 'YYYYMMDDHHmmss').format('YYYY-MM-DD HH:mm:ss'); |
276 | task.ts_date = moment(task.timestamp, 'YYYYMMDDHHmmss').format('YYYY-MM-DD'); | 285 | task.ts_date = moment(task.timestamp, 'YYYYMMDDHHmmss').format('YYYY-MM-DD'); |
277 | 286 | ||
278 | insertTaskToMongoDb(task); | 287 | insertTaskToMongoDb(task); |
279 | 288 | ||
280 | retry = maxRetry; | 289 | retry = maxRetry; |
281 | } | 290 | } |
282 | 291 | ||
283 | if (!task.retry) { | 292 | if (!task.retry) { |
284 | task.retry = retry; | 293 | task.retry = retry; |
285 | } | 294 | } |
286 | 295 | ||
287 | var remoteProduct = task.remoteProduct.split(','); | 296 | var remoteProduct = task.remoteProduct.split(','); |
288 | 297 | ||
289 | var params = { | 298 | var params = { |
290 | trxtype: '01', | 299 | trxtype: '01', |
291 | prdcode: remoteProduct[0], | 300 | prdcode: remoteProduct[0], |
292 | value: remoteProduct[1], | 301 | value: remoteProduct[1], |
293 | msisdn: task.destination, | 302 | msisdn: task.destination, |
294 | trxid: task.requestId, | 303 | trxid: task.requestId, |
295 | uid: config.h2h_out.userid, | 304 | uid: config.h2h_out.userid, |
296 | password: config.h2h_out.password, | 305 | password: config.h2h_out.password, |
297 | }; | 306 | }; |
298 | 307 | ||
299 | var postBody = createXmlPayload(params); | 308 | var postBody = createXmlPayload(params); |
300 | 309 | ||
301 | var partnerUrl = url.parse(config.h2h_out.partner); | 310 | var partnerUrl = url.parse(config.h2h_out.partner); |
302 | var postRequest = { | 311 | var postRequest = { |
303 | host: partnerUrl.hostname, | 312 | host: partnerUrl.hostname, |
304 | path: partnerUrl.path, | 313 | path: partnerUrl.path, |
305 | port: partnerUrl.port, | 314 | port: partnerUrl.port, |
306 | method: "POST", | 315 | method: "POST", |
307 | headers: { | 316 | headers: { |
308 | 'Content-Type': 'text/xml', | 317 | 'Content-Type': 'text/xml', |
309 | 'Content-Length': Buffer.byteLength(postBody) | 318 | 'Content-Length': Buffer.byteLength(postBody) |
310 | } | 319 | } |
311 | }; | 320 | }; |
312 | 321 | ||
313 | logger.info('POST to partner', {postRequest: postRequest}); | 322 | logger.info('POST to partner', {postRequest: postRequest}); |
314 | var req = http.request(postRequest, function( res ) { | 323 | var req = http.request(postRequest, function( res ) { |
315 | 324 | ||
316 | logger.verbose('Status code: ' + res.statusCode ); | 325 | logger.verbose('Status code: ' + res.statusCode ); |
317 | var buffer = ""; | 326 | var buffer = ""; |
318 | 327 | ||
319 | res.on( "data", function( data ) { | 328 | res.on( "data", function( data ) { |
320 | buffer = buffer + data; | 329 | buffer = buffer + data; |
321 | }); | 330 | }); |
322 | 331 | ||
323 | res.on( "end", function( data ) { | 332 | res.on( "end", function( data ) { |
324 | topupResponseHandler(buffer, task); | 333 | topupResponseHandler(buffer, task); |
325 | }); | 334 | }); |
326 | 335 | ||
327 | }); | 336 | }); |
328 | 337 | ||
329 | req.on('error', function(e) { | 338 | req.on('error', function(e) { |
330 | logger.warn('problem with request: ' + e.message); | 339 | logger.warn('problem with request: ' + e.message); |
331 | callbackReport(task['requestId'], '68', e.message); | 340 | callbackReport(task['requestId'], '68', e.message); |
332 | 341 | ||
333 | topupRequestRetry(task); | 342 | topupRequestRetry(task); |
334 | }); | 343 | }); |
335 | 344 | ||
336 | req.write(postBody); | 345 | req.write(postBody); |
337 | req.end(); | 346 | req.end(); |
338 | } | 347 | } |