Commit 44e4cd25a74936e386c7203ed4bc25abe3ab10a2

Authored by Adhidarma Hadiwinoto
1 parent 4acfa75293
Exists in master

exports partner.start

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