Commit 9ab77682366d0575e0a0a8d43ae6b5eccddfe944
1 parent
cfd9b2dbd1
Exists in
master
MSISDN tidak ditemukan
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 | 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 | if (message.indexOf('Jenis produk tidak cocok') >= 0) { |
103 | response_code = '14'; | 103 | response_code = '14'; |
104 | } else if (message.indexOf('GAGAL. Nomor telp salah.') >= 0) { | 104 | } else if (message.indexOf('GAGAL. Nomor telp salah.') >= 0) { |
105 | response_code = '14'; | 105 | response_code = '14'; |
106 | } else if (message.indexOf('GAGAL. MSISDN tidak ditemukan') >= 0) { | ||
107 | response_code = '14'; | ||
106 | } | 108 | } |
109 | |||
107 | } | 110 | } |
108 | 111 | ||
109 | callbackReport(request_id, response_code, message); | 112 | callbackReport(request_id, response_code, message); |
110 | }); | 113 | }); |
111 | 114 | ||
112 | } | 115 | } |
113 | 116 | ||
114 | function createServer() { | 117 | function createServer() { |
115 | 118 | ||
116 | var httpServer = http.createServer(function(req, res) { | 119 | var httpServer = http.createServer(function(req, res) { |
117 | //console.log('Got request from partner ("' + req.url + '")'); | 120 | //console.log('Got request from partner ("' + req.url + '")'); |
118 | 121 | ||
119 | res.end('OK'); | 122 | res.end('OK'); |
120 | 123 | ||
121 | console.log('Reverse Report:'); | 124 | console.log('Reverse Report:'); |
122 | var qs = url.parse(req.url, true).query; | 125 | var qs = url.parse(req.url, true).query; |
123 | console.log(qs); | 126 | console.log(qs); |
124 | 127 | ||
125 | var response_code = '68'; | 128 | var response_code = '68'; |
126 | var request_id = qs.pid; | 129 | var request_id = qs.pid; |
127 | var message = qs.msg; | 130 | var message = qs.msg; |
128 | 131 | ||
129 | if (qs.code == 2) { | 132 | if (qs.code == 2) { |
130 | // gagal | 133 | // gagal |
131 | response_code = '40'; | 134 | response_code = '40'; |
132 | } else if (qs.code == 3) { | 135 | } else if (qs.code == 3) { |
133 | // refund | 136 | // refund |
134 | response_code = '40'; | 137 | response_code = '40'; |
135 | } else if (qs.code == 4) { | 138 | } else if (qs.code == 4) { |
136 | response_code = '00'; | 139 | response_code = '00'; |
137 | message = 'SN=' + qs.sn + ';' + message; | 140 | message = 'SN=' + qs.sn + ';' + message; |
138 | } | 141 | } |
139 | 142 | ||
140 | callbackReport(request_id, response_code, message); | 143 | callbackReport(request_id, response_code, message); |
141 | }); | 144 | }); |
142 | 145 | ||
143 | httpServer.listen(config.h2h_out.listen_port, function() { | 146 | httpServer.listen(config.h2h_out.listen_port, function() { |
144 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); | 147 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); |
145 | }); | 148 | }); |
146 | } | 149 | } |
147 | 150 | ||
148 | function start(_config, _callbackReport) { | 151 | function start(_config, _callbackReport) { |
149 | config = _config; | 152 | config = _config; |
150 | callbackReport = _callbackReport | 153 | callbackReport = _callbackReport |
151 | 154 | ||
152 | createServer(); | 155 | createServer(); |
153 | } | 156 | } |
154 | 157 | ||
155 | exports.start = start; | 158 | exports.start = start; |
156 | exports.topupRequest = topupRequest; | 159 | exports.topupRequest = topupRequest; |
157 | exports.calculateSignature = calculateSignature; | 160 | exports.calculateSignature = calculateSignature; |
158 | 161 |