Commit 7e26aeff4775207bc5bde33f966517ee35ca41b8
1 parent
88b4e0d50f
Exists in
master
coba plain http req
Showing 1 changed file with 26 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 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 version=\"1.0\" ?>\n" + 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 | /* | ||
68 | request({uri: config.h2h_out.partner, method: 'POST'}, function(error, response, body) { | 69 | request({uri: config.h2h_out.partner, method: 'POST'}, function(error, response, body) { |
69 | if (error) { | 70 | if (error) { |
70 | var error_mesasge = 'Error requesting to partner: ' + error; | 71 | var error_mesasge = 'Error requesting to partner: ' + error; |
71 | console.log(error_message); | 72 | console.log(error_message); |
72 | callbackReport(task['requestId'], '40', error_message); | 73 | callbackReport(task['requestId'], '40', error_message); |
73 | return; | 74 | return; |
74 | } | 75 | } |
75 | 76 | ||
76 | if (response.statusCode != 200) { | 77 | if (response.statusCode != 200) { |
77 | var error_mesasge = 'HTTP status code = ' + response.statusCode; | 78 | var error_mesasge = 'HTTP status code = ' + response.statusCode; |
78 | console.log(error_message); | 79 | console.log(error_message); |
79 | callbackReport(task['requestId'], '40', error_message); | 80 | callbackReport(task['requestId'], '40', error_message); |
80 | return; | 81 | return; |
81 | } | 82 | } |
82 | 83 | ||
83 | console.log('Direct response from partner:'); | 84 | console.log('Direct response from partner:'); |
84 | console.log(body); | 85 | console.log(body); |
85 | callbackReport(task['requestId'], '68', 'cek'); | 86 | callbackReport(task['requestId'], '68', 'cek'); |
86 | 87 | ||
87 | }).end(payload_xml);; | 88 | }).end(payload_xml); |
89 | */ | ||
88 | 90 | ||
91 | var postRequest = { | ||
92 | host: "202.152.62.2", | ||
93 | path: "/RELOAD97.php", | ||
94 | port: 7713, | ||
95 | method: "POST" | ||
96 | }; | ||
97 | |||
98 | var buffer = ""; | ||
99 | var req = http.request( postRequest, function( res ) { | ||
100 | |||
101 | console.log( res.statusCode ); | ||
102 | var buffer = ""; | ||
103 | res.on( "data", function( data ) { buffer = buffer + data; } ); | ||
104 | res.on( "end", function( data ) { console.log( buffer ); } ); | ||
105 | |||
106 | }); | ||
107 | |||
108 | req.on('error', function(e) { | ||
109 | console.log('problem with request: ' + e.message); | ||
110 | }); | ||
111 | |||
112 | req.write( payload_xml ); | ||
113 | req.end(); | ||
89 | } | 114 | } |
90 | 115 | ||
91 | function createServer() { | 116 | function createServer() { |
92 | var httpServer = http.createServer(function(req, res) { | 117 | var httpServer = http.createServer(function(req, res) { |
93 | console.log('Got request from partner: ' + req.url); | 118 | console.log('Got request from partner: ' + req.url); |
94 | res.end('OK'); | 119 | res.end('OK'); |
95 | }); | 120 | }); |
96 | 121 | ||
97 | httpServer.listen(config.h2h_out.listen_port, function() { | 122 | httpServer.listen(config.h2h_out.listen_port, function() { |
98 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); | 123 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); |
99 | }); | 124 | }); |
100 | } | 125 | } |
101 | 126 | ||
102 | 127 | ||
103 | function start(_config, _callbackReport) { | 128 | function start(_config, _callbackReport) { |
104 | config = _config; | 129 | config = _config; |
105 | callbackReport = _callbackReport | 130 | callbackReport = _callbackReport |
106 | 131 | ||
107 | createServer(); | 132 | createServer(); |
108 | } | 133 | } |
109 | 134 | ||
110 | exports.start = start; | 135 | exports.start = start; |
111 | exports.topupRequest = topupRequest; | 136 | exports.topupRequest = topupRequest; |
112 | 137 |