Commit b0180091c08b0be24eb5409b7e6e79143ba8ff09

Authored by Adhidarma Hadiwinoto
1 parent 4daf69632d
Exists in master

update balance

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