Commit 6ec424b07d1838dea4779b2cdad58bcd470923a2
1 parent
ac8c1ea6eb
Exists in
master
pushResponseToDb jika ada kegagalan
Showing 1 changed file with 22 additions and 6 deletions Inline Diff
partner-bayarkilat.js
1 | var request = require('request'); | 1 | var request = require('request'); |
2 | var url = require('url'); | 2 | var url = require('url'); |
3 | var winston = require('winston'); | 3 | var winston = require('winston'); |
4 | var xml2jsParser = require('xml2js').parseString; | 4 | var xml2jsParser = require('xml2js').parseString; |
5 | 5 | ||
6 | var config; | 6 | var config; |
7 | var aaa; | 7 | var aaa; |
8 | var callbackReport; | 8 | var callbackReport; |
9 | var logger; | 9 | var logger; |
10 | 10 | ||
11 | process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; | 11 | process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; |
12 | 12 | ||
13 | function start(_config, _callbackReport, options) { | 13 | function start(_config, _callbackReport, options) { |
14 | config = _config; | 14 | config = _config; |
15 | callbackReport = _callbackReport; | 15 | callbackReport = _callbackReport; |
16 | 16 | ||
17 | if (options && options.aaa) { | 17 | if (options && options.aaa) { |
18 | aaa = options.aaa; | 18 | aaa = options.aaa; |
19 | } | 19 | } |
20 | 20 | ||
21 | if (options && options.logger) { | 21 | if (options && options.logger) { |
22 | logger = options.logger; | 22 | logger = options.logger; |
23 | } else { | 23 | } else { |
24 | logger = new winston.Logger({ | 24 | logger = new winston.Logger({ |
25 | transports: [ | 25 | transports: [ |
26 | new (winston.transports.Console)() | 26 | new (winston.transports.Console)() |
27 | ] | 27 | ] |
28 | }); | 28 | }); |
29 | } | 29 | } |
30 | } | 30 | } |
31 | 31 | ||
32 | 32 | ||
33 | function topupRequest(task, retry) { | 33 | function topupRequest(task, retry) { |
34 | aaa.insertTaskToMongoDb(task); | 34 | aaa.insertTaskToMongoDb(task); |
35 | 35 | ||
36 | var partnerUrl = url.parse(config.h2h_out.partner); | 36 | var partnerUrl = url.parse(config.h2h_out.partner); |
37 | var product = prepareRemoteProductCode(task.remoteProduct); | 37 | var product = prepareRemoteProductCode(task.remoteProduct); |
38 | 38 | ||
39 | var options = { | 39 | var options = { |
40 | url: config.h2h_out.partner, | 40 | url: config.h2h_out.partner, |
41 | qs: { | 41 | qs: { |
42 | request: 'PURCHASE*' | 42 | request: 'PURCHASE*' |
43 | + task.requestId + '*' | 43 | + task.requestId + '*' |
44 | + product.product + '*' | 44 | + product.product + '*' |
45 | + product.productDetail + '*' | 45 | + product.productDetail + '*' |
46 | + task.destination + '*' | 46 | + task.destination + '*' |
47 | + product.nominal + '*' | 47 | + product.nominal + '*' |
48 | + '0*' | 48 | + '0*' |
49 | + config.h2h_out.noid + '*' | 49 | + config.h2h_out.noid + '*' |
50 | + config.h2h_out.userid + '*' | 50 | + config.h2h_out.userid + '*' |
51 | + config.h2h_out.password | 51 | + config.h2h_out.password |
52 | } | 52 | } |
53 | }; | 53 | }; |
54 | 54 | ||
55 | logger.info('Requesting to partner', {requestOption: options}); | 55 | logger.info('Requesting to partner', {requestOption: options}); |
56 | request(options, function(error, response, body) { | 56 | request(options, function(error, response, body) { |
57 | if (error) { | 57 | if (error) { |
58 | logger.warn('Error requesting to partner', {error: error}); | 58 | logger.warn('Error requesting to partner', {error: error}); |
59 | callbackReport(task.requestId, '68', 'Error requesting to partner. ' + error); | 59 | callbackReport(task.requestId, '68', 'Error requesting to partner. ' + error); |
60 | |||
61 | var _response = { | ||
62 | raw: 'Error requesting to partner. ' + error, | ||
63 | parsed: { | ||
64 | MESSAGE: 'Error requesting to partner. ' + error, | ||
65 | error: error | ||
66 | } | ||
67 | } | ||
68 | aaa.pushResponseToMongoDb(task, _response, '68'); | ||
69 | |||
60 | return; | 70 | return; |
61 | } | 71 | } |
62 | 72 | ||
63 | if (response.statusCode != 200) { | 73 | if (response.statusCode != 200) { |
64 | var message = 'Partner response with http status code other that 200 (' + response.statusCode +')'; | 74 | var message = 'Partner response with http status code other that 200 (' + response.statusCode + ')'; |
65 | 75 | ||
66 | logger.warn(message); | 76 | logger.warn(message); |
67 | callbackReport(task.requestId, '68', message); | 77 | callbackReport(task.requestId, '68', message); |
78 | |||
79 | var _response = { | ||
80 | raw: 'Partner response with http status code other that 200 (' + response.statusCode + ')', | ||
81 | parsed: { | ||
82 | MESSAGE: 'Partner response with http status code other that 200 (' + response.statusCode + ')', | ||
83 | responseHttpStatus: response.statusCode, | ||
84 | responseBody: body, | ||
85 | } | ||
86 | } | ||
87 | aaa.pushResponseToMongoDb(task, _response, '68'); | ||
88 | |||
68 | return; | 89 | return; |
69 | } | 90 | } |
70 | 91 | ||
71 | /* | ||
72 | logger.verbose('Got response', {requestId: task.requestId, responseBody: body}); | ||
73 | callbackReport(task.requestId, '68', body); | ||
74 | */ | ||
75 | |||
76 | logger.verbose('Got respose', {rawBody: body}); | 92 | logger.verbose('Got respose', {rawBody: body}); |
77 | parseResponse(body, task); | 93 | parseResponse(body, task); |
78 | }); | 94 | }); |
79 | } | 95 | } |
80 | 96 | ||
81 | function getSn(response) { | 97 | function getSn(response) { |
82 | try { | 98 | try { |
83 | var sn = response.xml.ket1[0]; | 99 | var sn = response.xml.ket1[0]; |
84 | return sn; | 100 | return sn; |
85 | } | 101 | } |
86 | catch (err) { | 102 | catch (err) { |
87 | return; | 103 | return; |
88 | } | 104 | } |
89 | } | 105 | } |
90 | 106 | ||
91 | function getHarga(response) { | 107 | function getHarga(response) { |
92 | try { | 108 | try { |
93 | var harga = response.xml.amount[0]; | 109 | var harga = response.xml.amount[0]; |
94 | return harga; | 110 | return harga; |
95 | } | 111 | } |
96 | catch (err) { | 112 | catch (err) { |
97 | return; | 113 | return; |
98 | } | 114 | } |
99 | } | 115 | } |
100 | 116 | ||
101 | function getSaldo(response) { | 117 | function getSaldo(response) { |
102 | try { | 118 | try { |
103 | var saldo = response.xml.saldo[0]; | 119 | var saldo = response.xml.saldo[0]; |
104 | return saldo; | 120 | return saldo; |
105 | } | 121 | } |
106 | catch (err) { | 122 | catch (err) { |
107 | return; | 123 | return; |
108 | } | 124 | } |
109 | } | 125 | } |
110 | 126 | ||
111 | function parseResponse(body, task) { | 127 | function parseResponse(body, task) { |
112 | xml2jsParser(body, function(err, response) { | 128 | xml2jsParser(body, function(err, response) { |
113 | if (err) { | 129 | if (err) { |
114 | logger.warn('Error parsing XML', {error: err, task: task, responseBody: body}); | 130 | logger.warn('Error parsing XML', {error: err, task: task, responseBody: body}); |
115 | 131 | ||
116 | var message = 'Error parsing XML. ' + err + '. ' + body; | 132 | var message = 'Error parsing XML. ' + err + '. ' + body; |
117 | 133 | ||
118 | var _response = { | 134 | var _response = { |
119 | raw: body, | 135 | raw: body, |
120 | parsed: { | 136 | parsed: { |
121 | MESSAGE: message | 137 | MESSAGE: message |
122 | } | 138 | } |
123 | } | 139 | } |
124 | aaa.pushResponseToMongoDb(task, _response, '68'); | 140 | aaa.pushResponseToMongoDb(task, _response, '68'); |
125 | 141 | ||
126 | callbackReport(task.requestId, '68', message); | 142 | callbackReport(task.requestId, '68', message); |
127 | return; | 143 | return; |
128 | } | 144 | } |
129 | 145 | ||
130 | logger.info('Got response', {response: response}); | 146 | logger.info('Got response', {response: response}); |
131 | 147 | ||
132 | var responseCode; | 148 | var responseCode; |
133 | var message; | 149 | var message; |
134 | 150 | ||
135 | try { | 151 | try { |
136 | responseCode = response.xml.response_code[0]; | 152 | responseCode = response.xml.response_code[0]; |
137 | message = response.xml.response_message[0]; | 153 | message = response.xml.response_message[0]; |
138 | } | 154 | } |
139 | catch(errGetParam) { | 155 | catch(errGetParam) { |
140 | logger.warn('Exception saat parsing hasil', {error: errGetParam, task: task, responseBody: body}); | 156 | logger.warn('Exception saat parsing hasil', {error: errGetParam, task: task, responseBody: body}); |
141 | 157 | ||
142 | var _response = { | 158 | var _response = { |
143 | raw: body, | 159 | raw: body, |
144 | parsed: { | 160 | parsed: { |
145 | MESSAGE: 'Exception saat parsing hasil. ' + errGetParam, | 161 | MESSAGE: 'Exception saat parsing hasil. ' + errGetParam, |
146 | xml: response.xml | 162 | xml: response.xml |
147 | } | 163 | } |
148 | } | 164 | } |
149 | aaa.pushResponseToMongoDb(task, _response, '68'); | 165 | aaa.pushResponseToMongoDb(task, _response, '68'); |
150 | 166 | ||
151 | callbackReport(task.requestId, '68', 'Exception saat parsing hasil. ' + errGetParam); | 167 | callbackReport(task.requestId, '68', 'Exception saat parsing hasil. ' + errGetParam); |
152 | return; | 168 | return; |
153 | } | 169 | } |
154 | 170 | ||
155 | var st24rc; | 171 | var st24rc; |
156 | if (parseInt(responseCode) == 0) { | 172 | if (parseInt(responseCode) == 0) { |
157 | st24rc = '00'; | 173 | st24rc = '00'; |
158 | } | 174 | } |
159 | else if (parseInt(responseCode) == '99') { | 175 | else if (parseInt(responseCode) == '99') { |
160 | st24rc = '68' | 176 | st24rc = '68' |
161 | } | 177 | } |
162 | else { | 178 | else { |
163 | st24rc = '40'; | 179 | st24rc = '40'; |
164 | } | 180 | } |
165 | 181 | ||
166 | var st24message = message; | 182 | var st24message = message; |
167 | if (responseCode) { | 183 | if (responseCode) { |
168 | st24message = responseCode + ' ' + st24message; | 184 | st24message = responseCode + ' ' + st24message; |
169 | } | 185 | } |
170 | 186 | ||
171 | if (st24rc == '00') { | 187 | if (st24rc == '00') { |
172 | var sn = getSn(response); | 188 | var sn = getSn(response); |
173 | 189 | ||
174 | if (sn) { | 190 | if (sn) { |
175 | st24message = 'SN=' + sn + ';' + st24message + '. SN=' + sn; | 191 | st24message = 'SN=' + sn + ';' + st24message + '. SN=' + sn; |
176 | } | 192 | } |
177 | } | 193 | } |
178 | 194 | ||
179 | var harga = getHarga(response); | 195 | var harga = getHarga(response); |
180 | if (harga) { | 196 | if (harga) { |
181 | st24message = st24message + '. Harga ' + harga; | 197 | st24message = st24message + '. Harga ' + harga; |
182 | } | 198 | } |
183 | 199 | ||
184 | var saldo = getSaldo(response); | 200 | var saldo = getSaldo(response); |
185 | if (saldo) { | 201 | if (saldo) { |
186 | st24message = st24message + '. Saldo ' + saldo; | 202 | st24message = st24message + '. Saldo ' + saldo; |
187 | aaa.updateBalance(saldo); | 203 | aaa.updateBalance(saldo); |
188 | } | 204 | } |
189 | 205 | ||
190 | var _response = { | 206 | var _response = { |
191 | raw: body, | 207 | raw: body, |
192 | parsed: { | 208 | parsed: { |
193 | MESSAGE: st24message, | 209 | MESSAGE: st24message, |
194 | xml: response.xml | 210 | xml: response.xml |
195 | } | 211 | } |
196 | } | 212 | } |
197 | 213 | ||
198 | aaa.pushResponseToMongoDb(task, _response, st24rc); | 214 | aaa.pushResponseToMongoDb(task, _response, st24rc); |
199 | 215 | ||
200 | callbackReport(task.requestId, st24rc, st24message); | 216 | callbackReport(task.requestId, st24rc, st24message); |
201 | }); | 217 | }); |
202 | } | 218 | } |
203 | 219 | ||
204 | function prepareRemoteProductCode(remoteProduct) { | 220 | function prepareRemoteProductCode(remoteProduct) { |
205 | var product = remoteProduct.split(','); | 221 | var product = remoteProduct.split(','); |
206 | 222 | ||
207 | if (product.length != 3) { | 223 | if (product.length != 3) { |
208 | return; | 224 | return; |
209 | } | 225 | } |
210 | 226 | ||
211 | return { | 227 | return { |
212 | product: product[0], | 228 | product: product[0], |
213 | productDetail: product[1], | 229 | productDetail: product[1], |
214 | nominal: product[2] | 230 | nominal: product[2] |
215 | } | 231 | } |
216 | } | 232 | } |
217 | 233 | ||
218 | function paddingDestination(destination, width) { | 234 | function paddingDestination(destination, width) { |
219 | if (!width) { | 235 | if (!width) { |
220 | width = 13; | 236 | width = 13; |
221 | } | 237 | } |
222 | 238 | ||
223 | if (destination.length > width) { | 239 | if (destination.length > width) { |
224 | return destination; | 240 | return destination; |
225 | } | 241 | } |
226 | 242 | ||
227 | var padder = "000000000000000000000000000"; | 243 | var padder = "000000000000000000000000000"; |
228 | 244 | ||
229 | return (padder + destination).slice(-1 * width); | 245 | return (padder + destination).slice(-1 * width); |
230 | } | 246 | } |
231 | 247 |