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