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