Commit cfd9b2dbd10510ed6ae8522c8a128d66020e4eef

Authored by Adhidarma Hadiwinoto
1 parent 1ffca70467
Exists in master

Nomor telp salah

Showing 1 changed file with 2 additions and 0 deletions Inline Diff

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