Commit 233ce148e4dd08a22ab827f6e12f2872ca79570c
1 parent
da19a3d225
Exists in
master
perbaikan parsing produk
Showing 1 changed file with 4 additions and 4 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: task.remoteProduct[0], | 61 | product: remoteProduct[0], |
62 | product_type: task.remoteProduct[1], | 62 | product_type: remoteProduct[1], |
63 | id_pel: task.destination, | 63 | id_pel: task.destination, |
64 | nominal: task.remoteProduct[2], | 64 | nominal: remoteProduct[2], |
65 | admin: task.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 + createUrlPath(pathParams) | 70 | url: config.h2h_out.partner + createUrlPath(pathParams) |
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 | callbackReport(task.requestId, data.rc, data.combinedMessage, {task: task}); | 117 | callbackReport(task.requestId, data.rc, data.combinedMessage, {task: task}); |
118 | }) | 118 | }) |
119 | 119 | ||
120 | } | 120 | } |
121 | 121 | ||
122 | function createUrlPath(options) { | 122 | function createUrlPath(options) { |
123 | let urlPath = [ | 123 | let urlPath = [ |
124 | "get", | 124 | "get", |
125 | options.request_type || "purchase", | 125 | options.request_type || "purchase", |
126 | "json", | 126 | "json", |
127 | options.noid, | 127 | options.noid, |
128 | options.token, | 128 | options.token, |
129 | options.product, | 129 | options.product, |
130 | options.product_type, | 130 | options.product_type, |
131 | options.id_pel, | 131 | options.id_pel, |
132 | options.nominal || 0, | 132 | options.nominal || 0, |
133 | options.admin || 0, | 133 | options.admin || 0, |
134 | options.trace_id | 134 | options.trace_id |
135 | ].join('/'); | 135 | ].join('/'); |
136 | 136 | ||
137 | return '/' + urlPath; | 137 | return '/' + urlPath; |
138 | } | 138 | } |
139 | 139 | ||
140 | function parseResponseBody(body) { | 140 | function parseResponseBody(body) { |
141 | let data; | 141 | let data; |
142 | 142 | ||
143 | try { | 143 | try { |
144 | data = JSON.parse(body); | 144 | data = JSON.parse(body); |
145 | } | 145 | } |
146 | catch(e) { | 146 | catch(e) { |
147 | if (logger) { | 147 | if (logger) { |
148 | logger.warn('Exception on parsing result body: ' + e); | 148 | logger.warn('Exception on parsing result body: ' + e); |
149 | return; | 149 | return; |
150 | } | 150 | } |
151 | } | 151 | } |
152 | 152 | ||
153 | return data; | 153 | return data; |
154 | } | 154 | } |
155 | 155 | ||
156 | function responseDataProcessor(responseData) { | 156 | function responseDataProcessor(responseData) { |
157 | let retval = { | 157 | let retval = { |
158 | rc: '68', | 158 | rc: '68', |
159 | sn: '', | 159 | sn: '', |
160 | responseMessage: '', | 160 | responseMessage: '', |
161 | amount: 0, | 161 | amount: 0, |
162 | balance: 0, | 162 | balance: 0, |
163 | ts: '', | 163 | ts: '', |
164 | product: '', | 164 | product: '', |
165 | productType: '' | 165 | productType: '' |
166 | } | 166 | } |
167 | 167 | ||
168 | let combinedMessage = []; | 168 | let combinedMessage = []; |
169 | 169 | ||
170 | if (responseData.response_code == '0000') { | 170 | if (responseData.response_code == '0000') { |
171 | retval.rc = '00'; | 171 | retval.rc = '00'; |
172 | } | 172 | } |
173 | else if (['9990','0099','0068','0063','0005','9997','9999','9998','9990','0600','1003'].indexOf(responseData.response_code) >= 0) { | 173 | else if (['9990','0099','0068','0063','0005','9997','9999','9998','9990','0600','1003'].indexOf(responseData.response_code) >= 0) { |
174 | retval.rc = '68'; | 174 | retval.rc = '68'; |
175 | } | 175 | } |
176 | else { | 176 | else { |
177 | retval.rc = '40'; | 177 | retval.rc = '40'; |
178 | } | 178 | } |
179 | 179 | ||
180 | if (responseData.response_code) { | 180 | if (responseData.response_code) { |
181 | combinedMessage.push(responseData.response_code); | 181 | combinedMessage.push(responseData.response_code); |
182 | } | 182 | } |
183 | 183 | ||
184 | if (responseData.product) { | 184 | if (responseData.product) { |
185 | combinedMessage.push(responseData.product) | 185 | combinedMessage.push(responseData.product) |
186 | } | 186 | } |
187 | 187 | ||
188 | if (responseData.produk_tipe) { | 188 | if (responseData.produk_tipe) { |
189 | combinedMessage.push(responseData.produk_tipe) | 189 | combinedMessage.push(responseData.produk_tipe) |
190 | } | 190 | } |
191 | 191 | ||
192 | if (responseData.idpel) { | 192 | if (responseData.idpel) { |
193 | combinedMessage.push(responseData.idpel) | 193 | combinedMessage.push(responseData.idpel) |
194 | } | 194 | } |
195 | 195 | ||
196 | retval.responseMessage = responseData.response_message || ''; | 196 | retval.responseMessage = responseData.response_message || ''; |
197 | if (retval.responseMessage) { | 197 | if (retval.responseMessage) { |
198 | combinedMessage.push(retval.responseMessage); | 198 | combinedMessage.push(retval.responseMessage); |
199 | } | 199 | } |
200 | 200 | ||
201 | if (responseData.detail) { | 201 | if (responseData.detail) { |
202 | retval.sn = responseData.detail.voucherSerialNumber || ''; | 202 | retval.sn = responseData.detail.voucherSerialNumber || ''; |
203 | } | 203 | } |
204 | 204 | ||
205 | retval.amount = responseData.amount || 0; | 205 | retval.amount = responseData.amount || 0; |
206 | combinedMessage.push('amount: ' + retval.amount); | 206 | combinedMessage.push('amount: ' + retval.amount); |
207 | 207 | ||
208 | retval.balance = responseData.saldo || 0; | 208 | retval.balance = responseData.saldo || 0; |
209 | combinedMessage.push('balance: ' + retval.balance); | 209 | combinedMessage.push('balance: ' + retval.balance); |
210 | 210 | ||
211 | retval.ts = responseData.waktu || ''; | 211 | retval.ts = responseData.waktu || ''; |
212 | combinedMessage.push(retval.ts); | 212 | combinedMessage.push(retval.ts); |
213 | 213 | ||
214 | retval.loadTime = responseData.loadTime || ''; | 214 | retval.loadTime = responseData.loadTime || ''; |
215 | combinedMessage.push('load time: ' + retval.loadTime); | 215 | combinedMessage.push('load time: ' + retval.loadTime); |
216 | 216 | ||
217 | retval.combinedMessage = combinedMessage.join(' '); | 217 | retval.combinedMessage = combinedMessage.join(' '); |
218 | 218 | ||
219 | if (retval.sn) { | 219 | if (retval.sn) { |
220 | retval.combinedMessage = 'SN=' + retval.sn + '; ' + retval.combinedMessage; | 220 | retval.combinedMessage = 'SN=' + retval.sn + '; ' + retval.combinedMessage; |
221 | } | 221 | } |
222 | 222 | ||
223 | return retval; | 223 | return retval; |
224 | } | 224 | } |
225 | 225 | ||
226 | exports.start = start; | 226 | exports.start = start; |
227 | exports.createUrlPath = createUrlPath; | 227 | exports.createUrlPath = createUrlPath; |
228 | exports.parseResponseBody = parseResponseBody; | 228 | exports.parseResponseBody = parseResponseBody; |
229 | exports.responseDataProcessor = responseDataProcessor; | 229 | exports.responseDataProcessor = responseDataProcessor; |
230 | exports.topupRequest = topupRequest; | 230 | exports.topupRequest = topupRequest; |
231 | 231 |