Commit 76325823cec64a0bec0ea9ca37c44dd34e582209
1 parent
f53f2def06
Exists in
master
kekurangan kurung tutup
Showing 1 changed file with 1 additions and 1 deletions Inline Diff
partner-datacell.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 | function calculateSignature(userid, password, msisdn, timestamp) { | 16 | function calculateSignature(userid, password, msisdn, timestamp) { |
17 | var a = msisdn.substr(msisdn.length - 4) + timestamp; | 17 | var a = msisdn.substr(msisdn.length - 4) + timestamp; |
18 | console.log(a); | 18 | console.log(a); |
19 | var b = userid.substr(0, 4) + password; | 19 | var b = userid.substr(0, 4) + password; |
20 | console.log(b); | 20 | console.log(b); |
21 | 21 | ||
22 | return xor.encode(a,b); | 22 | return xor.encode(a,b); |
23 | } | 23 | } |
24 | 24 | ||
25 | function createPayload(task) { | 25 | function createPayload(task) { |
26 | var timestamp = strftime('%H%M%S'); | 26 | var timestamp = strftime('%H%M%S'); |
27 | 27 | ||
28 | var payload = { | 28 | var payload = { |
29 | datacell: [ | 29 | datacell: [ |
30 | { perintah: 'charge'}, | 30 | { perintah: 'charge'}, |
31 | {oprcode: task['remoteProduct']}, | 31 | {oprcode: task['remoteProduct']}, |
32 | {userid: config.h2h_out.userid}, | 32 | {userid: config.h2h_out.userid}, |
33 | {time: timestamp}, | 33 | {time: timestamp}, |
34 | {msisdn: task['destination']}, | 34 | {msisdn: task['destination']}, |
35 | {ref_trxid: task['requestId']}, | 35 | {ref_trxid: task['requestId']}, |
36 | {sgn: calculateSignature(config.h2h_out.userid, config.h2h_out.password, task['destination'], timestamp)} | 36 | {sgn: calculateSignature(config.h2h_out.userid, config.h2h_out.password, task['destination'], timestamp)} |
37 | ] | 37 | ] |
38 | }; | 38 | }; |
39 | 39 | ||
40 | console.log(payload); | 40 | console.log(payload); |
41 | return "<?xml version=\"1.0\" ?>\n" + xml(payload); | 41 | return "<?xml version=\"1.0\" ?>\n" + xml(payload); |
42 | } | 42 | } |
43 | 43 | ||
44 | function topupRequest(task, retry) { | 44 | function topupRequest(task, retry) { |
45 | if (config.globals.requests_count == undefined) { | 45 | if (config.globals.requests_count == undefined) { |
46 | config.globals.requests_count = 1; | 46 | config.globals.requests_count = 1; |
47 | } else { | 47 | } else { |
48 | config.globals.requests_count++; | 48 | config.globals.requests_count++; |
49 | } | 49 | } |
50 | 50 | ||
51 | if (config.globals.active_requests_count == undefined) { | 51 | if (config.globals.active_requests_count == undefined) { |
52 | config.globals.active_requests_count = 1; | 52 | config.globals.active_requests_count = 1; |
53 | } else { | 53 | } else { |
54 | config.globals.active_requests_count++; | 54 | config.globals.active_requests_count++; |
55 | } | 55 | } |
56 | 56 | ||
57 | if (config.globals.max_active_requests_count == undefined) { | 57 | if (config.globals.max_active_requests_count == undefined) { |
58 | config.globals.max_active_requests_count = config.globals.active_requests_count; | 58 | config.globals.max_active_requests_count = config.globals.active_requests_count; |
59 | } else { | 59 | } else { |
60 | config.globals.max_active_requests_count = math.max(config.globals.max_active_requests_count, config.globals.active_requests_count); | 60 | config.globals.max_active_requests_count = math.max(config.globals.max_active_requests_count, config.globals.active_requests_count); |
61 | } | 61 | } |
62 | 62 | ||
63 | 63 | ||
64 | if (retry === undefined) { | 64 | if (retry === undefined) { |
65 | retry = max_retry; | 65 | retry = max_retry; |
66 | } | 66 | } |
67 | 67 | ||
68 | var payload_xml = createPayload(task); | 68 | var payload_xml = createPayload(task); |
69 | console.log(payload_xml); | 69 | console.log(payload_xml); |
70 | 70 | ||
71 | var postRequest = { | 71 | var postRequest = { |
72 | host: "202.152.62.2", | 72 | host: "202.152.62.2", |
73 | path: "/RELOAD97.php", | 73 | path: "/RELOAD97.php", |
74 | port: 7713, | 74 | port: 7713, |
75 | method: "POST", | 75 | method: "POST", |
76 | headers: { | 76 | headers: { |
77 | 'Content-Type': 'text/xml', | 77 | 'Content-Type': 'text/xml', |
78 | 'Content-Length': Buffer.byteLength(payload_xml) | 78 | 'Content-Length': Buffer.byteLength(payload_xml) |
79 | } | 79 | } |
80 | }; | 80 | }; |
81 | 81 | ||
82 | var buffer = ""; | 82 | var buffer = ""; |
83 | var req = http.request( postRequest, function( res ) { | 83 | var req = http.request( postRequest, function( res ) { |
84 | 84 | ||
85 | console.log('Status code: ' + res.statusCode ); | 85 | console.log('Status code: ' + res.statusCode ); |
86 | var buffer = ""; | 86 | var buffer = ""; |
87 | res.on( "data", function( data ) { buffer = buffer + data; } ); | 87 | res.on( "data", function( data ) { buffer = buffer + data; } ); |
88 | res.on( "end", function( data ) { | 88 | res.on( "end", function( data ) { |
89 | 89 | ||
90 | xml2js(buffer, function (err, result) { | 90 | xml2js(buffer, function (err, result) { |
91 | if (err) { | 91 | if (err) { |
92 | console.log(buffer); | 92 | console.log(buffer); |
93 | callbackReport(task['requestId'], '40', buffer); | 93 | callbackReport(task['requestId'], '40', buffer); |
94 | return; | 94 | return; |
95 | } | 95 | } |
96 | 96 | ||
97 | console.log(result); | 97 | console.log(result); |
98 | } | 98 | }); |
99 | 99 | ||
100 | }); | 100 | }); |
101 | 101 | ||
102 | }); | 102 | }); |
103 | 103 | ||
104 | req.on('error', function(e) { | 104 | req.on('error', function(e) { |
105 | console.log('problem with request: ' + e.message); | 105 | console.log('problem with request: ' + e.message); |
106 | }); | 106 | }); |
107 | 107 | ||
108 | req.write( payload_xml ); | 108 | req.write( payload_xml ); |
109 | req.end(); | 109 | req.end(); |
110 | } | 110 | } |
111 | 111 | ||
112 | function createServer() { | 112 | function createServer() { |
113 | var httpServer = http.createServer(function(req, res) { | 113 | var httpServer = http.createServer(function(req, res) { |
114 | console.log('Got request from partner, url: ' + req.url); | 114 | console.log('Got request from partner, url: ' + req.url); |
115 | console.log('REQ:'); | 115 | console.log('REQ:'); |
116 | console.log(req); | 116 | console.log(req); |
117 | res.end('OK'); | 117 | res.end('OK'); |
118 | }); | 118 | }); |
119 | 119 | ||
120 | httpServer.listen(config.h2h_out.listen_port, function() { | 120 | httpServer.listen(config.h2h_out.listen_port, function() { |
121 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); | 121 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); |
122 | }); | 122 | }); |
123 | } | 123 | } |
124 | 124 | ||
125 | 125 | ||
126 | function start(_config, _callbackReport) { | 126 | function start(_config, _callbackReport) { |
127 | config = _config; | 127 | config = _config; |
128 | callbackReport = _callbackReport | 128 | callbackReport = _callbackReport |
129 | 129 | ||
130 | createServer(); | 130 | createServer(); |
131 | } | 131 | } |
132 | 132 | ||
133 | exports.start = start; | 133 | exports.start = start; |
134 | exports.topupRequest = topupRequest; | 134 | exports.topupRequest = topupRequest; |
135 | 135 |