Commit 883fdb10286513a45ad41b000f234268b254c446
1 parent
9ab7768236
Exists in
master
parsing message utk respon error
Showing 1 changed file with 22 additions and 8 deletions Inline Diff
partner-trugee.js
1 | var http = require('http'); | 1 | var http = require('http'); |
2 | var url = require('url'); | 2 | var url = require('url'); |
3 | var math = require('mathjs'); | 3 | var math = require('mathjs'); |
4 | var xml = require('xml'); | 4 | var xml = require('xml'); |
5 | var xml2js = require('xml2js').parseString; | 5 | var xml2js = require('xml2js').parseString; |
6 | var strftime = require('strftime'); | 6 | var strftime = require('strftime'); |
7 | var xor = require('base64-xor'); | 7 | var xor = require('base64-xor'); |
8 | var request = require('request'); | 8 | var request = require('request'); |
9 | 9 | ||
10 | var config; | 10 | var config; |
11 | var callbackReport; | 11 | var callbackReport; |
12 | 12 | ||
13 | var max_retry = 2; | 13 | var max_retry = 2; |
14 | var sleep_before_retry = 2000; | 14 | var sleep_before_retry = 2000; |
15 | 15 | ||
16 | var trx_balances = {}; | 16 | var trx_balances = {}; |
17 | var trx_prices = {}; | 17 | var trx_prices = {}; |
18 | 18 | ||
19 | function calculateSignature(password, msisdn, timestamp) { | 19 | function calculateSignature(password, msisdn, timestamp) { |
20 | var a = timestamp + msisdn.substr(msisdn.length - 4); | 20 | var a = timestamp + msisdn.substr(msisdn.length - 4); |
21 | var b = msisdn.substr(msisdn.length - 4).split('').reverse().join('') + password; | 21 | var b = msisdn.substr(msisdn.length - 4).split('').reverse().join('') + password; |
22 | 22 | ||
23 | return xor.encode(a,b); | 23 | return xor.encode(a,b); |
24 | } | 24 | } |
25 | 25 | ||
26 | function createPayload(task) { | 26 | function createPayload(task) { |
27 | var timestamp = strftime('%H%M%S'); | 27 | var timestamp = strftime('%H%M%S'); |
28 | 28 | ||
29 | var payload = { | 29 | var payload = { |
30 | pulsamatic: [ | 30 | pulsamatic: [ |
31 | {command: 'TOPUP'}, | 31 | {command: 'TOPUP'}, |
32 | {vtype: task['remoteProduct']}, | 32 | {vtype: task['remoteProduct']}, |
33 | {userid: config.h2h_out.userid}, | 33 | {userid: config.h2h_out.userid}, |
34 | {time: timestamp}, | 34 | {time: timestamp}, |
35 | {msisdn: task['destination']}, | 35 | {msisdn: task['destination']}, |
36 | {trxid: task['requestId']}, | 36 | {trxid: task['requestId']}, |
37 | {sign: calculateSignature(config.h2h_out.password, task['destination'], timestamp)} | 37 | {sign: calculateSignature(config.h2h_out.password, task['destination'], timestamp)} |
38 | ] | 38 | ] |
39 | }; | 39 | }; |
40 | 40 | ||
41 | console.log(payload); | 41 | console.log(payload); |
42 | return "<?xml version=\"1.0\" ?>\n" + xml(payload); | 42 | return "<?xml version=\"1.0\" ?>\n" + xml(payload); |
43 | } | 43 | } |
44 | 44 | ||
45 | function topupRequest(task, retry) { | 45 | function topupRequest(task, retry) { |
46 | 46 | ||
47 | console.log('Requesting to partner:'); | 47 | console.log('Requesting to partner:'); |
48 | var payload_xml = createPayload(task); | 48 | var payload_xml = createPayload(task); |
49 | var partner = url.parse(config.h2h_out.partner); | 49 | var partner = url.parse(config.h2h_out.partner); |
50 | 50 | ||
51 | var request_options = { | 51 | var request_options = { |
52 | host: partner.hostname, | 52 | host: partner.hostname, |
53 | path: partner.path, | 53 | path: partner.path, |
54 | port: partner.port, | 54 | port: partner.port, |
55 | method: "POST", | 55 | method: "POST", |
56 | headers: { | 56 | headers: { |
57 | 'Content-Type': 'text/xml', | 57 | 'Content-Type': 'text/xml', |
58 | 'Content-Length': Buffer.byteLength(payload_xml) | 58 | 'Content-Length': Buffer.byteLength(payload_xml) |
59 | } | 59 | } |
60 | }; | 60 | }; |
61 | 61 | ||
62 | var buffer = ""; | 62 | var buffer = ""; |
63 | var req = http.request(request_options, function( res ) { | 63 | var req = http.request(request_options, function( res ) { |
64 | 64 | ||
65 | console.log('Status code: ' + res.statusCode ); | 65 | console.log('Status code: ' + res.statusCode ); |
66 | var buffer = ""; | 66 | var buffer = ""; |
67 | res.on( "data", function( data ) { buffer = buffer + data; } ); | 67 | res.on( "data", function( data ) { buffer = buffer + data; } ); |
68 | res.on( "end", function( data ) { | 68 | res.on( "end", function( data ) { |
69 | directResponseHandler(buffer, task['requestId']); | 69 | directResponseHandler(buffer, task['requestId']); |
70 | }); | 70 | }); |
71 | 71 | ||
72 | }); | 72 | }); |
73 | 73 | ||
74 | req.on('error', function(e) { | 74 | req.on('error', function(e) { |
75 | console.log('problem with request: ' + e.message); | 75 | console.log('problem with request: ' + e.message); |
76 | callbackReport(task['requestId'], '40', e.message); | 76 | callbackReport(task['requestId'], '40', e.message); |
77 | }); | 77 | }); |
78 | 78 | ||
79 | req.write( payload_xml ); | 79 | req.write( payload_xml ); |
80 | req.end(); | 80 | req.end(); |
81 | } | 81 | } |
82 | 82 | ||
83 | function directResponseHandler(body, request_id) { | 83 | function directResponseHandler(body, request_id) { |
84 | console.log('Direct Response:'); | 84 | console.log('Direct Response:'); |
85 | xml2js(body, function (err, result) { | 85 | xml2js(body, function (err, result) { |
86 | if (err) { | 86 | if (err) { |
87 | console.log(body); | 87 | console.log(body); |
88 | callbackReport(request_id, '40', buffer); | 88 | callbackReport(request_id, '40', buffer); |
89 | return; | 89 | return; |
90 | } | 90 | } |
91 | console.log(result); | 91 | console.log(result); |
92 | 92 | ||
93 | var response_code = '68'; | 93 | var response_code = '68'; |
94 | 94 | ||
95 | var request_id = result.pulsamatic.partner_trxid[0].trim(); | 95 | var request_id = result.pulsamatic.partner_trxid[0].trim(); |
96 | var message = result.pulsamatic.message[0].trim(); | 96 | var message = result.pulsamatic.message[0].trim(); |
97 | var status = result.pulsamatic.result[0].trim(); | 97 | var status = result.pulsamatic.result[0].trim(); |
98 | 98 | ||
99 | if (status === 'failed') { | 99 | if (status === 'failed') { |
100 | response_code = '40'; | 100 | response_code = '40'; |
101 | 101 | ||
102 | if (message.indexOf('Jenis produk tidak cocok') >= 0) { | 102 | var new_response_code = responseCodeFromMessage(message); |
103 | response_code = '14'; | 103 | if (new_response_code) { |
104 | } else if (message.indexOf('GAGAL. Nomor telp salah.') >= 0) { | 104 | response_code = new_response_code; |
105 | response_code = '14'; | ||
106 | } else if (message.indexOf('GAGAL. MSISDN tidak ditemukan') >= 0) { | ||
107 | response_code = '14'; | ||
108 | } | 105 | } |
109 | 106 | ||
110 | } | 107 | } |
111 | 108 | ||
112 | callbackReport(request_id, response_code, message); | 109 | callbackReport(request_id, response_code, message); |
113 | }); | 110 | }); |
114 | 111 | } | |
112 | |||
113 | function responseCodeFromMessage(message) { | ||
114 | if (message.indexOf('Jenis produk tidak cocok') >= 0) { | ||
115 | return '14'; | ||
116 | } else if (message.indexOf('GAGAL. Nomor telp salah.') >= 0) { | ||
117 | return '14'; | ||
118 | } else if (message.indexOf('GAGAL. MSISDN tidak ditemukan') >= 0) { | ||
119 | return '14'; | ||
120 | } | ||
121 | return; | ||
115 | } | 122 | } |
116 | 123 | ||
117 | function createServer() { | 124 | function createServer() { |
118 | 125 | ||
119 | var httpServer = http.createServer(function(req, res) { | 126 | var httpServer = http.createServer(function(req, res) { |
120 | //console.log('Got request from partner ("' + req.url + '")'); | 127 | //console.log('Got request from partner ("' + req.url + '")'); |
121 | 128 | ||
122 | res.end('OK'); | 129 | res.end('OK'); |
123 | 130 | ||
124 | console.log('Reverse Report:'); | 131 | console.log('Reverse Report:'); |
125 | var qs = url.parse(req.url, true).query; | 132 | var qs = url.parse(req.url, true).query; |
126 | console.log(qs); | 133 | console.log(qs); |
127 | 134 | ||
128 | var response_code = '68'; | 135 | var response_code = '68'; |
129 | var request_id = qs.pid; | 136 | var request_id = qs.pid; |
130 | var message = qs.msg; | 137 | var message = qs.msg; |
131 | 138 | ||
132 | if (qs.code == 2) { | 139 | if (qs.code == 2) { |
133 | // gagal | 140 | // gagal |
134 | response_code = '40'; | 141 | response_code = '40'; |
135 | } else if (qs.code == 3) { | 142 | } else if (qs.code == 3) { |
136 | // refund | 143 | // refund |
137 | response_code = '40'; | 144 | response_code = '40'; |
138 | } else if (qs.code == 4) { | 145 | } else if (qs.code == 4) { |
139 | response_code = '00'; | 146 | response_code = '00'; |
140 | message = 'SN=' + qs.sn + ';' + message; | 147 | message = 'SN=' + qs.sn + ';' + message; |
141 | } | 148 | } |
149 | |||
150 | if (respose_code == '40') { | ||
151 | var new_response_code = responseCodeFromMessage(message); | ||
152 | if (new_response_code) { | ||
153 | response_code = new_response_code; | ||
154 | } | ||
155 | } | ||
142 | 156 | ||
143 | callbackReport(request_id, response_code, message); | 157 | callbackReport(request_id, response_code, message); |
144 | }); | 158 | }); |
145 | 159 | ||
146 | httpServer.listen(config.h2h_out.listen_port, function() { | 160 | httpServer.listen(config.h2h_out.listen_port, function() { |
147 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); | 161 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); |
148 | }); | 162 | }); |
149 | } | 163 | } |
150 | 164 | ||
151 | function start(_config, _callbackReport) { | 165 | function start(_config, _callbackReport) { |
152 | config = _config; | 166 | config = _config; |
153 | callbackReport = _callbackReport | 167 | callbackReport = _callbackReport |
154 | 168 | ||
155 | createServer(); | 169 | createServer(); |
156 | } | 170 | } |
157 | 171 | ||
158 | exports.start = start; | 172 | exports.start = start; |