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