Commit 80d5f6d276fc81b82363263ebcd3b526c0b2359b
1 parent
b280085258
Exists in
master
resync with node-sate24
Showing 2 changed files with 21 additions and 3 deletions Inline Diff
package.json
1 | { | 1 | { |
2 | "name": "sate24-to-trugee", | 2 | "name": "sate24-to-trugee", |
3 | "version": "0.0.1", | 3 | "version": "0.0.1", |
4 | "description": "ST24 to Trugee", | 4 | "description": "ST24 to Trugee", |
5 | "main": "index.js", | 5 | "main": "index.js", |
6 | "scripts": { | 6 | "scripts": { |
7 | "test": "mocha" | 7 | "test": "mocha" |
8 | }, | 8 | }, |
9 | "repository": { | 9 | "repository": { |
10 | "type": "git", | 10 | "type": "git", |
11 | "url": "git@gitlab.kodesumber.com:reload97/sate24-to-trugee.git" | 11 | "url": "git@gitlab.kodesumber.com:reload97/sate24-to-trugee.git" |
12 | }, | 12 | }, |
13 | "keywords": [ | 13 | "keywords": [ |
14 | "datacell", | 14 | "datacell", |
15 | "ppob", | 15 | "ppob", |
16 | "st24" | 16 | "st24" |
17 | ], | 17 | ], |
18 | "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>", | 18 | "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>", |
19 | "license": "BSD", | 19 | "license": "BSD", |
20 | "dependencies": { | 20 | "dependencies": { |
21 | "sate24": "git+http://git@gitlab.kodesumber.com/reload97/node-sate24.git", | 21 | "sate24": "git+http://git@gitlab.kodesumber.com/reload97/node-sate24.git", |
22 | "iniparser": "~1.0.5", | 22 | "iniparser": "~1.0.5", |
23 | "base64-xor": "~0.10.0", | 23 | "base64-xor": "~0.10.0", |
24 | "request": "~2.60.0", | 24 | "request": "~2.60.0", |
25 | "mathjs": "~1.7.1", | 25 | "mathjs": "~1.7.1", |
26 | "xml": "~1.0.0", | 26 | "xml": "~1.0.0", |
27 | "xml2js": "~0.4.9", | 27 | "xml2js": "~0.4.9", |
28 | "strftime": "~0.9.2", | 28 | "strftime": "~0.9.2", |
29 | "url": "~0.10.3" | 29 | "url": "~0.10.3", |
30 | "sate24-expresso": "git+http://gitlab.kodesumber.com/reload97/sate24-expresso.git", | ||
31 | "winston": "~2.1.1" | ||
30 | } | 32 | } |
31 | } | 33 | } |
32 | 34 |
partner-trugee.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 | var aaa; | ||
13 | var logger; | ||
12 | 14 | ||
13 | var max_retry = 2; | 15 | var max_retry = 2; |
14 | var sleep_before_retry = 2000; | 16 | var sleep_before_retry = 2000; |
15 | 17 | ||
16 | var trx_balances = {}; | 18 | var trx_balances = {}; |
17 | var trx_prices = {}; | 19 | var trx_prices = {}; |
18 | 20 | ||
19 | function calculateSignature(password, msisdn, timestamp) { | 21 | function calculateSignature(password, msisdn, timestamp) { |
20 | var a = timestamp + msisdn.substr(msisdn.length - 4); | 22 | var a = timestamp + msisdn.substr(msisdn.length - 4); |
21 | var b = msisdn.substr(msisdn.length - 4).split('').reverse().join('') + password; | 23 | var b = msisdn.substr(msisdn.length - 4).split('').reverse().join('') + password; |
22 | 24 | ||
23 | return xor.encode(a,b); | 25 | return xor.encode(a,b); |
24 | } | 26 | } |
25 | 27 | ||
26 | function createPayload(task) { | 28 | function createPayload(task) { |
27 | var timestamp = strftime('%H%M%S'); | 29 | var timestamp = strftime('%H%M%S'); |
28 | 30 | ||
29 | var payload = { | 31 | var payload = { |
30 | pulsamatic: [ | 32 | pulsamatic: [ |
31 | {command: 'TOPUP'}, | 33 | {command: 'TOPUP'}, |
32 | {vtype: task['remoteProduct']}, | 34 | {vtype: task['remoteProduct']}, |
33 | {userid: config.h2h_out.userid}, | 35 | {userid: config.h2h_out.userid}, |
34 | {time: timestamp}, | 36 | {time: timestamp}, |
35 | {msisdn: task['destination']}, | 37 | {msisdn: task['destination']}, |
36 | {trxid: task['requestId']}, | 38 | {trxid: task['requestId']}, |
37 | {sign: calculateSignature(config.h2h_out.password, task['destination'], timestamp)} | 39 | {sign: calculateSignature(config.h2h_out.password, task['destination'], timestamp)} |
38 | ] | 40 | ] |
39 | }; | 41 | }; |
40 | 42 | ||
41 | console.log(payload); | 43 | console.log(payload); |
42 | return "<?xml version=\"1.0\" ?>\n" + xml(payload); | 44 | return "<?xml version=\"1.0\" ?>\n" + xml(payload); |
43 | } | 45 | } |
44 | 46 | ||
45 | function topupRequest(task, retry) { | 47 | function topupRequest(task, retry) { |
46 | 48 | ||
47 | console.log('Requesting to partner:'); | 49 | console.log('Requesting to partner:'); |
48 | var payload_xml = createPayload(task); | 50 | var payload_xml = createPayload(task); |
49 | var partner = url.parse(config.h2h_out.partner); | 51 | var partner = url.parse(config.h2h_out.partner); |
50 | 52 | ||
51 | var request_options = { | 53 | var request_options = { |
52 | host: partner.hostname, | 54 | host: partner.hostname, |
53 | path: partner.path, | 55 | path: partner.path, |
54 | port: partner.port, | 56 | port: partner.port, |
55 | method: "POST", | 57 | method: "POST", |
56 | headers: { | 58 | headers: { |
57 | 'Content-Type': 'text/xml', | 59 | 'Content-Type': 'text/xml', |
58 | 'Content-Length': Buffer.byteLength(payload_xml) | 60 | 'Content-Length': Buffer.byteLength(payload_xml) |
59 | } | 61 | } |
60 | }; | 62 | }; |
61 | 63 | ||
62 | var buffer = ""; | 64 | var buffer = ""; |
63 | var req = http.request(request_options, function( res ) { | 65 | var req = http.request(request_options, function( res ) { |
64 | 66 | ||
65 | console.log('Status code: ' + res.statusCode ); | 67 | console.log('Status code: ' + res.statusCode ); |
66 | var buffer = ""; | 68 | var buffer = ""; |
67 | res.on( "data", function( data ) { buffer = buffer + data; } ); | 69 | res.on( "data", function( data ) { buffer = buffer + data; } ); |
68 | res.on( "end", function( data ) { | 70 | res.on( "end", function( data ) { |
69 | directResponseHandler(buffer, task['requestId']); | 71 | directResponseHandler(buffer, task['requestId']); |
70 | }); | 72 | }); |
71 | 73 | ||
72 | }); | 74 | }); |
73 | 75 | ||
74 | req.on('error', function(e) { | 76 | req.on('error', function(e) { |
75 | console.log('problem with request: ' + e.message); | 77 | console.log('problem with request: ' + e.message); |
76 | callbackReport(task['requestId'], '40', e.message); | 78 | callbackReport(task['requestId'], '40', e.message); |
77 | }); | 79 | }); |
78 | 80 | ||
79 | req.write( payload_xml ); | 81 | req.write( payload_xml ); |
80 | req.end(); | 82 | req.end(); |
81 | } | 83 | } |
82 | 84 | ||
83 | function directResponseHandler(body, request_id) { | 85 | function directResponseHandler(body, request_id) { |
84 | console.log('Direct Response:'); | 86 | console.log('Direct Response:'); |
85 | xml2js(body, function (err, result) { | 87 | xml2js(body, function (err, result) { |
86 | if (err) { | 88 | if (err) { |
87 | console.log(body); | 89 | console.log(body); |
88 | callbackReport(request_id, '40', buffer); | 90 | callbackReport(request_id, '40', buffer); |
89 | return; | 91 | return; |
90 | } | 92 | } |
91 | console.log(result); | 93 | console.log(result); |
92 | 94 | ||
93 | var response_code = '68'; | 95 | var response_code = '68'; |
94 | 96 | ||
95 | var request_id = result.pulsamatic.partner_trxid[0].trim(); | 97 | var request_id = result.pulsamatic.partner_trxid[0].trim(); |
96 | var message = result.pulsamatic.message[0].trim(); | 98 | var message = result.pulsamatic.message[0].trim(); |
97 | var status = result.pulsamatic.result[0].trim(); | 99 | var status = result.pulsamatic.result[0].trim(); |
98 | 100 | ||
99 | if (status === 'failed') { | 101 | if (status === 'failed') { |
100 | response_code = '40'; | 102 | response_code = '40'; |
101 | 103 | ||
102 | var new_response_code = responseCodeFromMessage(message); | 104 | var new_response_code = responseCodeFromMessage(message); |
103 | if (new_response_code) { | 105 | if (new_response_code) { |
104 | response_code = new_response_code; | 106 | response_code = new_response_code; |
105 | } | 107 | } |
106 | 108 | ||
107 | } | 109 | } |
108 | 110 | ||
109 | callbackReport(request_id, response_code, message); | 111 | callbackReport(request_id, response_code, message); |
110 | }); | 112 | }); |
111 | } | 113 | } |
112 | 114 | ||
113 | function responseCodeFromMessage(message) { | 115 | function responseCodeFromMessage(message) { |
114 | if (message.indexOf('Jenis produk tidak cocok') >= 0) { | 116 | if (message.indexOf('Jenis produk tidak cocok') >= 0) { |
115 | return '14'; | 117 | return '14'; |
116 | } else if (message.indexOf('GAGAL. Nomor telp salah.') >= 0) { | 118 | } else if (message.indexOf('GAGAL. Nomor telp salah.') >= 0) { |
117 | return '14'; | 119 | return '14'; |
118 | } else if (message.indexOf('GAGAL. MSISDN tidak ditemukan') >= 0) { | 120 | } else if (message.indexOf('GAGAL. MSISDN tidak ditemukan') >= 0) { |
119 | return '14'; | 121 | return '14'; |
120 | } else if (message.indexOf('GAGAL. Jenis produk ini sedang gangguan.') >= 0) { | 122 | } else if (message.indexOf('GAGAL. Jenis produk ini sedang gangguan.') >= 0) { |
121 | return '13'; | 123 | return '13'; |
122 | } | 124 | } |
123 | return; | 125 | return; |
124 | } | 126 | } |
125 | 127 | ||
126 | function createServer() { | 128 | function createServer() { |
127 | 129 | ||
128 | var httpServer = http.createServer(function(req, res) { | 130 | var httpServer = http.createServer(function(req, res) { |
129 | //console.log('Got request from partner ("' + req.url + '")'); | 131 | //console.log('Got request from partner ("' + req.url + '")'); |
130 | 132 | ||
131 | res.end('OK'); | 133 | res.end('OK'); |
132 | 134 | ||
133 | console.log('Reverse Report:'); | 135 | console.log('Reverse Report:'); |
134 | var qs = url.parse(req.url, true).query; | 136 | var qs = url.parse(req.url, true).query; |
135 | console.log(qs); | 137 | console.log(qs); |
136 | 138 | ||
137 | var response_code = '68'; | 139 | var response_code = '68'; |
138 | var request_id = qs.pid; | 140 | var request_id = qs.pid; |
139 | var message = qs.msg; | 141 | var message = qs.msg; |
140 | 142 | ||
141 | if (qs.code == 2) { | 143 | if (qs.code == 2) { |
142 | // gagal | 144 | // gagal |
143 | response_code = '40'; | 145 | response_code = '40'; |
144 | } else if (qs.code == 3) { | 146 | } else if (qs.code == 3) { |
145 | // refund | 147 | // refund |
146 | response_code = '40'; | 148 | response_code = '40'; |
147 | } else if (qs.code == 4) { | 149 | } else if (qs.code == 4) { |
148 | response_code = '00'; | 150 | response_code = '00'; |
149 | message = 'SN=' + qs.sn + ';' + message; | 151 | message = 'SN=' + qs.sn + ';' + message; |
150 | } | 152 | } |
151 | 153 | ||
152 | if (response_code == '40') { | 154 | if (response_code == '40') { |
153 | var new_response_code = responseCodeFromMessage(message); | 155 | var new_response_code = responseCodeFromMessage(message); |
154 | if (new_response_code) { | 156 | if (new_response_code) { |
155 | response_code = new_response_code; | 157 | response_code = new_response_code; |
156 | } | 158 | } |
157 | } | 159 | } |
158 | 160 | ||
159 | callbackReport(request_id, response_code, message); | 161 | callbackReport(request_id, response_code, message); |
160 | }); | 162 | }); |
161 | 163 | ||
162 | httpServer.listen(config.h2h_out.listen_port, function() { | 164 | httpServer.listen(config.h2h_out.listen_port, function() { |
163 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); | 165 | console.log('HTTP Reverse/Report server listen on port ' + config.h2h_out.listen_port); |
164 | }); | 166 | }); |
165 | } | 167 | } |
166 | 168 | ||
167 | function start(_config, _callbackReport) { | 169 | function start(_config, _callbackReport, options) { |
168 | config = _config; | 170 | config = _config; |
169 | callbackReport = _callbackReport | 171 | callbackReport = _callbackReport |
170 | 172 | ||
173 | if (options && options.aaa) { | ||
174 | aaa = options.aaa; | ||
175 | } | ||
176 | |||
177 | if (options && options.logger) { | ||
178 | logger = options.logger; | ||
179 | } else { | ||
180 | logger = new winston.Logger({ | ||
181 | transports: [ | ||
182 | new (winston.transports.Console)() | ||
183 | ] | ||
184 | }); | ||
185 | } | ||
186 | |||
171 | createServer(); | 187 | createServer(); |
172 | } | 188 | } |
173 | 189 | ||
174 | exports.start = start; | 190 | exports.start = start; |
175 | exports.topupRequest = topupRequest; | 191 | exports.topupRequest = topupRequest; |
176 | exports.calculateSignature = calculateSignature; | 192 | exports.calculateSignature = calculateSignature; |
177 | 193 |