Commit 981aeec500dc1fc7c0a7a50cf5f96b5a283fef9f
1 parent
01d059c2f6
Exists in
master
more verbose on steps
Showing 1 changed file with 3 additions and 0 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 | var payload_xml = createPayload(task); | 48 | var payload_xml = createPayload(task); |
48 | var partner = url.parse(config.h2h_out.partner); | 49 | var partner = url.parse(config.h2h_out.partner); |
49 | 50 | ||
50 | var request_options = { | 51 | var request_options = { |
51 | host: partner.hostname, | 52 | host: partner.hostname, |
52 | path: partner.path, | 53 | path: partner.path, |
53 | port: partner.port, | 54 | port: partner.port, |
54 | method: "POST", | 55 | method: "POST", |
55 | headers: { | 56 | headers: { |
56 | 'Content-Type': 'text/xml', | 57 | 'Content-Type': 'text/xml', |
57 | 'Content-Length': Buffer.byteLength(payload_xml) | 58 | 'Content-Length': Buffer.byteLength(payload_xml) |
58 | } | 59 | } |
59 | }; | 60 | }; |
60 | 61 | ||
61 | var buffer = ""; | 62 | var buffer = ""; |
62 | var req = http.request(request_options, function( res ) { | 63 | var req = http.request(request_options, function( res ) { |
63 | 64 | ||
64 | console.log('Status code: ' + res.statusCode ); | 65 | console.log('Status code: ' + res.statusCode ); |
65 | var buffer = ""; | 66 | var buffer = ""; |
66 | res.on( "data", function( data ) { buffer = buffer + data; } ); | 67 | res.on( "data", function( data ) { buffer = buffer + data; } ); |
67 | res.on( "end", function( data ) { | 68 | res.on( "end", function( data ) { |
68 | directResponseHandler(buffer, task['requestId']); | 69 | directResponseHandler(buffer, task['requestId']); |
69 | }); | 70 | }); |
70 | 71 | ||
71 | }); | 72 | }); |
72 | 73 | ||
73 | req.on('error', function(e) { | 74 | req.on('error', function(e) { |
74 | console.log('problem with request: ' + e.message); | 75 | console.log('problem with request: ' + e.message); |
75 | callbackReport(task['requestId'], '40', e.message); | 76 | callbackReport(task['requestId'], '40', e.message); |
76 | }); | 77 | }); |
77 | 78 | ||
78 | req.write( payload_xml ); | 79 | req.write( payload_xml ); |
79 | req.end(); | 80 | req.end(); |
80 | } | 81 | } |
81 | 82 | ||
82 | function directResponseHandler(body, request_id) { | 83 | function directResponseHandler(body, request_id) { |
84 | console.log('Direct Response:'); | ||
83 | xml2js(body, function (err, result) { | 85 | xml2js(body, function (err, result) { |
84 | if (err) { | 86 | if (err) { |
85 | console.log(body); | 87 | console.log(body); |
86 | callbackReport(request_id, '40', buffer); | 88 | callbackReport(request_id, '40', buffer); |
87 | return; | 89 | return; |
88 | } | 90 | } |
89 | console.log(result); | 91 | console.log(result); |
90 | 92 | ||
91 | var response_code = '68'; | 93 | var response_code = '68'; |
92 | 94 | ||
93 | var request_id = result.pulsamatic.partner_trxid[0].trim(); | 95 | var request_id = result.pulsamatic.partner_trxid[0].trim(); |
94 | var message = result.pulsamatic.message[0].trim(); | 96 | var message = result.pulsamatic.message[0].trim(); |
95 | 97 | ||
96 | if (result.pulsamatic.result[0].trim() == 'failed') { | 98 | if (result.pulsamatic.result[0].trim() == 'failed') { |
97 | response_code = '40'; | 99 | response_code = '40'; |
98 | } | 100 | } |
99 | 101 | ||
100 | callbackReport(request_id, response_code, message); | 102 | callbackReport(request_id, response_code, message); |
101 | }); | 103 | }); |
102 | 104 | ||
103 | } | 105 | } |
104 | 106 | ||
105 | function createServer() { | 107 | function createServer() { |
106 | 108 | ||
107 | var httpServer = http.createServer(function(req, res) { | 109 | var httpServer = http.createServer(function(req, res) { |
108 | //console.log('Got request from partner ("' + req.url + '")'); | 110 | //console.log('Got request from partner ("' + req.url + '")'); |
109 | 111 | ||
110 | res.end('OK'); | 112 | res.end('OK'); |
111 | 113 | ||
114 | console.log('Reverse Report:'); | ||
112 | var qs = url.parse(req.url, true).query; | 115 | var qs = url.parse(req.url, true).query; |
113 | console.log(qs); | 116 | console.log(qs); |
114 | 117 | ||
115 | var response_code = '68'; | 118 | var response_code = '68'; |
116 | var request_id = qs.pid; | 119 | var request_id = qs.pid; |
117 | var message = qs.msg; | 120 | var message = qs.msg; |
118 | 121 | ||
119 | if (qs.code == 2) { | 122 | if (qs.code == 2) { |
120 | // gagal | 123 | // gagal |
121 | response_code = '40'; | 124 | response_code = '40'; |
122 | } else if (qs.code == 3) { | 125 | } else if (qs.code == 3) { |
123 | // refund | 126 | // refund |
124 | response_code = '40'; | 127 | response_code = '40'; |
125 | } else if (qs.code == 4) { | 128 | } else if (qs.code == 4) { |
126 | response_code = '00'; | 129 | response_code = '00'; |
127 | message = 'SN=' + qs.sn + ';' + message; | 130 | message = 'SN=' + qs.sn + ';' + message; |
128 | } | 131 | } |
129 | 132 | ||
130 | callbackReport(request_id, response_code, message); | 133 | callbackReport(request_id, response_code, message); |
131 | }); | 134 | }); |
132 | 135 | ||
133 | httpServer.listen(config.h2h_out.listen_port, function() { | 136 | httpServer.listen(config.h2h_out.listen_port, function() { |
134 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); | 137 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); |
135 | }); | 138 | }); |
136 | } | 139 | } |
137 | 140 | ||
138 | function start(_config, _callbackReport) { | 141 | function start(_config, _callbackReport) { |
139 | config = _config; | 142 | config = _config; |
140 | callbackReport = _callbackReport | 143 | callbackReport = _callbackReport |
141 | 144 | ||
142 | createServer(); | 145 | createServer(); |
143 | } | 146 | } |
144 | 147 | ||
145 | exports.start = start; | 148 | exports.start = start; |
146 | exports.topupRequest = topupRequest; | 149 | exports.topupRequest = topupRequest; |
147 | exports.calculateSignature = calculateSignature; | 150 | exports.calculateSignature = calculateSignature; |
148 | 151 |