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