Commit 48979b2ba8d998d5de12dcbc2ded9def129040c3

Authored by Adhidarma Hadiwinoto
1 parent b0180091c0
Exists in master

melengkapi init object data

Showing 1 changed file with 3 additions and 0 deletions Inline Diff

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 amount: 0, 166 amount: 0,
166 balance: 0, 167 balance: 0,
168 saldo: 0,
167 ts: '', 169 ts: '',
170 loadTime: 0,
168 product: '', 171 product: '',
169 productType: '' 172 productType: ''
170 } 173 }
171 174
172 let combinedMessage = []; 175 let combinedMessage = [];
173 176
174 if (responseData.response_code == '0000') { 177 if (responseData.response_code == '0000') {
175 retval.rc = '00'; 178 retval.rc = '00';
176 } 179 }
177 else if (['9990','0099','0068','0063','0005','9997','9999','9998','9990','0600','1003'].indexOf(responseData.response_code) >= 0) { 180 else if (['9990','0099','0068','0063','0005','9997','9999','9998','9990','0600','1003'].indexOf(responseData.response_code) >= 0) {
178 retval.rc = '68'; 181 retval.rc = '68';
179 } 182 }
180 else { 183 else {
181 retval.rc = '40'; 184 retval.rc = '40';
182 } 185 }
183 186
184 if (responseData.response_code) { 187 if (responseData.response_code) {
185 combinedMessage.push(responseData.response_code); 188 combinedMessage.push(responseData.response_code);
186 } 189 }
187 190
188 if (responseData.product) { 191 if (responseData.product) {
189 combinedMessage.push(responseData.product) 192 combinedMessage.push(responseData.product)
190 } 193 }
191 194
192 if (responseData.produk_tipe) { 195 if (responseData.produk_tipe) {
193 combinedMessage.push(responseData.produk_tipe) 196 combinedMessage.push(responseData.produk_tipe)
194 } 197 }
195 198
196 if (responseData.idpel) { 199 if (responseData.idpel) {
197 combinedMessage.push(responseData.idpel) 200 combinedMessage.push(responseData.idpel)
198 } 201 }
199 202
200 retval.responseMessage = responseData.response_message || ''; 203 retval.responseMessage = responseData.response_message || '';
201 if (retval.responseMessage) { 204 if (retval.responseMessage) {
202 combinedMessage.push(retval.responseMessage); 205 combinedMessage.push(retval.responseMessage);
203 } 206 }
204 207
205 if (responseData.detail) { 208 if (responseData.detail) {
206 retval.sn = responseData.detail.voucherSerialNumber || ''; 209 retval.sn = responseData.detail.voucherSerialNumber || '';
207 } 210 }
208 211
209 retval.amount = responseData.amount || 0; 212 retval.amount = responseData.amount || 0;
210 combinedMessage.push('amount: ' + retval.amount); 213 combinedMessage.push('amount: ' + retval.amount);
211 214
212 retval.balance = responseData.saldo || 0; 215 retval.balance = responseData.saldo || 0;
213 retval.saldo = retval.balance; 216 retval.saldo = retval.balance;
214 combinedMessage.push('balance: ' + retval.balance); 217 combinedMessage.push('balance: ' + retval.balance);
215 218
216 retval.ts = responseData.waktu || ''; 219 retval.ts = responseData.waktu || '';
217 combinedMessage.push(retval.ts); 220 combinedMessage.push(retval.ts);
218 221
219 retval.loadTime = responseData.loadTime || ''; 222 retval.loadTime = responseData.loadTime || '';
220 combinedMessage.push('load time: ' + retval.loadTime); 223 combinedMessage.push('load time: ' + retval.loadTime);
221 224
222 retval.combinedMessage = combinedMessage.join(' '); 225 retval.combinedMessage = combinedMessage.join(' ');
223 226
224 if (retval.sn) { 227 if (retval.sn) {
225 retval.combinedMessage = 'SN=' + retval.sn + '; ' + retval.combinedMessage; 228 retval.combinedMessage = 'SN=' + retval.sn + '; ' + retval.combinedMessage;
226 } 229 }
227 230
228 return retval; 231 return retval;
229 } 232 }
230 233
231 exports.start = start; 234 exports.start = start;
232 exports.createUrlPath = createUrlPath; 235 exports.createUrlPath = createUrlPath;
233 exports.parseResponseBody = parseResponseBody; 236 exports.parseResponseBody = parseResponseBody;
234 exports.responseDataProcessor = responseDataProcessor; 237 exports.responseDataProcessor = responseDataProcessor;
235 exports.topupRequest = topupRequest; 238 exports.topupRequest = topupRequest;
236 239