Commit 3f0ff9875de535f30a3f513369d37724d2cddda9

Authored by Adhidarma Hadiwinoto
1 parent 22e8136644
Exists in master

getHarga dan getSaldo

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