Compare View

switch
from
...
to
 
Commits (2)

Changes

Showing 3 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');
12
11 const taskArchive = require('./task-archive'); 13 if (config.handler_name) {
12 14 process.title = "KOMODO-GW@" + config.handler_name;
13 if (config.handler_name) { 15 }
14 process.title = "KOMODO-GW@" + config.handler_name; 16
15 } 17 heartbeat.setModuleType('gateway');
16 18
17 heartbeat.setModuleType('gateway'); 19 var partner;
18 20
19 var partner; 21 function setPartner(_partner) {
20 22 partner = _partner;
21 function setPartner(_partner) { 23 }
22 partner = _partner; 24
23 } 25 function pullTask() {
24 26 if (!partner) {
25 function pullTask() { 27 return;
26 if (!partner) { 28 }
27 return; 29
28 } 30 let core_pull_task_url;
29 31
30 let core_pull_task_url; 32 if (config.core_url) {
31 33 core_pull_task_url = config.core_url + '/pull/task';
32 if (config.core_url) { 34 } else if (config.pull_url.task) {
33 core_pull_task_url = config.core_url + '/pull/task'; 35 core_pull_task_url = config.pull_url.task.replace('<CORE_APIKEY>', config.core_apikey);
34 } else if (config.pull_url.task) { 36 }
35 core_pull_task_url = config.pull_url.task.replace('<CORE_APIKEY>', config.core_apikey); 37
36 } 38 if (!core_pull_task_url) {
37 39 logger.warn('Unknown CORE task url');
38 if (!core_pull_task_url) { 40 return;
39 logger.warn('Unknown CORE task url'); 41 }
40 return; 42
41 } 43 let options = {
42 44 url: core_pull_task_url,
43 let options = { 45 qs: {
44 url: core_pull_task_url, 46 handler: config.handler_name,
45 qs: { 47 products: config.products.join(',')
46 handler: config.handler_name, 48 }
47 products: config.products.join(',') 49 }
48 } 50
49 } 51 request(options, function(error, response, body) {
50 52 if (error) {
51 request(options, function(error, response, body) { 53 if (matrix.core_is_healthy) {
52 if (error) { 54 logger.warn('Error pulling task from CORE', {error: error});
53 if (matrix.core_is_healthy) { 55 }
54 logger.warn('Error pulling task from CORE', {error: error}); 56 matrix.core_is_healthy = false;
55 } 57 return;
56 matrix.core_is_healthy = false; 58 }
57 return; 59
58 } 60 if (response.statusCode != 200) {
59 61 if (matrix.core_is_healthy) {
60 if (response.statusCode != 200) { 62 logger.warn('CORE http response status code for pull task is not 200', {http_response_status: response.statusCode});
61 if (matrix.core_is_healthy) { 63 }
62 logger.warn('CORE http response status code for pull task is not 200', {http_response_status: response.statusCode}); 64 matrix.core_is_healthy = false;
63 } 65 return;
64 matrix.core_is_healthy = false; 66 }
65 return; 67
66 } 68 if (!matrix.core_is_healthy) {
67 69 logger.verbose('CORE is healthy');
68 if (!matrix.core_is_healthy) { 70 }
69 logger.verbose('CORE is healthy'); 71 matrix.core_is_healthy = true;
70 } 72
71 matrix.core_is_healthy = true; 73 if (body == 'NONE') {
72 74 return;
73 if (body == 'NONE') { 75 }
74 return; 76
75 } 77 forwardCoreTaskToPartner(body);
76 78 });
77 forwardCoreTaskToPartner(body); 79 }
78 }); 80
79 } 81 function forwardCoreTaskToPartner(coreMessage) {
80 82 let task;
81 function forwardCoreTaskToPartner(coreMessage) { 83
82 let task; 84 try {
83 85 task = JSON.parse(coreMessage);
84 try { 86 }
85 task = JSON.parse(coreMessage); 87 catch(e) {
86 } 88 logger.warn('Exception on parsing CORE pull task response', {coreMessage: coreMessage, error: e});
87 catch(e) { 89 }
88 logger.warn('Exception on parsing CORE pull task response', {coreMessage: coreMessage, error: e}); 90
89 } 91 task.remote_product = getRemoteProduct(task.product);
90 92
91 task.remote_product = getRemoteProduct(task.product); 93 taskArchive.get(task, function(res) {
94 if (res && partner.advice) {
95 partner.advice(task);
96 }
97 else {
98 partner.buy(task);
99 }
100 });
101
102
92 103 }
93 taskArchive.get(task, function(res) { 104
94 if (res && partner.advice) { 105 function replaceRc(original_rc) {
95 partner.advice(task); 106 if (!config || !config.replace_rc || !config.replace_rc.length) {
96 } 107 return original_rc;
97 else { 108 }
98 partner.buy(task); 109
99 } 110 return config.replace_rc[original_rc] || original_rc;
100 }); 111 }
101 112
102 113 function report(data) {
103 } 114
104 115 let core_pull_report_url;
105 function replaceRc(original_rc) { 116
106 if (!config || !config.replace_rc || !config.replace_rc.length) { 117 if (config.core_url) {
107 return original_rc; 118 core_pull_report_url = config.core_url + '/pull/report';
108 } 119 } else if (config.pull_url.report) {
109 120 core_pull_report_url = config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey);
110 return config.replace_rc[original_rc] || original_rc; 121 }
111 } 122
112 123 if (!core_pull_report_url) {
113 function report(data) { 124 logger.warn('Unknown CORE report url');
114 125 return;
115 let core_pull_report_url; 126 }
116 127
117 if (config.core_url) { 128 let options = {
118 core_pull_report_url = config.core_url + '/pull/report'; 129 url: core_pull_report_url,
119 } else if (config.pull_url.report) { 130 form: {
120 core_pull_report_url = config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey); 131 trx_id: data.trx_id,
121 } 132 rc: replaceRc(data.rc),
122 133 message: data.message,
123 if (!core_pull_report_url) { 134 handler: config.handler_name,
124 logger.warn('Unknown CORE report url'); 135 sn: data.sn,
125 return; 136 amount: data.amount,
126 } 137 raw: data.raw,
127 138 misc: data.misc
128 let options = { 139 }
129 url: core_pull_report_url, 140 }
130 form: { 141
131 trx_id: data.trx_id, 142 if (!config.do_not_verbose_log_report) {
132 rc: replaceRc(data.rc), 143 logger.verbose('Report to CORE using HTTP POST');
133 message: data.message, 144 }
134 handler: config.handler_name, 145
135 sn: data.sn, 146 request.post(options, function(error, response, body) {
136 amount: data.amount, 147 if (error) {
137 raw: data.raw, 148 logger.warn('Error reporting to CORE', {error: error});
138 misc: data.misc 149 }
139 } 150 else if (response.statusCode != 200) {
140 } 151 logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode});
141 152 }
142 if (!config.do_not_verbose_log_report) { 153 else if (!config.do_not_verbose_log_report) {
143 logger.verbose('Report to CORE using HTTP POST'); 154 logger.verbose('Report has been sent to CORE', {requestOptions: options});
144 } 155 }
145 156 });
146 request.post(options, function(error, response, body) { 157 }
147 if (error) { 158
148 logger.warn('Error reporting to CORE', {error: error}); 159 function resendReport(data) {
149 } 160 let sleepBeforeResend = 1000;
150 else if (response.statusCode != 200) { 161 logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms')
151 logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode}); 162
152 } 163 setTimeout(
153 else if (!config.do_not_verbose_log_report) { 164 function() {
154 logger.verbose('Report has been sent to CORE', {requestOptions: options}); 165 report(data);
155 } 166 },
156 }); 167 sleepBeforeResend
157 } 168 )
158 169 }
159 function resendReport(data) { 170
160 let sleepBeforeResend = 1000; 171 function isPaused() {
161 logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms') 172 return matrix.paused;
162 173 }
163 setTimeout( 174
164 function() { 175 function pause() {
165 report(data); 176 matrix.paused = true;
166 }, 177 }
167 sleepBeforeResend 178
168 ) 179 function resume() {
169 } 180 matrix.pause = false;
170 181 }
171 function isPaused() { 182
172 return matrix.paused; 183 function initMatrix() {
173 } 184 if (!matrix) {
174 185 matrix = {};
175 function pause() { 186 }
176 matrix.paused = true; 187
177 } 188 matrix.counter = {
178 189 trx: 0
179 function resume() { 190 }
180 matrix.pause = false; 191 }
181 } 192
182 193 function incrementCounterTrx() {
183 function initMatrix() { 194 matrix.counter.trx++;
184 if (!matrix) { 195 }
185 matrix = {}; 196
186 } 197 function getRemoteProduct(product) {
187 198 let remoteProduct = config.remote_products[product];
188 matrix.counter = { 199 return remoteProduct || product;
189 trx: 0 200 }
190 } 201
191 } 202 initMatrix();
192 203 setInterval(pullTask, config.pull_interval_ms || 1000);
193 function incrementCounterTrx() { 204
194 matrix.counter.trx++; 205 exports.setPartner = setPartner;
195 } 206 exports.isPaused = isPaused;
196 207 exports.pause = pause;
197 function getRemoteProduct(product) { 208 exports.resume = resume;
198 let remoteProduct = config.remote_products[product]; 209 exports.report = report;
199 return remoteProduct || product; 210
gateway/task-archive.js
File was created 1 "use strict";
2
3 const module_name = require('path').basename(__filename);
4
5 const redis = require("redis");
6
7 const config = require('../config');
8 const logger = require('../logger');
9 const matrix = require('../matrix');
10
11 let redis_client;
12
13 function init() {
14 if (!config.redis) {
15 return;
16 }
17
18 redis_client = redis.createClient(config.redis);
19 }
20
21 function keyword(task) {
22 return 'geckoo_' + config.handler_name + '_' + task.trx_id;
23 }
24
25 function put(task) {
26 if (!redis_client) {
27 return;
28 }
29
30 const keyword = keyword(task);
31
32 redis_client.set(keyword, JSON.stringify(task));
33 redis_client.expire(keyword, 3600 * 24 * 15);
34 }
35
36 function get(task, cb) {
37 if (!redis_client) {
38 cb(null);
39 return;
40 }
41
42 redis_client.get(keyword(task), function(res) {
43 if (!res) {
44 cb(null);
45 }
46 else {
47 let resObj;
48
49 try {
50 resObj = JSON.parse(res);
51 }
52 catch(e) {
53 cb(null);
54 return;
55 }
56
57 cb(resObj);
58 }
59 });
60 }
61
62 init();
63
64 exports.put = put;
65 exports.get = get;
1 "use strict"; 66
1 { 1 {
2 "name": "komodo-sdk", 2 "name": "komodo-sdk",
3 "version": "1.14.1", 3 "version": "1.14.2",
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.2", 24 "express": "^4.16.2",
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 "moment": "^2.19.1", 27 "moment": "^2.19.1",
28 "numeral": "^2.0.6", 28 "numeral": "^2.0.6",
29 "nunjucks": "^3.0.1", 29 "nunjucks": "^3.0.1",
30 "redis": "^2.8.0",
30 "redis": "^2.8.0", 31 "request": "^2.81.0",
31 "request": "^2.81.0", 32 "simple-git": "^1.80.1",
32 "simple-git": "^1.80.1", 33 "strftime": "^0.10.0",
33 "strftime": "^0.10.0", 34 "uniqid": "^4.1.1",
34 "uniqid": "^4.1.1", 35 "uuid": "^3.1.0",
35 "uuid": "^3.1.0", 36 "winston": "^2.3.1",
36 "winston": "^2.3.1", 37 "winston-circular-buffer": "^1.0.0",
37 "winston-circular-buffer": "^1.0.0", 38 "winston-daily-rotate-file": "^1.4.6"
38 "winston-daily-rotate-file": "^1.4.6" 39 }
39 } 40 }
40 } 41