Commit ac8c1ea6eb02618184ea92b665e1756a5ee5b677

Authored by Adhidarma Hadiwinoto
1 parent 4ab069319c
Exists in master

hapus excess empty lines

Showing 1 changed file with 0 additions and 3 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 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 getHarga(response) { 91 function getHarga(response) {
92 try { 92 try {
93 var harga = response.xml.amount[0]; 93 var harga = response.xml.amount[0];
94 return harga; 94 return harga;
95 } 95 }
96 catch (err) { 96 catch (err) {
97 return; 97 return;
98 } 98 }
99 } 99 }
100 100
101 function getSaldo(response) { 101 function getSaldo(response) {
102 try { 102 try {
103 var saldo = response.xml.saldo[0]; 103 var saldo = response.xml.saldo[0];
104 return saldo; 104 return saldo;
105 } 105 }
106 catch (err) { 106 catch (err) {
107 return; 107 return;
108 } 108 }
109 } 109 }
110 110
111 function parseResponse(body, task) { 111 function parseResponse(body, task) {
112 xml2jsParser(body, function(err, response) { 112 xml2jsParser(body, function(err, response) {
113 if (err) { 113 if (err) {
114 logger.warn('Error parsing XML', {error: err, task: task, responseBody: body}); 114 logger.warn('Error parsing XML', {error: err, task: task, responseBody: body});
115 115
116 var message = 'Error parsing XML. ' + err + '. ' + body; 116 var message = 'Error parsing XML. ' + err + '. ' + body;
117 117
118 var _response = { 118 var _response = {
119 raw: body, 119 raw: body,
120 parsed: { 120 parsed: {
121 MESSAGE: message 121 MESSAGE: message
122 } 122 }
123 } 123 }
124 aaa.pushResponseToMongoDb(task, _response, '68'); 124 aaa.pushResponseToMongoDb(task, _response, '68');
125 125
126 callbackReport(task.requestId, '68', message); 126 callbackReport(task.requestId, '68', message);
127 return; 127 return;
128 } 128 }
129 129
130 logger.info('Got response', {response: response}); 130 logger.info('Got response', {response: response});
131 131
132 var responseCode; 132 var responseCode;
133 var message; 133 var message;
134 134
135 try { 135 try {
136 responseCode = response.xml.response_code[0]; 136 responseCode = response.xml.response_code[0];
137 message = response.xml.response_message[0]; 137 message = response.xml.response_message[0];
138 } 138 }
139 catch(errGetParam) { 139 catch(errGetParam) {
140 logger.warn('Exception saat parsing hasil', {error: errGetParam, task: task, responseBody: body}); 140 logger.warn('Exception saat parsing hasil', {error: errGetParam, task: task, responseBody: body});
141 141
142 var _response = { 142 var _response = {
143 raw: body, 143 raw: body,
144 parsed: { 144 parsed: {
145 MESSAGE: 'Exception saat parsing hasil. ' + errGetParam, 145 MESSAGE: 'Exception saat parsing hasil. ' + errGetParam,
146 xml: response.xml 146 xml: response.xml
147 } 147 }
148 } 148 }
149 aaa.pushResponseToMongoDb(task, _response, '68'); 149 aaa.pushResponseToMongoDb(task, _response, '68');
150 150
151 callbackReport(task.requestId, '68', 'Exception saat parsing hasil. ' + errGetParam); 151 callbackReport(task.requestId, '68', 'Exception saat parsing hasil. ' + errGetParam);
152 return; 152 return;
153 } 153 }
154 154
155 var st24rc; 155 var st24rc;
156 if (parseInt(responseCode) == 0) { 156 if (parseInt(responseCode) == 0) {
157 st24rc = '00'; 157 st24rc = '00';
158 } 158 }
159 else if (parseInt(responseCode) == '99') { 159 else if (parseInt(responseCode) == '99') {
160 st24rc = '68' 160 st24rc = '68'
161 } 161 }
162 else { 162 else {
163 st24rc = '40'; 163 st24rc = '40';
164 } 164 }
165 165
166 var st24message = message; 166 var st24message = message;
167 if (responseCode) { 167 if (responseCode) {
168 st24message = responseCode + ' ' + st24message; 168 st24message = responseCode + ' ' + st24message;
169 } 169 }
170 170
171 if (st24rc == '00') { 171 if (st24rc == '00') {
172 var sn = getSn(response); 172 var sn = getSn(response);
173 173
174 if (sn) { 174 if (sn) {
175 st24message = 'SN=' + sn + ';' + st24message + '. SN=' + sn; 175 st24message = 'SN=' + sn + ';' + st24message + '. SN=' + sn;
176 } 176 }
177 } 177 }
178 178
179 var harga = getHarga(response); 179 var harga = getHarga(response);
180 if (harga) { 180 if (harga) {
181 st24message = st24message + '. Harga ' + harga; 181 st24message = st24message + '. Harga ' + harga;
182 } 182 }
183 183
184 var saldo = getSaldo(response); 184 var saldo = getSaldo(response);
185 if (saldo) { 185 if (saldo) {
186 st24message = st24message + '. Saldo ' + saldo; 186 st24message = st24message + '. Saldo ' + saldo;
187 aaa.updateBalance(saldo); 187 aaa.updateBalance(saldo);
188 } 188 }
189 189
190 var _response = { 190 var _response = {
191 raw: body, 191 raw: body,
192 parsed: { 192 parsed: {
193 MESSAGE: st24message, 193 MESSAGE: st24message,
194 xml: response.xml 194 xml: response.xml
195 } 195 }
196 } 196 }
197 197
198 aaa.pushResponseToMongoDb(task, _response, st24rc); 198 aaa.pushResponseToMongoDb(task, _response, st24rc);
199 199
200 callbackReport(task.requestId, st24rc, st24message); 200 callbackReport(task.requestId, st24rc, st24message);
201
202
203
204 }); 201 });
205 } 202 }
206 203
207 function prepareRemoteProductCode(remoteProduct) { 204 function prepareRemoteProductCode(remoteProduct) {
208 var product = remoteProduct.split(','); 205 var product = remoteProduct.split(',');
209 206
210 if (product.length != 3) { 207 if (product.length != 3) {
211 return; 208 return;
212 } 209 }
213 210
214 return { 211 return {
215 product: product[0], 212 product: product[0],
216 productDetail: product[1], 213 productDetail: product[1],
217 nominal: product[2] 214 nominal: product[2]
218 } 215 }
219 } 216 }
220 217
221 function paddingDestination(destination, width) { 218 function paddingDestination(destination, width) {
222 if (!width) { 219 if (!width) {
223 width = 13; 220 width = 13;
224 } 221 }
225 222
226 if (destination.length > width) { 223 if (destination.length > width) {
227 return destination; 224 return destination;
228 } 225 }
229 226
230 var padder = "000000000000000000000000000"; 227 var padder = "000000000000000000000000000";
231 228
232 return (padder + destination).slice(-1 * width); 229 return (padder + destination).slice(-1 * width);
233 } 230 }
234 231
235 exports.start = start; 232 exports.start = start;
236 exports.topupRequest = topupRequest; 233 exports.topupRequest = topupRequest;
237 exports.prepareRemoteProductCode = prepareRemoteProductCode; 234 exports.prepareRemoteProductCode = prepareRemoteProductCode;
238 exports.paddingDestination = paddingDestination; 235 exports.paddingDestination = paddingDestination;
239 236