Compare View

switch
from
...
to
 
Commits (2)

Changes

Showing 2 changed files Inline Diff

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 10
11 const taskArchive = require('./task-archive'); 11 const taskArchive = require('./task-archive');
12 12
13 const MAX_SLEEP_BEFORE_RESEND_MS = 500; 13 const MAX_SLEEP_BEFORE_RESEND_MS = 500;
14 14
15 if (config.handler_name) { 15 if (config.handler_name) {
16 process.title = "KOMODO-GW@" + config.handler_name; 16 process.title = "KOMODO-GW@" + config.handler_name;
17 } 17 }
18 18
19 if (!matrix.pending_tasks) { 19 if (!matrix.pending_tasks) {
20 matrix.sdk_pending_tasks = []; 20 matrix.sdk_pending_tasks = [];
21 } 21 }
22 22
23 if (!matrix.active_tasks) { 23 if (!matrix.active_tasks) {
24 matrix.sdk_unresponsed_tasks = []; 24 matrix.sdk_unresponsed_tasks = [];
25 } 25 }
26 26
27 heartbeat.setModuleType('gateway'); 27 heartbeat.setModuleType('gateway');
28 28
29 var partner; 29 var partner;
30 30
31 function setPartner(_partner) { 31 function setPartner(_partner) {
32 partner = _partner; 32 partner = _partner;
33 } 33 }
34 34
35 function pullTask() { 35 function pullTask() {
36 if (!partner) { 36 if (!partner) {
37 return; 37 return;
38 } 38 }
39 39
40 let core_pull_task_url; 40 let core_pull_task_url;
41 41
42 if (config.core_url) { 42 if (config.core_url) {
43 core_pull_task_url = config.core_url + '/pull/task'; 43 core_pull_task_url = config.core_url + '/pull/task';
44 } else if (config.pull_url.task) { 44 } else if (config.pull_url.task) {
45 core_pull_task_url = config.pull_url.task.replace('<CORE_APIKEY>', config.core_apikey); 45 core_pull_task_url = config.pull_url.task.replace('<CORE_APIKEY>', config.core_apikey);
46 } 46 }
47 47
48 if (!core_pull_task_url) { 48 if (!core_pull_task_url) {
49 logger.warn('Unknown CORE task url'); 49 logger.warn('Unknown CORE task url');
50 return; 50 return;
51 } 51 }
52 52
53 let options = { 53 let options = {
54 url: core_pull_task_url, 54 url: core_pull_task_url,
55 qs: { 55 qs: {
56 handler: config.handler_name, 56 handler: config.handler_name,
57 products: config.products.join(',') 57 products: config.products.join(',')
58 } 58 }
59 } 59 }
60 60
61 request(options, function(error, response, body) { 61 request(options, function(error, response, body) {
62 if (error) { 62 if (error) {
63 if (matrix.core_is_healthy) { 63 if (matrix.core_is_healthy) {
64 logger.warn('Error pulling task from CORE', {error: error}); 64 logger.warn('Error pulling task from CORE', {error: error});
65 } 65 }
66 matrix.core_is_healthy = false; 66 matrix.core_is_healthy = false;
67 return; 67 return;
68 } 68 }
69 69
70 if (response.statusCode != 200) { 70 if (response.statusCode != 200) {
71 if (matrix.core_is_healthy) { 71 if (matrix.core_is_healthy) {
72 logger.warn('CORE http response status code for pull task is not 200', {http_response_status: response.statusCode}); 72 logger.warn('CORE http response status code for pull task is not 200', {http_response_status: response.statusCode});
73 } 73 }
74 matrix.core_is_healthy = false; 74 matrix.core_is_healthy = false;
75 return; 75 return;
76 } 76 }
77 77
78 if (!matrix.core_is_healthy) { 78 if (!matrix.core_is_healthy) {
79 logger.verbose('CORE is healthy'); 79 logger.verbose('CORE is healthy');
80 } 80 }
81 matrix.core_is_healthy = true; 81 matrix.core_is_healthy = true;
82 82
83 if (body == 'NONE') { 83 if (body == 'NONE') {
84 return; 84 return;
85 } 85 }
86 86
87 forwardCoreTaskToPartner(body); 87 forwardCoreTaskToPartner(body);
88 }); 88 });
89 } 89 }
90 90
91 function putTaskToMatrix(task) { 91 function putTaskToMatrix(task) {
92 if (matrix.sdk_unresponsed_tasks.indexOf(task.trx_id) < 0) { 92 if (matrix.sdk_unresponsed_tasks.indexOf(task.trx_id) < 0) {
93 matrix.sdk_unresponsed_tasks.push(task.trix_id); 93 matrix.sdk_unresponsed_tasks.push(task.trix_id);
94 } 94 }
95 95
96 if (matrix.sdk_pending_tasks.indexOf(task.trx_id) < 0) { 96 if (matrix.sdk_pending_tasks.indexOf(task.trx_id) < 0) {
97 matrix.sdk_pending_tasks.push(task.trx_id); 97 matrix.sdk_pending_tasks.push(task.trx_id);
98 } 98 }
99 } 99 }
100 100
101 function updateTaskOnMatrix(trx_id, rc) { 101 function updateTaskOnMatrix(trx_id, rc) {
102 const unresponsed_task_idx = matrix.sdk_unresponsed_tasks.indexOf(trx_id); 102 const unresponsed_task_idx = matrix.sdk_unresponsed_tasks.indexOf(trx_id);
103 if (unresponsed_task_idx >= 0) { 103 if (unresponsed_task_idx >= 0) {
104 matrix.sdk_unresponsed_tasks.splice(unresponsed_task_idx, 1); 104 matrix.sdk_unresponsed_tasks.splice(unresponsed_task_idx, 1);
105 } 105 }
106 106
107 if (rc === '68') { 107 if (rc === '68') {
108 return; 108 return;
109 } 109 }
110 110
111 const pending_task_idx = matrix.sdk_pending_tasks.indexOf(trx_id); 111 const pending_task_idx = matrix.sdk_pending_tasks.indexOf(trx_id);
112 if (pending_task_idx >= 0) { 112 if (pending_task_idx >= 0) {
113 matrix.sdk_pending_tasks.splice(pending_task_idx, 1); 113 matrix.sdk_pending_tasks.splice(pending_task_idx, 1);
114 } 114 }
115 } 115 }
116 116
117 function forwardCoreTaskToPartner(coreMessage) { 117 function forwardCoreTaskToPartner(coreMessage) {
118 let task; 118 let task;
119 119
120 try { 120 try {
121 task = JSON.parse(coreMessage); 121 task = JSON.parse(coreMessage);
122 } 122 }
123 catch(e) { 123 catch(e) {
124 logger.warn('Exception on parsing CORE pull task response', {coreMessage: coreMessage, error: e}); 124 logger.warn('Exception on parsing CORE pull task response', {coreMessage: coreMessage, error: e});
125 } 125 }
126 126
127 incrementCounterTrx(); 127 incrementCounterTrx();
128 128
129 task.remote_product = getRemoteProduct(task.product); 129 task.remote_product = getRemoteProduct(task.product);
130 130
131 putTaskToMatrix(task); 131 putTaskToMatrix(task);
132 132
133 taskArchive.get(task, function(res) { 133 taskArchive.get(task, function(res) {
134 if (res && partner.advice) { 134 if (res && partner.advice) {
135 partner.advice(task); 135 partner.advice(task);
136 } 136 }
137 else { 137 else {
138 partner.buy(task); 138 partner.buy(task);
139 } 139 }
140 }); 140 });
141 } 141 }
142 142
143 function replaceRc(original_rc) { 143 function replaceRc(original_rc) {
144 if (!config || !config.replace_rc || !config.replace_rc.length) { 144 if (!config || !config.replace_rc || !config.replace_rc.length) {
145 return original_rc; 145 return original_rc;
146 } 146 }
147 147
148 return config.replace_rc[original_rc] || original_rc; 148 return config.replace_rc[original_rc] || original_rc;
149 } 149 }
150 150
151 function report(data) { 151 function report(data) {
152 152
153 let core_pull_report_url; 153 let core_pull_report_url;
154 154
155 if (data && data.trx_id && data.rc) { 155 if (data && data.trx_id && data.rc) {
156 updateTaskOnMatrix(data.trx_id, data.rc); 156 updateTaskOnMatrix(data.trx_id, data.rc);
157 } 157 }
158 158
159 if (config.core_url) { 159 if (config.core_url) {
160 core_pull_report_url = config.core_url + '/pull/report'; 160 core_pull_report_url = config.core_url + '/pull/report';
161 } else if (config.pull_url.report) { 161 } else if (config.pull_url.report) {
162 core_pull_report_url = config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey); 162 core_pull_report_url = config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey);
163 } 163 }
164 164
165 if (!core_pull_report_url) { 165 if (!core_pull_report_url) {
166 logger.warn('Unknown CORE report url'); 166 logger.warn('Unknown CORE report url');
167 return; 167 return;
168 } 168 }
169 169
170 if (config && config.push_server && config.push_server.apikey && config.push_server.advice && config.push_server.advice.url && config.push_server.advice.port) { 170 if (config && config.push_server && config.push_server.apikey && config.push_server.advice && config.push_server.advice.url && config.push_server.advice.port) {
171 if (!data.misc) { 171 if (!data.misc) {
172 data.misc = {}; 172 data.misc = {};
173 } 173 }
174 174
175 logger.verbose('Including advice url on report'); 175 logger.verbose('Including advice url on report');
176 176
177 data.misc.advice_url = config.push_server.advice.url; 177 data.misc.advice_url = config.push_server.advice.url;
178 } 178 }
179 179
180 let options = { 180 let options = {
181 url: core_pull_report_url, 181 url: core_pull_report_url,
182 form: { 182 form: {
183 trx_id: data.trx_id, 183 trx_id: data.trx_id,
184 rc: replaceRc(data.rc), 184 rc: replaceRc(data.rc),
185 message: data.message, 185 message: data.message,
186 handler: config.handler_name, 186 handler: config.handler_name,
187 sn: data.sn, 187 sn: data.sn,
188 amount: data.amount, 188 amount: data.amount,
189 raw: data.raw, 189 raw: data.raw,
190 misc: data.misc 190 misc: data.misc
191 } 191 }
192 } 192 }
193 193
194 if (!config.do_not_verbose_log_report) { 194 if (!config.do_not_verbose_log_report) {
195 logger.verbose('Report to CORE using HTTP POST'); 195 logger.verbose('Report to CORE using HTTP POST');
196 } 196 }
197 197
198 request.post(options, function(error, response, body) { 198 request.post(options, function(error, response, body) {
199 if (error) { 199 if (error) {
200 logger.warn('Error reporting to CORE', {error: error}); 200 logger.warn('Error reporting to CORE', {error: error});
201 resendReport(data); 201 resendReport(data);
202 } 202 }
203 else if (response.statusCode != 200) { 203 else if (response.statusCode != 200) {
204 logger.warn('Error reporting to CORE, http response status is not 200', {requestOptions: options, http_response_status: response.statusCode}); 204 logger.warn('Error reporting to CORE, http response status is not 200', {requestOptions: options, http_response_status: response.statusCode});
205 resendReport(data); 205 resendReport(data);
206 } 206 }
207 else if (!config.do_not_verbose_log_report) { 207 else if (!config.do_not_verbose_log_report) {
208 logger.verbose('Report has been sent to CORE', {requestOptions: options}); 208 logger.verbose('Report has been sent to CORE', {requestOptions: options});
209 } 209 }
210 }); 210 });
211 } 211 }
212 212
213 function resendReport(data) { 213 function resendReport(data) {
214 const sleepBeforeResend = Math.round(Math.random() * MAX_SLEEP_BEFORE_RESEND_MS) 214 const sleepBeforeResend = Math.round(Math.random() * MAX_SLEEP_BEFORE_RESEND_MS)
215 logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms') 215 logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms')
216 216
217 setTimeout( 217 setTimeout(
218 function() { 218 function() {
219 report(data); 219 report(data);
220 }, 220 },
221 sleepBeforeResend 221 sleepBeforeResend
222 ) 222 )
223 } 223 }
224 224
225 function isPaused() { 225 function isPaused() {
226 return matrix.paused; 226 return matrix.paused;
227 } 227 }
228 228
229 function pause() { 229 function pause() {
230 matrix.paused = true; 230 matrix.paused = true;
231 } 231 }
232 232
233 function resume() { 233 function resume() {
234 matrix.pause = false; 234 matrix.pause = false;
235 } 235 }
236 236
237 function initMatrix() { 237 function initMatrix() {
238 if (!matrix) { 238 if (!matrix) {
239 matrix = {}; 239 matrix = {};
240 } 240 }
241 241
242 matrix.counter = { 242 matrix.counter = {
243 trx: 0 243 trx: 0
244 } 244 }
245 } 245 }
246 246
247 function incrementCounterTrx() { 247 function incrementCounterTrx() {
248 matrix.counter.trx++; 248 matrix.counter.trx++;
249 } 249 }
250 250
251 function getRemoteProduct(product) { 251 function getRemoteProduct(product) {
252 let remoteProduct = config.remote_products[product]; 252 let remoteProduct = config.remote_products[product];
253 return remoteProduct || product; 253 return remoteProduct || product;
254 } 254 }
255 255
256 initMatrix(); 256 initMatrix();
257 setInterval(pullTask, config.pull_interval_ms || 1000); 257 setInterval(pullTask, config.pull_interval_ms || 1000);
258 258
259 exports.setPartner = setPartner; 259 exports.setPartner = setPartner;
260 exports.isPaused = isPaused; 260 exports.isPaused = isPaused;
261 exports.pause = pause; 261 exports.pause = pause;
262 exports.resume = resume; 262 exports.resume = resume;
263 exports.report = report; 263 exports.report = report;
264 exports.getRemoteProduct = getRemoteProduct; 264 exports.getRemoteProduct = getRemoteProduct;
265 265
1 { 1 {
2 "name": "komodo-sdk", 2 "name": "komodo-sdk",
3 "version": "1.20.0", 3 "version": "1.20.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