Compare View

switch
from
...
to
 
Commits (2)

Changes

Showing 3 changed files Inline Diff

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