Commit d004bf81a1fdcfe61e786344291313f76e43a49f
1 parent
a07de01048
Exists in
master
response code utk duplicate
Showing 1 changed file with 2 additions and 0 deletions Inline Diff
partner-datacell.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 | function calculateSignature(userid, password, msisdn, timestamp) { | 16 | function calculateSignature(userid, password, msisdn, timestamp) { |
17 | var a = msisdn.substr(msisdn.length - 4) + timestamp; | 17 | var a = msisdn.substr(msisdn.length - 4) + timestamp; |
18 | console.log(a); | 18 | console.log(a); |
19 | var b = userid.substr(0, 4) + password; | 19 | var b = userid.substr(0, 4) + password; |
20 | console.log(b); | 20 | console.log(b); |
21 | 21 | ||
22 | return xor.encode(a,b); | 22 | return xor.encode(a,b); |
23 | } | 23 | } |
24 | 24 | ||
25 | function createPayload(task) { | 25 | function createPayload(task) { |
26 | var timestamp = strftime('%H%M%S'); | 26 | var timestamp = strftime('%H%M%S'); |
27 | 27 | ||
28 | var payload = { | 28 | var payload = { |
29 | datacell: [ | 29 | datacell: [ |
30 | { perintah: 'charge'}, | 30 | { perintah: 'charge'}, |
31 | {oprcode: task['remoteProduct']}, | 31 | {oprcode: task['remoteProduct']}, |
32 | {userid: config.h2h_out.userid}, | 32 | {userid: config.h2h_out.userid}, |
33 | {time: timestamp}, | 33 | {time: timestamp}, |
34 | {msisdn: task['destination']}, | 34 | {msisdn: task['destination']}, |
35 | {ref_trxid: task['requestId']}, | 35 | {ref_trxid: task['requestId']}, |
36 | {sgn: calculateSignature(config.h2h_out.userid, config.h2h_out.password, task['destination'], timestamp)} | 36 | {sgn: calculateSignature(config.h2h_out.userid, config.h2h_out.password, task['destination'], timestamp)} |
37 | ] | 37 | ] |
38 | }; | 38 | }; |
39 | 39 | ||
40 | console.log(payload); | 40 | console.log(payload); |
41 | return "<?xml version=\"1.0\" ?>\n" + xml(payload); | 41 | return "<?xml version=\"1.0\" ?>\n" + xml(payload); |
42 | } | 42 | } |
43 | 43 | ||
44 | function topupRequest(task, retry) { | 44 | function topupRequest(task, retry) { |
45 | if (config.globals.requests_count == undefined) { | 45 | if (config.globals.requests_count == undefined) { |
46 | config.globals.requests_count = 1; | 46 | config.globals.requests_count = 1; |
47 | } else { | 47 | } else { |
48 | config.globals.requests_count++; | 48 | config.globals.requests_count++; |
49 | } | 49 | } |
50 | 50 | ||
51 | if (config.globals.active_requests_count == undefined) { | 51 | if (config.globals.active_requests_count == undefined) { |
52 | config.globals.active_requests_count = 1; | 52 | config.globals.active_requests_count = 1; |
53 | } else { | 53 | } else { |
54 | config.globals.active_requests_count++; | 54 | config.globals.active_requests_count++; |
55 | } | 55 | } |
56 | 56 | ||
57 | if (config.globals.max_active_requests_count == undefined) { | 57 | if (config.globals.max_active_requests_count == undefined) { |
58 | config.globals.max_active_requests_count = config.globals.active_requests_count; | 58 | config.globals.max_active_requests_count = config.globals.active_requests_count; |
59 | } else { | 59 | } else { |
60 | config.globals.max_active_requests_count = math.max(config.globals.max_active_requests_count, config.globals.active_requests_count); | 60 | config.globals.max_active_requests_count = math.max(config.globals.max_active_requests_count, config.globals.active_requests_count); |
61 | } | 61 | } |
62 | 62 | ||
63 | 63 | ||
64 | if (retry === undefined) { | 64 | if (retry === undefined) { |
65 | retry = max_retry; | 65 | retry = max_retry; |
66 | } | 66 | } |
67 | 67 | ||
68 | var payload_xml = createPayload(task); | 68 | var payload_xml = createPayload(task); |
69 | console.log(payload_xml); | 69 | console.log(payload_xml); |
70 | 70 | ||
71 | var postRequest = { | 71 | var postRequest = { |
72 | host: "202.152.62.2", | 72 | host: "202.152.62.2", |
73 | path: "/RELOAD97.php", | 73 | path: "/RELOAD97.php", |
74 | port: 7713, | 74 | port: 7713, |
75 | method: "POST", | 75 | method: "POST", |
76 | headers: { | 76 | headers: { |
77 | 'Content-Type': 'text/xml', | 77 | 'Content-Type': 'text/xml', |
78 | 'Content-Length': Buffer.byteLength(payload_xml) | 78 | 'Content-Length': Buffer.byteLength(payload_xml) |
79 | } | 79 | } |
80 | }; | 80 | }; |
81 | 81 | ||
82 | var buffer = ""; | 82 | var buffer = ""; |
83 | var req = http.request( postRequest, function( res ) { | 83 | var req = http.request( postRequest, function( res ) { |
84 | 84 | ||
85 | console.log('Status code: ' + res.statusCode ); | 85 | console.log('Status code: ' + res.statusCode ); |
86 | var buffer = ""; | 86 | var buffer = ""; |
87 | res.on( "data", function( data ) { buffer = buffer + data; } ); | 87 | res.on( "data", function( data ) { buffer = buffer + data; } ); |
88 | res.on( "end", function( data ) { | 88 | res.on( "end", function( data ) { |
89 | topupResponseHandler(buffer); | 89 | topupResponseHandler(buffer); |
90 | }); | 90 | }); |
91 | 91 | ||
92 | }); | 92 | }); |
93 | 93 | ||
94 | req.on('error', function(e) { | 94 | req.on('error', function(e) { |
95 | console.log('problem with request: ' + e.message); | 95 | console.log('problem with request: ' + e.message); |
96 | }); | 96 | }); |
97 | 97 | ||
98 | req.write( payload_xml ); | 98 | req.write( payload_xml ); |
99 | req.end(); | 99 | req.end(); |
100 | } | 100 | } |
101 | 101 | ||
102 | function topupResponseHandler(body, request_id) { | 102 | function topupResponseHandler(body, request_id) { |
103 | xml2js(body, function (err, result) { | 103 | xml2js(body, function (err, result) { |
104 | if (err) { | 104 | if (err) { |
105 | console.log(body); | 105 | console.log(body); |
106 | callbackReport(request_id, '40', buffer); | 106 | callbackReport(request_id, '40', buffer); |
107 | return; | 107 | return; |
108 | } | 108 | } |
109 | 109 | ||
110 | console.log(result); | 110 | console.log(result); |
111 | 111 | ||
112 | request_id = result.datacell.ref_trxid[0].trim(); | 112 | request_id = result.datacell.ref_trxid[0].trim(); |
113 | 113 | ||
114 | var response_code = '68'; | 114 | var response_code = '68'; |
115 | var message = result.datacell.message[0].trim(); | 115 | var message = result.datacell.message[0].trim(); |
116 | 116 | ||
117 | if (result.datacell.resultcode[0] == '999') { | 117 | if (result.datacell.resultcode[0] == '999') { |
118 | response_code = '40'; | 118 | response_code = '40'; |
119 | 119 | ||
120 | if (message.indexOf('Nomor tujuan salah') >= 0) { | 120 | if (message.indexOf('Nomor tujuan salah') >= 0) { |
121 | response_code = '14'; | 121 | response_code = '14'; |
122 | } else if (message.indexOf('*GAGAL, transaksi yang sama sudah ada dalam 10 menit') >= 0) { | ||
123 | response_code = '55'; | ||
122 | } | 124 | } |
123 | } | 125 | } |
124 | 126 | ||
125 | callbackReport(request_id, response_code, message); | 127 | callbackReport(request_id, response_code, message); |
126 | }); | 128 | }); |
127 | } | 129 | } |
128 | 130 | ||
129 | function createServer() { | 131 | function createServer() { |
130 | 132 | ||
131 | var httpServer = http.createServer(function(req, res) { | 133 | var httpServer = http.createServer(function(req, res) { |
132 | console.log('Got request from partner ("' + req.url + '")'); | 134 | console.log('Got request from partner ("' + req.url + '")'); |
133 | 135 | ||
134 | var body = ""; | 136 | var body = ""; |
135 | req.on('data', function (chunk) { | 137 | req.on('data', function (chunk) { |
136 | body += chunk; | 138 | body += chunk; |
137 | }); | 139 | }); |
138 | 140 | ||
139 | req.on('end', function () { | 141 | req.on('end', function () { |
140 | res.writeHead(200); | 142 | res.writeHead(200); |
141 | res.end('OK'); | 143 | res.end('OK'); |
142 | 144 | ||
143 | console.log(body); | 145 | console.log(body); |
144 | topupResponseHandler(body); | 146 | topupResponseHandler(body); |
145 | }); | 147 | }); |
146 | }); | 148 | }); |
147 | 149 | ||
148 | httpServer.listen(config.h2h_out.listen_port, function() { | 150 | httpServer.listen(config.h2h_out.listen_port, function() { |
149 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); | 151 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); |
150 | }); | 152 | }); |
151 | } | 153 | } |
152 | 154 | ||
153 | 155 | ||
154 | function start(_config, _callbackReport) { | 156 | function start(_config, _callbackReport) { |
155 | config = _config; | 157 | config = _config; |
156 | callbackReport = _callbackReport | 158 | callbackReport = _callbackReport |
157 | 159 | ||
158 | createServer(); | 160 | createServer(); |
159 | } | 161 | } |
160 | 162 | ||
161 | exports.start = start; | 163 | exports.start = start; |
162 | exports.topupRequest = topupRequest; | 164 | exports.topupRequest = topupRequest; |
163 | 165 |