Commit c66f76c6e02e87043ba3b94874e499d53bac6db9

Authored by Adhidarma Hadiwinoto
1 parent 9de9fde1f7
Exists in master

top level element

Showing 1 changed file with 1 additions and 1 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 strftime = require('strftime'); 5 var strftime = require('strftime');
6 var xor = require('base64-xor'); 6 var xor = require('base64-xor');
7 var request = require('request'); 7 var request = require('request');
8 8
9 var config; 9 var config;
10 var callbackReport; 10 var callbackReport;
11 11
12 var max_retry = 2; 12 var max_retry = 2;
13 var sleep_before_retry = 2000; 13 var sleep_before_retry = 2000;
14 14
15 function calculateSignature(userid, password, msisdn, timestamp) { 15 function calculateSignature(userid, password, msisdn, timestamp) {
16 var a = msisdn.substr(msisdn.length - 4) + timestamp; 16 var a = msisdn.substr(msisdn.length - 4) + timestamp;
17 var b = userid.substr(0, 4) + password; 17 var b = userid.substr(0, 4) + password;
18 18
19 return xor.encode(a,b); 19 return xor.encode(a,b);
20 } 20 }
21 21
22 function createPayload(task) { 22 function createPayload(task) {
23 var timestamp = strftime('%H%M%S'); 23 var timestamp = strftime('%H%M%S');
24 24
25 var payload = { 25 var payload = {
26 datacell: [ 26 datacell: [
27 { perintah: 'charge'}, 27 { perintah: 'charge'},
28 {oprcode: task['remoteProduct']}, 28 {oprcode: task['remoteProduct']},
29 {userid: config.h2h_out.userid}, 29 {userid: config.h2h_out.userid},
30 {time: timestamp}, 30 {time: timestamp},
31 {msisdn: task['destination']}, 31 {msisdn: task['destination']},
32 {ref_trxid: task['requestId']}, 32 {ref_trxid: task['requestId']},
33 {sgn: calculateSignature(config.h2h_out.userid, config.h2h_out.password, task['destination'], timestamp)} 33 {sgn: calculateSignature(config.h2h_out.userid, config.h2h_out.password, task['destination'], timestamp)}
34 ] 34 ]
35 }; 35 };
36 36
37 console.log(payload); 37 console.log(payload);
38 return xml(payload); 38 return "<?xml version=\"1.0\" ?>\n" + xml(payload);
39 } 39 }
40 40
41 function topupRequest(task, retry) { 41 function topupRequest(task, retry) {
42 if (config.globals.requests_count == undefined) { 42 if (config.globals.requests_count == undefined) {
43 config.globals.requests_count = 1; 43 config.globals.requests_count = 1;
44 } else { 44 } else {
45 config.globals.requests_count++; 45 config.globals.requests_count++;
46 } 46 }
47 47
48 if (config.globals.active_requests_count == undefined) { 48 if (config.globals.active_requests_count == undefined) {
49 config.globals.active_requests_count = 1; 49 config.globals.active_requests_count = 1;
50 } else { 50 } else {
51 config.globals.active_requests_count++; 51 config.globals.active_requests_count++;
52 } 52 }
53 53
54 if (config.globals.max_active_requests_count == undefined) { 54 if (config.globals.max_active_requests_count == undefined) {
55 config.globals.max_active_requests_count = config.globals.active_requests_count; 55 config.globals.max_active_requests_count = config.globals.active_requests_count;
56 } else { 56 } else {
57 config.globals.max_active_requests_count = math.max(config.globals.max_active_requests_count, config.globals.active_requests_count); 57 config.globals.max_active_requests_count = math.max(config.globals.max_active_requests_count, config.globals.active_requests_count);
58 } 58 }
59 59
60 60
61 if (retry === undefined) { 61 if (retry === undefined) {
62 retry = max_retry; 62 retry = max_retry;
63 } 63 }
64 64
65 var payload_xml = createPayload(task); 65 var payload_xml = createPayload(task);
66 console.log(payload_xml); 66 console.log(payload_xml);
67 67
68 request.post(config.h2h_out.partner, {message: payload_xml}, function(error, response, body) { 68 request.post(config.h2h_out.partner, {message: payload_xml}, function(error, response, body) {
69 if (error) { 69 if (error) {
70 var error_mesasge = 'Error requesting to partner: ' + error; 70 var error_mesasge = 'Error requesting to partner: ' + error;
71 console.log(error_message); 71 console.log(error_message);
72 callbackReport(task['requestId'], '40', error_message); 72 callbackReport(task['requestId'], '40', error_message);
73 return; 73 return;
74 } 74 }
75 75
76 if (response.statusCode != 200) { 76 if (response.statusCode != 200) {
77 var error_mesasge = 'HTTP status code = ' + response.statusCode; 77 var error_mesasge = 'HTTP status code = ' + response.statusCode;
78 console.log(error_message); 78 console.log(error_message);
79 callbackReport(task['requestId'], '40', error_message); 79 callbackReport(task['requestId'], '40', error_message);
80 return; 80 return;
81 } 81 }
82 82
83 console.log('Direct response from partner:'); 83 console.log('Direct response from partner:');
84 console.log(body); 84 console.log(body);
85 callbackReport(task['requestId'], '68', 'cek'); 85 callbackReport(task['requestId'], '68', 'cek');
86 86
87 });; 87 });;
88 } 88 }
89 89
90 function createServer() { 90 function createServer() {
91 var httpServer = http.createServer(function(req, res) { 91 var httpServer = http.createServer(function(req, res) {
92 console.log('Got request from partner: ' + req.url); 92 console.log('Got request from partner: ' + req.url);
93 res.end('OK'); 93 res.end('OK');
94 }); 94 });
95 95
96 httpServer.listen(config.h2h_out.listen_port, function() { 96 httpServer.listen(config.h2h_out.listen_port, function() {
97 console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); 97 console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port);
98 }); 98 });
99 } 99 }
100 100
101 101
102 function start(_config, _callbackReport) { 102 function start(_config, _callbackReport) {
103 config = _config; 103 config = _config;
104 callbackReport = _callbackReport 104 callbackReport = _callbackReport
105 105
106 createServer(); 106 createServer();
107 } 107 }
108 108
109 exports.start = start; 109 exports.start = start;
110 exports.topupRequest = topupRequest; 110 exports.topupRequest = topupRequest;
111 111