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