Commit d2daacf79c5bb4ee82e0a63e04300cc03c9ced4a
1 parent
c956984a75
Exists in
master
try to resolve get sn
Showing 1 changed file with 2 additions and 2 deletions Inline Diff
partner-cjk.js
1 | var winston = require('winston'); | 1 | var winston = require('winston'); |
2 | var crypto = require('crypto'); | 2 | var crypto = require('crypto'); |
3 | var xml = require('xml'); | 3 | var xml = require('xml'); |
4 | var url = require('url'); | 4 | var url = require('url'); |
5 | var http = require('http'); | 5 | var http = require('http'); |
6 | var xml2js = require('xml2js').parseString; | 6 | var xml2js = require('xml2js').parseString; |
7 | 7 | ||
8 | var config; | 8 | var config; |
9 | var callbackReport; | 9 | var callbackReport; |
10 | var aaa; | 10 | var aaa; |
11 | var logger; | 11 | var logger; |
12 | var options; | 12 | var options; |
13 | 13 | ||
14 | var maxRetry = 10; | 14 | var maxRetry = 10; |
15 | var sleepBeforeRetry = 30; | 15 | var sleepBeforeRetry = 30; |
16 | 16 | ||
17 | function start(_config, _callbackReport, options) { | 17 | function start(_config, _callbackReport, options) { |
18 | config = _config; | 18 | config = _config; |
19 | callbackReport = _callbackReport | 19 | callbackReport = _callbackReport |
20 | 20 | ||
21 | if (options && options.aaa) { | 21 | if (options && options.aaa) { |
22 | aaa = options.aaa; | 22 | aaa = options.aaa; |
23 | } | 23 | } |
24 | 24 | ||
25 | if (options && options.logger) { | 25 | if (options && options.logger) { |
26 | logger = options.logger; | 26 | logger = options.logger; |
27 | } else { | 27 | } else { |
28 | logger = new winston.Logger({ | 28 | logger = new winston.Logger({ |
29 | transports: [ | 29 | transports: [ |
30 | new (winston.transports.Console)() | 30 | new (winston.transports.Console)() |
31 | ] | 31 | ] |
32 | }); | 32 | }); |
33 | } | 33 | } |
34 | } | 34 | } |
35 | 35 | ||
36 | function calculateSignature(params) { | 36 | function calculateSignature(params) { |
37 | var passwordHash = crypto.createHash('sha1').update(params.password).digest().toString('hex'); | 37 | var passwordHash = crypto.createHash('sha1').update(params.password).digest().toString('hex'); |
38 | var plain = params.trxtype + params.prdcode + params.value + params.msisdn + params.trxid + params.uid + passwordHash; | 38 | var plain = params.trxtype + params.prdcode + params.value + params.msisdn + params.trxid + params.uid + passwordHash; |
39 | var signature = crypto.createHash('sha256').update(plain).digest().toString('hex'); | 39 | var signature = crypto.createHash('sha256').update(plain).digest().toString('hex'); |
40 | 40 | ||
41 | try { | 41 | try { |
42 | logger.verbose('Signature calculated', {plain: plain, signature: signature}); | 42 | logger.verbose('Signature calculated', {plain: plain, signature: signature}); |
43 | } | 43 | } |
44 | catch(err) {} | 44 | catch(err) {} |
45 | 45 | ||
46 | return signature; | 46 | return signature; |
47 | } | 47 | } |
48 | 48 | ||
49 | function createXmlPayload(params) { | 49 | function createXmlPayload(params) { |
50 | var payload = "<?xml version=\"1.0\" ?>\n" + xml({ | 50 | var payload = "<?xml version=\"1.0\" ?>\n" + xml({ |
51 | ciwaru: [ | 51 | ciwaru: [ |
52 | {trxtype: params.trxtype}, | 52 | {trxtype: params.trxtype}, |
53 | {prdcode: params.prdcode}, | 53 | {prdcode: params.prdcode}, |
54 | {value: params.value}, | 54 | {value: params.value}, |
55 | {msisdn: params.msisdn}, | 55 | {msisdn: params.msisdn}, |
56 | {trxid: params.trxid}, | 56 | {trxid: params.trxid}, |
57 | {uid: params.uid}, | 57 | {uid: params.uid}, |
58 | {hash: calculateSignature(params)} | 58 | {hash: calculateSignature(params)} |
59 | ] | 59 | ] |
60 | }); | 60 | }); |
61 | 61 | ||
62 | try { logger.verbose("Payload: " + payload); } | 62 | try { logger.verbose("Payload: " + payload); } |
63 | catch(errLog) {} | 63 | catch(errLog) {} |
64 | 64 | ||
65 | return payload; | 65 | return payload; |
66 | } | 66 | } |
67 | 67 | ||
68 | function getSNFromMessage(message) { | 68 | function getSNFromMessage(message) { |
69 | var sn_match = message.match(/SN: (\w+)/); | ||
69 | try { | 70 | try { |
70 | var sn_match = message.match(/SN: (\w+)/); | ||
71 | |||
72 | logger.verbose('Got SN: ' + sn_match[1]); | 71 | logger.verbose('Got SN: ' + sn_match[1]); |
73 | return sn_match[1]; | 72 | return sn_match[1]; |
74 | } | 73 | } |
75 | catch(err) { | 74 | catch(err) { |
75 | logger.verbose('Exception on getting sn from message', {err: err, sn_match: sn_match}); | ||
76 | return ''; | 76 | return ''; |
77 | } | 77 | } |
78 | } | 78 | } |
79 | 79 | ||
80 | function hasSuccessKeywords(message) { | 80 | function hasSuccessKeywords(message) { |
81 | var keywords = ['SUKSES', 'Finish']; | 81 | var keywords = ['SUKSES', 'Finish']; |
82 | 82 | ||
83 | var count = keywords.length; | 83 | var count = keywords.length; |
84 | for (var i=0; i < count; i++) { | 84 | for (var i=0; i < count; i++) { |
85 | if (message.indexOf(keywords[i]) >= 0) { | 85 | if (message.indexOf(keywords[i]) >= 0) { |
86 | return true; | 86 | return true; |
87 | } | 87 | } |
88 | } | 88 | } |
89 | return false; | 89 | return false; |
90 | } | 90 | } |
91 | 91 | ||
92 | function supplierRcToST24Rc(rc) { | 92 | function supplierRcToST24Rc(rc) { |
93 | var rcs = { | 93 | var rcs = { |
94 | '0001': '40', | 94 | '0001': '40', |
95 | '0019': '13', // produk tidak tersedia | 95 | '0019': '13', // produk tidak tersedia |
96 | } | 96 | } |
97 | 97 | ||
98 | if (rcs[rc]) { | 98 | if (rcs[rc]) { |
99 | return rcs[rc]; | 99 | return rcs[rc]; |
100 | } else { | 100 | } else { |
101 | return; | 101 | return; |
102 | } | 102 | } |
103 | } | 103 | } |
104 | 104 | ||
105 | function topupResponseHandler(body, task) { | 105 | function topupResponseHandler(body, task) { |
106 | //logger.info('Got reply from partner', {body: body}); | 106 | //logger.info('Got reply from partner', {body: body}); |
107 | xml2js(body, function(err, result) { | 107 | xml2js(body, function(err, result) { |
108 | if (err) { | 108 | if (err) { |
109 | logger.warn('Got invalid XML from partner', {err: err, body: body, task: task}); | 109 | logger.warn('Got invalid XML from partner', {err: err, body: body, task: task}); |
110 | } | 110 | } |
111 | logger.info('XML message from partner', {result: result}); | 111 | logger.info('XML message from partner', {result: result}); |
112 | 112 | ||
113 | var rc = '68'; | 113 | var rc = '68'; |
114 | var message = result.ciwaru.msg[0]; | 114 | var message = result.ciwaru.msg[0]; |
115 | 115 | ||
116 | var trxid = 0; | 116 | var trxid = 0; |
117 | try { | 117 | try { |
118 | trxid = result.ciwaru.reqnum[0]; | 118 | trxid = result.ciwaru.reqnum[0]; |
119 | } | 119 | } |
120 | catch(err) { | 120 | catch(err) { |
121 | trxid = result.ciwaru.trxid[0]; | 121 | trxid = result.ciwaru.trxid[0]; |
122 | } | 122 | } |
123 | 123 | ||
124 | if (message.toUpperCase().indexOf('PENDING') >= 0) { | 124 | if (message.toUpperCase().indexOf('PENDING') >= 0) { |
125 | rc = '68'; | 125 | rc = '68'; |
126 | } | 126 | } |
127 | else if (hasSuccessKeywords(message)) { | 127 | else if (hasSuccessKeywords(message)) { |
128 | var sn = getSNFromMessage(result.ciwaru.msg); | 128 | var sn = getSNFromMessage(result.ciwaru.msg); |
129 | message = 'SN=' + sn + '; ' + message; | 129 | message = 'SN=' + sn + '; ' + message; |
130 | rc = '00'; | 130 | rc = '00'; |
131 | } else { | 131 | } else { |
132 | rc = supplierRcToST24Rc(result.ciwaru.rc[0]); | 132 | rc = supplierRcToST24Rc(result.ciwaru.rc[0]); |
133 | if (!rc) { | 133 | if (!rc) { |
134 | rc = '68'; | 134 | rc = '68'; |
135 | } | 135 | } |
136 | } | 136 | } |
137 | 137 | ||
138 | if ((task.retry == maxRetry) || (rc != '68')) { | 138 | if ((task.retry == maxRetry) || (rc != '68')) { |
139 | callbackReport(trxid, rc, message); | 139 | callbackReport(trxid, rc, message); |
140 | } else { | 140 | } else { |
141 | logger.info('Not reporting to AAA for duplicate 68', {task: task}); | 141 | logger.info('Not reporting to AAA for duplicate 68', {task: task}); |
142 | } | 142 | } |
143 | 143 | ||
144 | if (rc == '68') { | 144 | if (rc == '68') { |
145 | task.retry--; | 145 | task.retry--; |
146 | 146 | ||
147 | if (task.retry) { | 147 | if (task.retry) { |
148 | logger.info('Got pending status, retrying in ' + sleepBeforeRetry + 's'); | 148 | logger.info('Got pending status, retrying in ' + sleepBeforeRetry + 's'); |
149 | setTimeout(topupRequest, sleepBeforeRetry * 1000, task, task.retry); | 149 | setTimeout(topupRequest, sleepBeforeRetry * 1000, task, task.retry); |
150 | } | 150 | } |
151 | else { | 151 | else { |
152 | logger.warn('Maximum retry for pending status exceeded', {task: task}); | 152 | logger.warn('Maximum retry for pending status exceeded', {task: task}); |
153 | } | 153 | } |
154 | } | 154 | } |
155 | }); | 155 | }); |
156 | } | 156 | } |
157 | 157 | ||
158 | function topupRequest(task, retry) { | 158 | function topupRequest(task, retry) { |
159 | 159 | ||
160 | if (retry === undefined) { | 160 | if (retry === undefined) { |
161 | task.retry = maxRetry; | 161 | task.retry = maxRetry; |
162 | } | 162 | } |
163 | 163 | ||
164 | var remoteProduct = task.remoteProduct.split(','); | 164 | var remoteProduct = task.remoteProduct.split(','); |
165 | 165 | ||
166 | var params = { | 166 | var params = { |
167 | trxtype: '01', | 167 | trxtype: '01', |
168 | prdcode: remoteProduct[0], | 168 | prdcode: remoteProduct[0], |
169 | value: remoteProduct[1], | 169 | value: remoteProduct[1], |
170 | msisdn: task.destination, | 170 | msisdn: task.destination, |
171 | trxid: task.requestId, | 171 | trxid: task.requestId, |
172 | uid: config.h2h_out.userid, | 172 | uid: config.h2h_out.userid, |
173 | password: config.h2h_out.password, | 173 | password: config.h2h_out.password, |
174 | }; | 174 | }; |
175 | 175 | ||
176 | var postBody = createXmlPayload(params); | 176 | var postBody = createXmlPayload(params); |
177 | 177 | ||
178 | var partnerUrl = url.parse(config.h2h_out.partner); | 178 | var partnerUrl = url.parse(config.h2h_out.partner); |
179 | var postRequest = { | 179 | var postRequest = { |
180 | host: partnerUrl.hostname, | 180 | host: partnerUrl.hostname, |
181 | path: partnerUrl.path, | 181 | path: partnerUrl.path, |
182 | port: partnerUrl.port, | 182 | port: partnerUrl.port, |
183 | method: "POST", | 183 | method: "POST", |
184 | headers: { | 184 | headers: { |
185 | 'Content-Type': 'text/xml', | 185 | 'Content-Type': 'text/xml', |
186 | 'Content-Length': Buffer.byteLength(postBody) | 186 | 'Content-Length': Buffer.byteLength(postBody) |
187 | } | 187 | } |
188 | }; | 188 | }; |
189 | 189 | ||
190 | logger.info('POST to partner', {postRequest: postRequest}); | 190 | logger.info('POST to partner', {postRequest: postRequest}); |
191 | var req = http.request(postRequest, function( res ) { | 191 | var req = http.request(postRequest, function( res ) { |
192 | 192 | ||
193 | logger.info('Status code: ' + res.statusCode ); | 193 | logger.info('Status code: ' + res.statusCode ); |
194 | var buffer = ""; | 194 | var buffer = ""; |
195 | res.on( "data", function( data ) { buffer = buffer + data; } ); | 195 | res.on( "data", function( data ) { buffer = buffer + data; } ); |
196 | res.on( "end", function( data ) { | 196 | res.on( "end", function( data ) { |
197 | topupResponseHandler(buffer, task); | 197 | topupResponseHandler(buffer, task); |
198 | }); | 198 | }); |
199 | }); | 199 | }); |
200 | 200 | ||
201 | req.on('error', function(e) { | 201 | req.on('error', function(e) { |
202 | logger.warn('problem with request: ' + e.message); | 202 | logger.warn('problem with request: ' + e.message); |
203 | callbackReport(task['requestId'], '40', e.message); | 203 | callbackReport(task['requestId'], '40', e.message); |
204 | }); | 204 | }); |
205 | 205 | ||
206 | req.write(postBody); | 206 | req.write(postBody); |
207 | req.end(); | 207 | req.end(); |
208 | } | 208 | } |
209 | 209 | ||
210 | exports.start = start; | 210 | exports.start = start; |
211 | exports.topupRequest = topupRequest; | 211 | exports.topupRequest = topupRequest; |
212 | exports.calculateSignature = calculateSignature; | 212 | exports.calculateSignature = calculateSignature; |
213 | exports.createXmlPayload = createXmlPayload; | 213 | exports.createXmlPayload = createXmlPayload; |