Compare View

switch
from
...
to
 
Commits (3)

Changes

Showing 4 changed files Inline Diff

1 "use strict"; 1 "use strict";
2 2
3 const fs = require('fs'); 3 const fs = require('fs');
4 const os = require('os'); 4 const os = require('os');
5 5
6 const candindates = [ 6 const candindates = [
7 '/etc/komodo/config.js', 7 '/etc/komodo/config.js',
8 os.homedir() + '/main/config.json', 8 os.homedir() + '/main/config.json',
9 process.cwd() + '/../../main/config.json', 9 process.cwd() + '/../../main/config.json',
10 os.homedir() + '/Projects/tektrans/dev/komodo/config.json'
10 os.homedir() + '/Projects/tektrans/dev/komodo/config.json' 11 ];
11 ]; 12
12 13 let config;
13 let config; 14
14 15 for (let candindate in candindates) {
15 for (let candindate in candindates) { 16 if (fs.existsSync(candindate)) {
16 if (fs.existsSync(candindate)) { 17 try {
18 config = require(candindate);
19 config.this_config_filename = candindate;
20 }
21 catch(e) {}
22
17 try { 23 break;
18 config = require(candindate); 24 }
19 config.this_config_filename = candindate; 25 }
20 } 26
21 catch(e) {} 27 module.exports = config;
22 28
1 "use strict"; 1 "use strict";
2 2
3 const config = require('./config.js'); 3 const config = require('./config.js');
4 const logger = require('../logger');
4 const logger = require('../logger'); 5 const configFromMain = require('./config-from-main');
5 const configFromMain = require('./config-from-main'); 6
6 7 let core_url;
7 let core_url; 8
8 9 if (config.core_url) {
10 logger.verbose('Using CORE url from local config.json');
9 if (config.core_url) { 11 core_url = config.core_url;
10 logger.verbose('Using CORE url from local config.json'); 12 }
11 core_url = config.core_url; 13 else if (configFromMain && configFromMain.core && configFromMain.core.url && configFromMain.core.apikey) {
14 logger.verbose('Using CORE url from main config', {filename: configFromMain.this_config_filename});
12 } 15 core_url = configFromMain.core.url.replace(/\/$/, '') + '/apikey/' + configFromMain.core.apikey + '/pull/task';
13 else if (configFromMain && configFromMain.core && configFromMain.core.url && configFromMain.core.apikey) { 16 }
14 logger.verbose('Using CORE url from main config', {filename: configFromMain.this_config_filename}); 17
15 core_url = configFromMain.core.url.replace(/\/$/, '') + '/apikey/' + configFromMain.core.apikey + '/pull/task'; 18 module.exports = core_url;
16 } 19
1 "use strict"; 1 "use strict";
2 2
3 const request = require('request'); 3 const request = require('request');
4 4
5 const config = require('../config'); 5 const config = require('../config');
6 const logger = require('../logger'); 6 const logger = require('../logger');
7 const matrix = require('../matrix'); 7 const matrix = require('../matrix');
8 const controlPanel = require('../control-panel'); 8 const controlPanel = require('../control-panel');
9 const heartbeat = require('../heartbeat'); 9 const heartbeat = require('../heartbeat');
10 const core_url = require('../core-url'); 10 const core_url = require('../core-url');
11 11
12 const taskArchive = require('./task-archive'); 12 const taskArchive = require('./task-archive');
13 13
14 const MAX_SLEEP_BEFORE_RESEND_MS = 500; 14 const MAX_SLEEP_BEFORE_RESEND_MS = 500;
15 15
16 if (config.handler_name) { 16 if (config.handler_name) {
17 process.title = "KOMODO-GW@" + config.handler_name; 17 process.title = "KOMODO-GW@" + config.handler_name;
18 } 18 }
19 19
20 matrix.sdk_pending_tasks_count = 0; 20 matrix.sdk_pending_tasks_count = 0;
21 matrix.sdk_unresponsed_tasks_count = 0; 21 matrix.sdk_unresponsed_tasks_count = 0;
22 matrix.sdk_pending_with_response_tasks_count = 0; 22 matrix.sdk_pending_with_response_tasks_count = 0;
23 23
24 if (!matrix.sdk_pending_tasks) { 24 if (!matrix.sdk_pending_tasks) {
25 matrix.sdk_pending_tasks = []; 25 matrix.sdk_pending_tasks = [];
26 } 26 }
27 27
28 if (!matrix.sdk_unresponsed_tasks) { 28 if (!matrix.sdk_unresponsed_tasks) {
29 matrix.sdk_unresponsed_tasks = []; 29 matrix.sdk_unresponsed_tasks = [];
30 } 30 }
31 31
32 if (!matrix.sdk_pending_with_response_tasks) { 32 if (!matrix.sdk_pending_with_response_tasks) {
33 matrix.sdk_pending_with_response_tasks = []; 33 matrix.sdk_pending_with_response_tasks = [];
34 } 34 }
35 35
36 heartbeat.setModuleType('gateway'); 36 heartbeat.setModuleType('gateway');
37 37
38 var partner; 38 var partner;
39 39
40 function setPartner(_partner) { 40 function setPartner(_partner) {
41 partner = _partner; 41 partner = _partner;
42 } 42 }
43 43
44 function pullTask() { 44 function pullTask() {
45 if (!partner) { 45 if (!partner) {
46 return; 46 return;
47 } 47 }
48 48
49 let core_pull_task_url; 49 let core_pull_task_url;
50 50
51 if (core_url) { 51 if (core_url) {
52 core_pull_task_url = core_url + '/pull/task'; 52 core_pull_task_url = core_url + '/pull/task';
53 } 53 }
54 else if (config && config.pull_url && config.pull_url.task) { 54 else if (config && config.pull_url && config.pull_url.task) {
55 core_pull_task_url = config.pull_url.task.replace('<CORE_APIKEY>', config.core_apikey); 55 core_pull_task_url = config.pull_url.task.replace('<CORE_APIKEY>', config.core_apikey);
56 } 56 }
57 57
58 if (!core_pull_task_url) { 58 if (!core_pull_task_url) {
59 logger.warn('Unknown CORE task url'); 59 logger.warn('Unknown CORE task url');
60 return; 60 return;
61 } 61 }
62 62
63 let options = { 63 let options = {
64 url: core_pull_task_url, 64 url: core_pull_task_url,
65 qs: { 65 qs: {
66 handler: config.handler_name, 66 handler: config.handler_name,
67 products: config.products.join(','), 67 products: config.products.join(','),
68 advice_url: (config && config.push_server && config.push_server.apikey && config.push_server.advice && config.push_server.advice.url && config.push_server.advice.port) ? config.push_server.advice.url : null 68 advice_url: (config && config.push_server && config.push_server.apikey && config.push_server.advice && config.push_server.advice.url && config.push_server.advice.port) ? config.push_server.advice.url : null
69 } 69 }
70 } 70 }
71 71
72 if (config && config.debug_request_task_to_core) { 72 if (config && config.debug_request_task_to_core) {
73 logger.verbose('Requesting task to CORE', {url: options.url, qs: options.qs}); 73 logger.verbose('Requesting task to CORE', {url: options.url, qs: options.qs});
74 } 74 }
75 75
76 request(options, function(error, response, body) { 76 request(options, function(error, response, body) {
77 if (error) { 77 if (error) {
78 if (matrix.core_is_healthy) { 78 if (matrix.core_is_healthy) {
79 logger.warn('Error pulling task from CORE', {error: error}); 79 logger.warn('Error pulling task from CORE', {error: error});
80 } 80 }
81 matrix.core_is_healthy = false; 81 matrix.core_is_healthy = false;
82 return; 82 return;
83 } 83 }
84 84
85 if (response.statusCode != 200) { 85 if (response.statusCode != 200) {
86 if (matrix.core_is_healthy) { 86 if (matrix.core_is_healthy) {
87 logger.warn('CORE http response status code for pull task is not 200', {http_response_status: response.statusCode}); 87 logger.warn('CORE http response status code for pull task is not 200', {http_response_status: response.statusCode});
88 } 88 }
89 matrix.core_is_healthy = false; 89 matrix.core_is_healthy = false;
90 return; 90 return;
91 } 91 }
92 92
93 if (!matrix.core_is_healthy) { 93 if (!matrix.core_is_healthy) {
94 logger.verbose('CORE is healthy'); 94 logger.verbose('CORE is healthy');
95 } 95 }
96 matrix.core_is_healthy = true; 96 matrix.core_is_healthy = true;
97 97
98 if (body == 'NONE') { 98 if (body == 'NONE') {
99 return; 99 return;
100 } 100 }
101 101
102 forwardCoreTaskToPartner(body); 102 forwardCoreTaskToPartner(body);
103 }); 103 });
104 } 104 }
105 105
106 function putTaskToMatrix(task) { 106 function putTaskToMatrix(task) {
107 if (matrix.sdk_unresponsed_tasks.indexOf(task.trx_id) < 0) { 107 if (matrix.sdk_unresponsed_tasks.indexOf(task.trx_id) < 0) {
108 matrix.sdk_unresponsed_tasks.push(task.trx_id); 108 matrix.sdk_unresponsed_tasks.push(task.trx_id);
109 matrix.sdk_unresponsed_tasks_count = matrix.sdk_unresponsed_tasks.length; 109 matrix.sdk_unresponsed_tasks_count = matrix.sdk_unresponsed_tasks.length;
110 } 110 }
111 111
112 if (matrix.sdk_pending_tasks.indexOf(task.trx_id) < 0) { 112 if (matrix.sdk_pending_tasks.indexOf(task.trx_id) < 0) {
113 matrix.sdk_pending_tasks.push(task.trx_id); 113 matrix.sdk_pending_tasks.push(task.trx_id);
114 matrix.sdk_pending_tasks_count = matrix.sdk_pending_tasks.length; 114 matrix.sdk_pending_tasks_count = matrix.sdk_pending_tasks.length;
115 } 115 }
116 } 116 }
117 117
118 function updateTaskOnMatrix(trx_id, rc) { 118 function updateTaskOnMatrix(trx_id, rc) {
119 const unresponsed_task_idx = matrix.sdk_unresponsed_tasks.indexOf(trx_id); 119 const unresponsed_task_idx = matrix.sdk_unresponsed_tasks.indexOf(trx_id);
120 if (unresponsed_task_idx >= 0) { 120 if (unresponsed_task_idx >= 0) {
121 matrix.sdk_unresponsed_tasks.splice(unresponsed_task_idx, 1); 121 matrix.sdk_unresponsed_tasks.splice(unresponsed_task_idx, 1);
122 } 122 }
123 matrix.sdk_unresponsed_tasks_count = matrix.sdk_unresponsed_tasks.length; 123 matrix.sdk_unresponsed_tasks_count = matrix.sdk_unresponsed_tasks.length;
124 124
125 if (rc == '68') { 125 if (rc == '68') {
126 const pending_with_response_tasks_idx = matrix.sdk_pending_with_response_tasks.indexOf(trx_id); 126 const pending_with_response_tasks_idx = matrix.sdk_pending_with_response_tasks.indexOf(trx_id);
127 if (pending_with_response_tasks_idx < 0) { 127 if (pending_with_response_tasks_idx < 0) {
128 matrix.sdk_pending_with_response_tasks.push(trx_id); 128 matrix.sdk_pending_with_response_tasks.push(trx_id);
129 matrix.sdk_pending_with_response_tasks_count = matrix.sdk_pending_with_response_tasks.length; 129 matrix.sdk_pending_with_response_tasks_count = matrix.sdk_pending_with_response_tasks.length;
130 } 130 }
131 } 131 }
132 else { 132 else {
133 const pending_task_idx = matrix.sdk_pending_tasks.indexOf(trx_id); 133 const pending_task_idx = matrix.sdk_pending_tasks.indexOf(trx_id);
134 if (pending_task_idx >= 0) { 134 if (pending_task_idx >= 0) {
135 matrix.sdk_pending_tasks.splice(pending_task_idx, 1); 135 matrix.sdk_pending_tasks.splice(pending_task_idx, 1);
136 matrix.sdk_pending_tasks_count = matrix.sdk_pending_tasks.length; 136 matrix.sdk_pending_tasks_count = matrix.sdk_pending_tasks.length;
137 } 137 }
138 138
139 const pending_with_response_tasks_idx = matrix.sdk_pending_with_response_tasks.indexOf(trx_id); 139 const pending_with_response_tasks_idx = matrix.sdk_pending_with_response_tasks.indexOf(trx_id);
140 if (pending_with_response_tasks_idx >= 0) { 140 if (pending_with_response_tasks_idx >= 0) {
141 matrix.sdk_pending_with_response_tasks.splice(pending_with_response_tasks_idx, 1); 141 matrix.sdk_pending_with_response_tasks.splice(pending_with_response_tasks_idx, 1);
142 matrix.sdk_pending_with_response_tasks_count = matrix.sdk_pending_with_response_tasks.length; 142 matrix.sdk_pending_with_response_tasks_count = matrix.sdk_pending_with_response_tasks.length;
143 } 143 }
144 } 144 }
145 } 145 }
146 146
147 function forwardCoreTaskToPartner(coreMessage) { 147 function forwardCoreTaskToPartner(coreMessage) {
148 let task; 148 let task;
149 149
150 try { 150 try {
151 task = JSON.parse(coreMessage); 151 task = JSON.parse(coreMessage);
152 } 152 }
153 catch(e) { 153 catch(e) {
154 logger.warn('Exception on parsing CORE pull task response', {coreMessage: coreMessage, error: e}); 154 logger.warn('Exception on parsing CORE pull task response', {coreMessage: coreMessage, error: e});
155 } 155 }
156 156
157 incrementCounterTrx(); 157 incrementCounterTrx();
158 158
159 task.remote_product = getRemoteProduct(task.product); 159 task.remote_product = getRemoteProduct(task.product);
160 160
161 putTaskToMatrix(task); 161 putTaskToMatrix(task);
162 162
163 taskArchive.get(task, function(res) { 163 taskArchive.get(task, function(res) {
164 if (res && partner.advice) { 164 if (res && partner.advice) {
165 partner.advice(task); 165 partner.advice(task);
166 } 166 }
167 else { 167 else {
168 partner.buy(task); 168 partner.buy(task);
169 } 169 }
170 }); 170 });
171 } 171 }
172 172
173 function replaceRc(original_rc) { 173 function replaceRc(original_rc) {
174 if (!config || !config.replace_rc) { 174 if (!config || !config.replace_rc) {
175 return original_rc; 175 return original_rc;
176 } 176 }
177 177
178 return config.replace_rc[original_rc] || original_rc; 178 return config.replace_rc[original_rc] || original_rc;
179 } 179 }
180 180
181 function report(data) { 181 function report(data) {
182 182
183 let core_pull_report_url; 183 let core_pull_report_url;
184 184
185 if (data && data.trx_id && data.rc) { 185 if (data && data.trx_id && data.rc) {
186 updateTaskOnMatrix(data.trx_id, data.rc); 186 updateTaskOnMatrix(data.trx_id, data.rc);
187 } 187 }
188 188
189 if (core_url) { 189 if (core_url) {
190 core_pull_report_url = core_url + '/pull/report'; 190 core_pull_report_url = core_url + '/pull/report';
191 } else if (config && config.pull_url && config.pull_url.report) { 191 } else if (config && config.pull_url && config.pull_url.report) {
192 core_pull_report_url = config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey); 192 core_pull_report_url = config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey);
193 } 193 }
194 194
195 if (!core_pull_report_url) { 195 if (!core_pull_report_url) {
196 logger.warn('Unknown CORE report url'); 196 logger.warn('Unknown CORE report url');
197 return; 197 return;
198 } 198 }
199 199
200 if (config && config.push_server && config.push_server.apikey && config.push_server.advice && config.push_server.advice.url && config.push_server.advice.port) { 200 if (config && config.push_server && config.push_server.apikey && config.push_server.advice && config.push_server.advice.url && config.push_server.advice.port) {
201 if (!data.misc) { 201 if (!data.misc) {
202 data.misc = {}; 202 data.misc = {};
203 } 203 }
204 204
205 logger.verbose('Including advice url on report'); 205 logger.verbose('Including advice url on report');
206 206
207 data.misc.advice_url = config.push_server.advice.url; 207 data.misc.advice_url = config.push_server.advice.url;
208 } 208 }
209 209
210 let options = { 210 let options = {
211 url: core_pull_report_url, 211 url: core_pull_report_url,
212 form: { 212 form: {
213 trx_id: data.trx_id, 213 trx_id: data.trx_id,
214 rc: replaceRc(data.rc), 214 rc: replaceRc(data.rc),
215 message: data.message, 215 message: data.message,
216 handler: config.handler_name, 216 handler: config.handler_name,
217 sn: data.sn, 217 sn: data.sn,
218 amount: data.amount, 218 amount: data.amount,
219 raw: data.raw, 219 raw: data.raw,
220 misc: data.misc 220 misc: data.misc
221 } 221 }
222 } 222 }
223 223
224 if (!config.do_not_verbose_log_report) { 224 if (!config.do_not_verbose_log_report) {
225 logger.verbose('Report to CORE using HTTP POST'); 225 logger.verbose('Report to CORE using HTTP POST');
226 } 226 }
227 227
228 request.post(options, function(error, response, body) { 228 request.post(options, function(error, response, body) {
229 if (error) { 229 if (error) {
230 logger.warn('Error reporting to CORE', {error: error}); 230 logger.warn('Error reporting to CORE', {error: error});
231 resendReport(data); 231 resendReport(data);
232 } 232 }
233 else if (response.statusCode != 200) { 233 else if (response.statusCode != 200) {
234 logger.warn('Error reporting to CORE, http response status is not 200', {requestOptions: options, http_response_status: response.statusCode}); 234 logger.warn('Error reporting to CORE, http response status is not 200', {requestOptions: options, http_response_status: response.statusCode});
235 resendReport(data); 235 resendReport(data);
236 } 236 }
237 else if (!config.do_not_verbose_log_report) { 237 else if (!config.do_not_verbose_log_report) {
238 logger.verbose('Report has been sent to CORE', {requestOptions: options}); 238 logger.verbose('Report has been sent to CORE', {requestOptions: options});
239 } 239 }
240 }); 240 });
241 } 241 }
242 242
243 function resendReport(data) { 243 function resendReport(data) {
244 const sleepBeforeResend = Math.round(Math.random() * MAX_SLEEP_BEFORE_RESEND_MS) 244 const sleepBeforeResend = Math.round(Math.random() * MAX_SLEEP_BEFORE_RESEND_MS)
245 logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms') 245 logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms')
246 246
247 setTimeout( 247 setTimeout(
248 function() { 248 function() {
249 report(data); 249 report(data);
250 }, 250 },
251 sleepBeforeResend 251 sleepBeforeResend
252 ) 252 )
253 } 253 }
254 254
255 function isPaused() { 255 function isPaused() {
256 return matrix.paused; 256 return matrix.paused;
257 } 257 }
258 258
259 function pause() { 259 function pause() {
260 matrix.paused = true; 260 matrix.paused = true;
261 } 261 }
262 262
263 function resume() { 263 function resume() {
264 matrix.pause = false; 264 matrix.pause = false;
265 } 265 }
266 266
267 function initMatrix() { 267 function initMatrix() {
268 if (!matrix) { 268 if (!matrix) {
269 matrix = {}; 269 matrix = {};
270 } 270 }
271 271
272 matrix.counter = { 272 matrix.counter = {
273 trx: 0 273 trx: 0
274 } 274 }
275 } 275 }
276 276
277 function incrementCounterTrx() { 277 function incrementCounterTrx() {
278 matrix.counter.trx++; 278 matrix.counter.trx++;
279 } 279 }
280 280
281 function getRemoteProduct(product) { 281 function getRemoteProduct(product) {
282 let remoteProduct = config.remote_products[product]; 282 let remoteProduct = config.remote_products[product];
283 return remoteProduct || product; 283 return remoteProduct || product;
284 } 284 }
285 285
286 initMatrix(); 286 initMatrix();
287 setInterval(pullTask, config.pull_interval_ms || 1000); 287 setInterval(pullTask, config.pull_interval_ms || 1000);
288 288
289 exports.setPartner = setPartner; 289 exports.setPartner = setPartner;
290 exports.isPaused = isPaused; 290 exports.isPaused = isPaused;
291 exports.pause = pause; 291 exports.pause = pause;
292 exports.resume = resume; 292 exports.resume = resume;
293 exports.report = report; 293 exports.report = report;
294 exports.getRemoteProduct = getRemoteProduct; 294 exports.getRemoteProduct = getRemoteProduct;
295 295
1 { 1 {
2 "name": "komodo-sdk", 2 "name": "komodo-sdk",
3 "version": "1.23.0", 3 "version": "1.23.1",
4 "description": "SDK for Komodo", 4 "description": "SDK for Komodo",
5 "main": "index.js", 5 "main": "index.js",
6 "scripts": { 6 "scripts": {
7 "test": "mocha", 7 "test": "mocha",
8 "postversion": "git push && git push --tags" 8 "postversion": "git push && git push --tags"
9 }, 9 },
10 "repository": { 10 "repository": {
11 "type": "git", 11 "type": "git",
12 "url": "git@gitlab.kodesumber.com:komodo/komodo-sdk.git" 12 "url": "git@gitlab.kodesumber.com:komodo/komodo-sdk.git"
13 }, 13 },
14 "keywords": [ 14 "keywords": [
15 "ppob", 15 "ppob",
16 "payment", 16 "payment",
17 "komodo" 17 "komodo"
18 ], 18 ],
19 "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>", 19 "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>",
20 "license": "ISC", 20 "license": "ISC",
21 "dependencies": { 21 "dependencies": {
22 "basic-auth": "^2.0.0", 22 "basic-auth": "^2.0.0",
23 "body-parser": "^1.18.2", 23 "body-parser": "^1.18.2",
24 "express": "^4.16.3", 24 "express": "^4.16.3",
25 "express-session": "^1.15.6", 25 "express-session": "^1.15.6",
26 "lru-cache": "^4.1.1", 26 "lru-cache": "^4.1.1",
27 "macaddress": "^0.2.8", 27 "macaddress": "^0.2.8",
28 "moment": "^2.19.1", 28 "moment": "^2.19.1",
29 "node-machine-id": "^1.1.10", 29 "node-machine-id": "^1.1.10",
30 "node-natural-sort": "^0.8.6", 30 "node-natural-sort": "^0.8.6",
31 "numeral": "^2.0.6", 31 "numeral": "^2.0.6",
32 "nunjucks": "^3.0.1", 32 "nunjucks": "^3.0.1",
33 "redis": "^2.8.0", 33 "redis": "^2.8.0",
34 "request": "^2.81.0", 34 "request": "^2.81.0",
35 "sha1": "^1.1.1", 35 "sha1": "^1.1.1",
36 "simple-git": "^1.80.1", 36 "simple-git": "^1.80.1",
37 "strftime": "^0.10.0", 37 "strftime": "^0.10.0",
38 "uniqid": "^4.1.1", 38 "uniqid": "^4.1.1",
39 "uuid": "^3.1.0", 39 "uuid": "^3.1.0",
40 "winston": "^2.3.1", 40 "winston": "^2.3.1",
41 "winston-circular-buffer": "^1.0.0", 41 "winston-circular-buffer": "^1.0.0",
42 "winston-daily-rotate-file": "^1.4.6" 42 "winston-daily-rotate-file": "^1.4.6"
43 } 43 }
44 } 44 }
45 45