Commit 39781c95207184f6c7f131bc38422adf4a4412b3
1 parent
274f90989c
Exists in
master
coba multipart
Showing 1 changed file with 2 additions and 2 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 | 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 | request( | 90 | request( |
91 | { | 91 | { |
92 | method: 'PUT', | 92 | method: 'PUT', |
93 | uri: 'config.h2h_out.partner', | 93 | uri: 'config.h2h_out.partner', |
94 | multipart: [ | 94 | multipart: [{ |
95 | 'content-type': 'text/xml', | 95 | 'content-type': 'text/xml', |
96 | body: payload_xml | 96 | body: payload_xml |
97 | ] | 97 | }] |
98 | }, | 98 | }, |
99 | 99 | ||
100 | function(error, response, body) { | 100 | function(error, response, body) { |
101 | if (error) { | 101 | if (error) { |
102 | var error_mesasge = 'Error requesting to partner: ' + error; | 102 | var error_mesasge = 'Error requesting to partner: ' + error; |
103 | console.log(error_message); | 103 | console.log(error_message); |
104 | callbackReport(task['requestId'], '40', error_message); | 104 | callbackReport(task['requestId'], '40', error_message); |
105 | return; | 105 | return; |
106 | } | 106 | } |
107 | 107 | ||
108 | if (response.statusCode != 200) { | 108 | if (response.statusCode != 200) { |
109 | var error_mesasge = 'HTTP status code = ' + response.statusCode; | 109 | var error_mesasge = 'HTTP status code = ' + response.statusCode; |
110 | console.log(error_message); | 110 | console.log(error_message); |
111 | callbackReport(task['requestId'], '40', error_message); | 111 | callbackReport(task['requestId'], '40', error_message); |
112 | return; | 112 | return; |
113 | } | 113 | } |
114 | 114 | ||
115 | console.log('Direct response from partner:'); | 115 | console.log('Direct response from partner:'); |
116 | console.log(body); | 116 | console.log(body); |
117 | callbackReport(task['requestId'], '68', 'cek'); | 117 | callbackReport(task['requestId'], '68', 'cek'); |
118 | } | 118 | } |
119 | ); | 119 | ); |
120 | 120 | ||
121 | } | 121 | } |
122 | 122 | ||
123 | function createServer() { | 123 | function createServer() { |
124 | var httpServer = http.createServer(function(req, res) { | 124 | var httpServer = http.createServer(function(req, res) { |
125 | console.log('Got request from partner: ' + req.url); | 125 | console.log('Got request from partner: ' + req.url); |
126 | res.end('OK'); | 126 | res.end('OK'); |
127 | }); | 127 | }); |
128 | 128 | ||
129 | httpServer.listen(config.h2h_out.listen_port, function() { | 129 | httpServer.listen(config.h2h_out.listen_port, function() { |
130 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); | 130 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); |
131 | }); | 131 | }); |
132 | } | 132 | } |
133 | 133 | ||
134 | 134 | ||
135 | function start(_config, _callbackReport) { | 135 | function start(_config, _callbackReport) { |
136 | config = _config; | 136 | config = _config; |
137 | callbackReport = _callbackReport | 137 | callbackReport = _callbackReport |
138 | 138 | ||
139 | createServer(); | 139 | createServer(); |
140 | } | 140 | } |
141 | 141 | ||
142 | exports.start = start; | 142 | exports.start = start; |
143 | exports.topupRequest = topupRequest; | 143 | exports.topupRequest = topupRequest; |
144 | 144 |