Commit 5d3ed0dc3d2a54cadfcd7b34d76d1e4d4580c8cd
1 parent
d00faff772
Exists in
master
updateBalance
Showing 1 changed file with 1 additions and 0 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.verbose('Requesting to partner', {requestOption: options}); | 55 | logger.verbose('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 | return; | 60 | return; |
61 | } | 61 | } |
62 | 62 | ||
63 | if (response.statusCode != 200) { | 63 | if (response.statusCode != 200) { |
64 | var message = 'Partner response with http status code other that 200 (' + response.statusCode +')'; | 64 | var message = 'Partner response with http status code other that 200 (' + response.statusCode +')'; |
65 | 65 | ||
66 | logger.warn(message); | 66 | logger.warn(message); |
67 | callbackReport(task.requestId, '68', message); | 67 | callbackReport(task.requestId, '68', message); |
68 | return; | 68 | return; |
69 | } | 69 | } |
70 | 70 | ||
71 | /* | 71 | /* |
72 | logger.verbose('Got response', {requestId: task.requestId, responseBody: body}); | 72 | logger.verbose('Got response', {requestId: task.requestId, responseBody: body}); |
73 | callbackReport(task.requestId, '68', body); | 73 | callbackReport(task.requestId, '68', body); |
74 | */ | 74 | */ |
75 | 75 | ||
76 | logger.verbose('Got respose', {rawBody: body}); | 76 | logger.verbose('Got respose', {rawBody: body}); |
77 | parseResponse(body, task); | 77 | parseResponse(body, task); |
78 | }); | 78 | }); |
79 | } | 79 | } |
80 | 80 | ||
81 | function getSn(response) { | 81 | function getSn(response) { |
82 | try { | 82 | try { |
83 | var sn = response.xml.ket1[0]; | 83 | var sn = response.xml.ket1[0]; |
84 | return sn; | 84 | return sn; |
85 | } | 85 | } |
86 | catch (err) { | 86 | catch (err) { |
87 | return; | 87 | return; |
88 | } | 88 | } |
89 | } | 89 | } |
90 | 90 | ||
91 | function parseResponse(body, task) { | 91 | function parseResponse(body, task) { |
92 | xml2jsParser(body, function(err, response) { | 92 | xml2jsParser(body, function(err, response) { |
93 | if (err) { | 93 | if (err) { |
94 | logger.warn('Error parsing XML', {error: err, task: task, responseBody: body}); | 94 | logger.warn('Error parsing XML', {error: err, task: task, responseBody: body}); |
95 | 95 | ||
96 | var message = 'Error parsing XML. ' + err + '. ' + body; | 96 | var message = 'Error parsing XML. ' + err + '. ' + body; |
97 | 97 | ||
98 | var _response = { | 98 | var _response = { |
99 | raw: body, | 99 | raw: body, |
100 | parsed: { | 100 | parsed: { |
101 | MESSAGE: message | 101 | MESSAGE: message |
102 | } | 102 | } |
103 | } | 103 | } |
104 | aaa.pushResponseToMongoDb(task, _response, '68'); | 104 | aaa.pushResponseToMongoDb(task, _response, '68'); |
105 | 105 | ||
106 | callbackReport(task.requestId, '68', message); | 106 | callbackReport(task.requestId, '68', message); |
107 | return; | 107 | return; |
108 | } | 108 | } |
109 | 109 | ||
110 | logger.info('Got response', {response: response}); | 110 | logger.info('Got response', {response: response}); |
111 | 111 | ||
112 | var responseCode; | 112 | var responseCode; |
113 | var message; | 113 | var message; |
114 | var saldo; | 114 | var saldo; |
115 | 115 | ||
116 | try { | 116 | try { |
117 | responseCode = response.xml.response_code[0]; | 117 | responseCode = response.xml.response_code[0]; |
118 | saldo = response.xml.saldo[0]; | 118 | saldo = response.xml.saldo[0]; |
119 | message = response.xml.response_message[0]; | 119 | message = response.xml.response_message[0]; |
120 | } | 120 | } |
121 | catch(errGetParam) { | 121 | catch(errGetParam) { |
122 | logger.warn('Exception saat parsing hasil', {error: errGetParam, task: task, responseBody: body}); | 122 | logger.warn('Exception saat parsing hasil', {error: errGetParam, task: task, responseBody: body}); |
123 | 123 | ||
124 | var _response = { | 124 | var _response = { |
125 | raw: body, | 125 | raw: body, |
126 | parsed: { | 126 | parsed: { |
127 | MESSAGE: 'Exception saat parsing hasil. ' + errGetParam, | 127 | MESSAGE: 'Exception saat parsing hasil. ' + errGetParam, |
128 | xml: response.xml | 128 | xml: response.xml |
129 | } | 129 | } |
130 | } | 130 | } |
131 | aaa.pushResponseToMongoDb(task, _response, '68'); | 131 | aaa.pushResponseToMongoDb(task, _response, '68'); |
132 | 132 | ||
133 | callbackReport(task.requestId, '68', 'Exception saat parsing hasil. ' + errGetParam); | 133 | callbackReport(task.requestId, '68', 'Exception saat parsing hasil. ' + errGetParam); |
134 | return; | 134 | return; |
135 | } | 135 | } |
136 | 136 | ||
137 | var st24rc; | 137 | var st24rc; |
138 | if (parseInt(responseCode) == 0) { | 138 | if (parseInt(responseCode) == 0) { |
139 | st24rc = '00'; | 139 | st24rc = '00'; |
140 | } | 140 | } |
141 | else if (parseInt(responseCode) == '99') { | 141 | else if (parseInt(responseCode) == '99') { |
142 | st24rc = '68' | 142 | st24rc = '68' |
143 | } | 143 | } |
144 | else { | 144 | else { |
145 | st24rc = '40'; | 145 | st24rc = '40'; |
146 | } | 146 | } |
147 | 147 | ||
148 | var st24message = message; | 148 | var st24message = message; |
149 | if (responseCode) { | 149 | if (responseCode) { |
150 | st24message = responseCode + ' ' + st24message; | 150 | st24message = responseCode + ' ' + st24message; |
151 | } | 151 | } |
152 | 152 | ||
153 | if (st24rc == '00') { | 153 | if (st24rc == '00') { |
154 | var sn = getSn(response); | 154 | var sn = getSn(response); |
155 | 155 | ||
156 | if (sn) { | 156 | if (sn) { |
157 | st24message = 'SN=' + sn + ';' + st24message + '. SN=' + sn; | 157 | st24message = 'SN=' + sn + ';' + st24message + '. SN=' + sn; |
158 | } | 158 | } |
159 | } | 159 | } |
160 | 160 | ||
161 | if (saldo) { | 161 | if (saldo) { |
162 | st24message = st24message + '. Saldo ' + saldo; | 162 | st24message = st24message + '. Saldo ' + saldo; |
163 | aaa.updateBalance(saldo); | ||
163 | } | 164 | } |
164 | 165 | ||
165 | var _response = { | 166 | var _response = { |
166 | raw: body, | 167 | raw: body, |
167 | parsed: { | 168 | parsed: { |
168 | MESSAGE: st24message, | 169 | MESSAGE: st24message, |
169 | xml: response.xml | 170 | xml: response.xml |
170 | } | 171 | } |
171 | } | 172 | } |
172 | 173 | ||
173 | aaa.pushResponseToMongoDb(task, _response, st24rc); | 174 | aaa.pushResponseToMongoDb(task, _response, st24rc); |
174 | 175 | ||
175 | callbackReport(task.requestId, st24rc, st24message); | 176 | callbackReport(task.requestId, st24rc, st24message); |
176 | 177 | ||
177 | 178 | ||
178 | 179 | ||
179 | }); | 180 | }); |
180 | } | 181 | } |
181 | 182 | ||
182 | function prepareRemoteProductCode(remoteProduct) { | 183 | function prepareRemoteProductCode(remoteProduct) { |
183 | var product = remoteProduct.split(','); | 184 | var product = remoteProduct.split(','); |
184 | 185 | ||
185 | if (product.length != 3) { | 186 | if (product.length != 3) { |
186 | return; | 187 | return; |
187 | } | 188 | } |
188 | 189 | ||
189 | return { | 190 | return { |
190 | product: product[0], | 191 | product: product[0], |
191 | productDetail: product[1], | 192 | productDetail: product[1], |
192 | nominal: product[2] | 193 | nominal: product[2] |
193 | } | 194 | } |
194 | } | 195 | } |
195 | 196 | ||
196 | exports.start = start; | 197 | exports.start = start; |
197 | exports.topupRequest = topupRequest; | 198 | exports.topupRequest = topupRequest; |
198 | exports.prepareRemoteProductCode = prepareRemoteProductCode; | 199 | exports.prepareRemoteProductCode = prepareRemoteProductCode; |
199 | 200 |