Commit db8c7adc333a977f01c23e5cdadd1ba0958d113d
1 parent
48979b2ba8
Exists in
master
rc 0010 dan 0021
Showing 1 changed file with 6 additions and 0 deletions Inline Diff
partner-bnisp.js
1 | "use strict"; | 1 | "use strict"; |
2 | 2 | ||
3 | const http = require('http'); | 3 | const http = require('http'); |
4 | http.globalAgent.maxSockets = Infinity; | 4 | http.globalAgent.maxSockets = Infinity; |
5 | 5 | ||
6 | const request = require('request'); | 6 | const request = require('request'); |
7 | 7 | ||
8 | var config; | 8 | var config; |
9 | var aaa; | 9 | var aaa; |
10 | var logger; | 10 | var logger; |
11 | 11 | ||
12 | function start(options) { | 12 | function start(options) { |
13 | if (!options) { | 13 | if (!options) { |
14 | console.log('Undefined options, terminating....'); | 14 | console.log('Undefined options, terminating....'); |
15 | process.exit(1); | 15 | process.exit(1); |
16 | } | 16 | } |
17 | 17 | ||
18 | if (options.config) { | 18 | if (options.config) { |
19 | config = options.config; | 19 | config = options.config; |
20 | } else { | 20 | } else { |
21 | console.log('Undefined options.config, terminating....') | 21 | console.log('Undefined options.config, terminating....') |
22 | process.exit(1); | 22 | process.exit(1); |
23 | } | 23 | } |
24 | 24 | ||
25 | if (options.aaa) { | 25 | if (options.aaa) { |
26 | aaa = options.aaa; | 26 | aaa = options.aaa; |
27 | } else { | 27 | } else { |
28 | console.log('Undefined options.aaa, terminating....') | 28 | console.log('Undefined options.aaa, terminating....') |
29 | process.exit(1); | 29 | process.exit(1); |
30 | } | 30 | } |
31 | 31 | ||
32 | if (options && options.logger) { | 32 | if (options && options.logger) { |
33 | logger = options.logger; | 33 | logger = options.logger; |
34 | } else { | 34 | } else { |
35 | console.log('Undefined options.logger, terminating....') | 35 | console.log('Undefined options.logger, terminating....') |
36 | process.exit(1); | 36 | process.exit(1); |
37 | } | 37 | } |
38 | } | 38 | } |
39 | 39 | ||
40 | function callbackReport(requestId, rc, message, options) { | 40 | function callbackReport(requestId, rc, message, options) { |
41 | aaa.callbackReportWithPushToMongoDb(requestId, rc, message); | 41 | aaa.callbackReportWithPushToMongoDb(requestId, rc, message); |
42 | } | 42 | } |
43 | 43 | ||
44 | function topupRequest(task) { | 44 | function topupRequest(task) { |
45 | aaa.insertTaskToMongoDb(task); | 45 | aaa.insertTaskToMongoDb(task); |
46 | _hitTopup(task); | 46 | _hitTopup(task); |
47 | } | 47 | } |
48 | 48 | ||
49 | function _hitTopup(task, isCheckStatus) { | 49 | function _hitTopup(task, isCheckStatus) { |
50 | 50 | ||
51 | const remoteProduct = task.remoteProduct.split('/'); | 51 | const remoteProduct = task.remoteProduct.split('/'); |
52 | if (remoteProduct.length < 4) { | 52 | if (remoteProduct.length < 4) { |
53 | callbackReport(task.requestId, '40', 'INTERNAL_MSG: Invalid remoteProduct', task) | 53 | callbackReport(task.requestId, '40', 'INTERNAL_MSG: Invalid remoteProduct', task) |
54 | return; | 54 | return; |
55 | } | 55 | } |
56 | 56 | ||
57 | let pathParams = { | 57 | let pathParams = { |
58 | request_type: "purchase", | 58 | request_type: "purchase", |
59 | noid: config.h2h_out.noid, | 59 | noid: config.h2h_out.noid, |
60 | token: config.h2h_out.token, | 60 | token: config.h2h_out.token, |
61 | product: remoteProduct[0], | 61 | product: remoteProduct[0], |
62 | product_type: remoteProduct[1], | 62 | product_type: remoteProduct[1], |
63 | id_pel: task.destination, | 63 | id_pel: task.destination, |
64 | nominal: remoteProduct[2], | 64 | nominal: remoteProduct[2], |
65 | admin: remoteProduct[3], | 65 | admin: remoteProduct[3], |
66 | trace_id: task.requestId | 66 | trace_id: task.requestId |
67 | } | 67 | } |
68 | 68 | ||
69 | const requestOptions = { | 69 | const requestOptions = { |
70 | url: config.h2h_out.partner.replace(/\/+$/, '') + '/' + createUrlPath(pathParams).replace(/^\/+/, '') | 70 | url: config.h2h_out.partner.replace(/\/+$/, '') + '/' + createUrlPath(pathParams).replace(/^\/+/, '') |
71 | } | 71 | } |
72 | 72 | ||
73 | logger.verbose('Requeting to partner', {requestOptions: requestOptions}); | 73 | logger.verbose('Requeting to partner', {requestOptions: requestOptions}); |
74 | request(requestOptions, function(error, response, body) { | 74 | request(requestOptions, function(error, response, body) { |
75 | if (error) { | 75 | if (error) { |
76 | let rc = '68'; | 76 | let rc = '68'; |
77 | 77 | ||
78 | if (!isCheckStatus && (error.syscall == 'connect')) { | 78 | if (!isCheckStatus && (error.syscall == 'connect')) { |
79 | rc = '91'; | 79 | rc = '91'; |
80 | } | 80 | } |
81 | 81 | ||
82 | logger.warn('Error requesting to partner', {task: task, rc: rc, error: error}); | 82 | logger.warn('Error requesting to partner', {task: task, rc: rc, error: error}); |
83 | callbackReport(task.requestId, rc, 'INTERNAL_MSG: Error requesting to partner. ' + error, {task: task}); | 83 | callbackReport(task.requestId, rc, 'INTERNAL_MSG: Error requesting to partner. ' + error, {task: task}); |
84 | return; | 84 | return; |
85 | } | 85 | } |
86 | 86 | ||
87 | if (response.statusCode != 200) { | 87 | if (response.statusCode != 200) { |
88 | let rc = '68'; | 88 | let rc = '68'; |
89 | 89 | ||
90 | logger.warn('HTTP status code is not 200', {task: task, http_status_code: response.statusCode}); | 90 | logger.warn('HTTP status code is not 200', {task: task, http_status_code: response.statusCode}); |
91 | callbackReport(task.requestId, rc, 'INTERNAL_MSG: HTTP status code ' + response.statusCode, {task: task}); | 91 | callbackReport(task.requestId, rc, 'INTERNAL_MSG: HTTP status code ' + response.statusCode, {task: task}); |
92 | return; | 92 | return; |
93 | } | 93 | } |
94 | 94 | ||
95 | if (!body) { | 95 | if (!body) { |
96 | let rc = '68'; | 96 | let rc = '68'; |
97 | 97 | ||
98 | logger.warn('Error processing response body', {task: task, responseBody: body}); | 98 | logger.warn('Error processing response body', {task: task, responseBody: body}); |
99 | callbackReport(task.requestId, rc, 'INTERNAL_MSG: Error processing response body', {task: task}); | 99 | callbackReport(task.requestId, rc, 'INTERNAL_MSG: Error processing response body', {task: task}); |
100 | return; | 100 | return; |
101 | } | 101 | } |
102 | 102 | ||
103 | if (body.trim() == 'invalid specs') { | 103 | if (body.trim() == 'invalid specs') { |
104 | let rc = '40'; | 104 | let rc = '40'; |
105 | 105 | ||
106 | logger.warn('Invalid specs', {task: task, responseBody: body}); | 106 | logger.warn('Invalid specs', {task: task, responseBody: body}); |
107 | callbackReport(task.requestId, rc, body); | 107 | callbackReport(task.requestId, rc, body); |
108 | return; | 108 | return; |
109 | } | 109 | } |
110 | 110 | ||
111 | logger.verbose('Got response from partner', {task: task, responseBody: body}); | 111 | logger.verbose('Got response from partner', {task: task, responseBody: body}); |
112 | 112 | ||
113 | const responseData = parseResponseBody(body); | 113 | const responseData = parseResponseBody(body); |
114 | logger.verbose('Response body parsed as json value', {responseData: responseData}); | 114 | logger.verbose('Response body parsed as json value', {responseData: responseData}); |
115 | const data = responseDataProcessor(responseData); | 115 | const data = responseDataProcessor(responseData); |
116 | 116 | ||
117 | if (data.balance && aaa.updateBalance) { | 117 | if (data.balance && aaa.updateBalance) { |
118 | aaa.updateBalance(data.balance); | 118 | aaa.updateBalance(data.balance); |
119 | } | 119 | } |
120 | 120 | ||
121 | callbackReport(task.requestId, data.rc, data.combinedMessage, {task: task}); | 121 | callbackReport(task.requestId, data.rc, data.combinedMessage, {task: task}); |
122 | }) | 122 | }) |
123 | 123 | ||
124 | } | 124 | } |
125 | 125 | ||
126 | function createUrlPath(options) { | 126 | function createUrlPath(options) { |
127 | let urlPath = [ | 127 | let urlPath = [ |
128 | "get", | 128 | "get", |
129 | options.request_type || "purchase", | 129 | options.request_type || "purchase", |
130 | "json", | 130 | "json", |
131 | options.noid, | 131 | options.noid, |
132 | options.token, | 132 | options.token, |
133 | options.product, | 133 | options.product, |
134 | options.product_type, | 134 | options.product_type, |
135 | options.id_pel, | 135 | options.id_pel, |
136 | options.nominal || 0, | 136 | options.nominal || 0, |
137 | options.admin || 0, | 137 | options.admin || 0, |
138 | options.trace_id | 138 | options.trace_id |
139 | ].join('/'); | 139 | ].join('/'); |
140 | 140 | ||
141 | return '/' + urlPath; | 141 | return '/' + urlPath; |
142 | } | 142 | } |
143 | 143 | ||
144 | function parseResponseBody(body) { | 144 | function parseResponseBody(body) { |
145 | let data; | 145 | let data; |
146 | 146 | ||
147 | try { | 147 | try { |
148 | data = JSON.parse(body); | 148 | data = JSON.parse(body); |
149 | } | 149 | } |
150 | catch(e) { | 150 | catch(e) { |
151 | if (logger) { | 151 | if (logger) { |
152 | logger.warn('Exception on parsing result body: ' + e); | 152 | logger.warn('Exception on parsing result body: ' + e); |
153 | return; | 153 | return; |
154 | } | 154 | } |
155 | } | 155 | } |
156 | 156 | ||
157 | return data; | 157 | return data; |
158 | } | 158 | } |
159 | 159 | ||
160 | function responseDataProcessor(responseData) { | 160 | function responseDataProcessor(responseData) { |
161 | let retval = { | 161 | let retval = { |
162 | rc: '68', | 162 | rc: '68', |
163 | sn: '', | 163 | sn: '', |
164 | responseMessage: '', | 164 | responseMessage: '', |
165 | combinedMessage: '', | 165 | combinedMessage: '', |
166 | amount: 0, | 166 | amount: 0, |
167 | balance: 0, | 167 | balance: 0, |
168 | saldo: 0, | 168 | saldo: 0, |
169 | ts: '', | 169 | ts: '', |
170 | loadTime: 0, | 170 | loadTime: 0, |
171 | product: '', | 171 | product: '', |
172 | productType: '' | 172 | productType: '' |
173 | } | 173 | } |
174 | 174 | ||
175 | let combinedMessage = []; | 175 | let combinedMessage = []; |
176 | 176 | ||
177 | if (responseData.response_code == '0000') { | 177 | if (responseData.response_code == '0000') { |
178 | retval.rc = '00'; | 178 | retval.rc = '00'; |
179 | } | 179 | } |
180 | else if (responseData.response_code == '0010') { | ||
181 | retval.rc = '14'; | ||
182 | } | ||
183 | else if (responseData.response_code == '0021') { | ||
184 | retval.rc = '14'; | ||
185 | } | ||
180 | else if (['9990','0099','0068','0063','0005','9997','9999','9998','9990','0600','1003'].indexOf(responseData.response_code) >= 0) { | 186 | else if (['9990','0099','0068','0063','0005','9997','9999','9998','9990','0600','1003'].indexOf(responseData.response_code) >= 0) { |
181 | retval.rc = '68'; | 187 | retval.rc = '68'; |
182 | } | 188 | } |
183 | else { | 189 | else { |
184 | retval.rc = '40'; | 190 | retval.rc = '40'; |
185 | } | 191 | } |
186 | 192 | ||
187 | if (responseData.response_code) { | 193 | if (responseData.response_code) { |
188 | combinedMessage.push(responseData.response_code); | 194 | combinedMessage.push(responseData.response_code); |
189 | } | 195 | } |
190 | 196 | ||
191 | if (responseData.product) { | 197 | if (responseData.product) { |
192 | combinedMessage.push(responseData.product) | 198 | combinedMessage.push(responseData.product) |
193 | } | 199 | } |
194 | 200 | ||
195 | if (responseData.produk_tipe) { | 201 | if (responseData.produk_tipe) { |
196 | combinedMessage.push(responseData.produk_tipe) | 202 | combinedMessage.push(responseData.produk_tipe) |
197 | } | 203 | } |
198 | 204 | ||
199 | if (responseData.idpel) { | 205 | if (responseData.idpel) { |
200 | combinedMessage.push(responseData.idpel) | 206 | combinedMessage.push(responseData.idpel) |
201 | } | 207 | } |
202 | 208 | ||
203 | retval.responseMessage = responseData.response_message || ''; | 209 | retval.responseMessage = responseData.response_message || ''; |
204 | if (retval.responseMessage) { | 210 | if (retval.responseMessage) { |
205 | combinedMessage.push(retval.responseMessage); | 211 | combinedMessage.push(retval.responseMessage); |
206 | } | 212 | } |
207 | 213 | ||
208 | if (responseData.detail) { | 214 | if (responseData.detail) { |
209 | retval.sn = responseData.detail.voucherSerialNumber || ''; | 215 | retval.sn = responseData.detail.voucherSerialNumber || ''; |
210 | } | 216 | } |
211 | 217 | ||
212 | retval.amount = responseData.amount || 0; | 218 | retval.amount = responseData.amount || 0; |
213 | combinedMessage.push('amount: ' + retval.amount); | 219 | combinedMessage.push('amount: ' + retval.amount); |
214 | 220 | ||
215 | retval.balance = responseData.saldo || 0; | 221 | retval.balance = responseData.saldo || 0; |
216 | retval.saldo = retval.balance; | 222 | retval.saldo = retval.balance; |
217 | combinedMessage.push('balance: ' + retval.balance); | 223 | combinedMessage.push('balance: ' + retval.balance); |
218 | 224 | ||
219 | retval.ts = responseData.waktu || ''; | 225 | retval.ts = responseData.waktu || ''; |
220 | combinedMessage.push(retval.ts); | 226 | combinedMessage.push(retval.ts); |
221 | 227 | ||
222 | retval.loadTime = responseData.loadTime || ''; | 228 | retval.loadTime = responseData.loadTime || ''; |
223 | combinedMessage.push('load time: ' + retval.loadTime); | 229 | combinedMessage.push('load time: ' + retval.loadTime); |
224 | 230 | ||
225 | retval.combinedMessage = combinedMessage.join(' '); | 231 | retval.combinedMessage = combinedMessage.join(' '); |
226 | 232 | ||
227 | if (retval.sn) { | 233 | if (retval.sn) { |
228 | retval.combinedMessage = 'SN=' + retval.sn + '; ' + retval.combinedMessage; | 234 | retval.combinedMessage = 'SN=' + retval.sn + '; ' + retval.combinedMessage; |
229 | } | 235 | } |
230 | 236 | ||
231 | return retval; | 237 | return retval; |
232 | } | 238 | } |
233 | 239 | ||
234 | exports.start = start; | 240 | exports.start = start; |
235 | exports.createUrlPath = createUrlPath; | 241 | exports.createUrlPath = createUrlPath; |
236 | exports.parseResponseBody = parseResponseBody; | 242 | exports.parseResponseBody = parseResponseBody; |
237 | exports.responseDataProcessor = responseDataProcessor; | 243 | exports.responseDataProcessor = responseDataProcessor; |
238 | exports.topupRequest = topupRequest; | 244 | exports.topupRequest = topupRequest; |
239 | 245 |