Compare View

switch
from
...
to
 
Commits (3)

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 if (config.handler_name) { 11 if (config.handler_name) {
12 process.title = "KOMODO-GW@" + config.handler_name; 12 process.title = "KOMODO-GW@" + config.handler_name;
13 } 13 }
14 14
15 heartbeat.setModuleType('gateway'); 15 heartbeat.setModuleType('gateway');
16 16
17 var partner; 17 var partner;
18 18
19 function setPartner(_partner) { 19 function setPartner(_partner) {
20 partner = _partner; 20 partner = _partner;
21 } 21 }
22 22
23 function pullTask() { 23 function pullTask() {
24 if (!partner) { 24 if (!partner) {
25 return; 25 return;
26 } 26 }
27 27
28 let core_pull_task_url; 28 let core_pull_task_url;
29 29
30 if (config.core_url) { 30 if (config.core_url) {
31 core_pull_task_url = config.core_url + '/pull/task'; 31 core_pull_task_url = config.core_url + '/pull/task';
32 } else if (config.pull_url.task) { 32 } else if (config.pull_url.task) {
33 core_pull_task_url = config.pull_url.task.replace('<CORE_APIKEY>', config.core_apikey); 33 core_pull_task_url = config.pull_url.task.replace('<CORE_APIKEY>', config.core_apikey);
34 } 34 }
35 35
36 if (!core_pull_task_url) { 36 if (!core_pull_task_url) {
37 logger.warn('Unknown CORE task url'); 37 logger.warn('Unknown CORE task url');
38 return; 38 return;
39 } 39 }
40 40
41 let options = { 41 let options = {
42 url: core_pull_task_url, 42 url: core_pull_task_url,
43 qs: { 43 qs: {
44 handler: config.handler_name, 44 handler: config.handler_name,
45 products: config.products.join(',') 45 products: config.products.join(',')
46 } 46 }
47 } 47 }
48 48
49 request(options, function(error, response, body) { 49 request(options, function(error, response, body) {
50 if (error) { 50 if (error) {
51 if (matrix.core_is_healthy) { 51 if (matrix.core_is_healthy) {
52 logger.warn('Error pulling task from CORE', {error: error}); 52 logger.warn('Error pulling task from CORE', {error: error});
53 } 53 }
54 matrix.core_is_healthy = false; 54 matrix.core_is_healthy = false;
55 return; 55 return;
56 } 56 }
57 57
58 if (response.statusCode != 200) { 58 if (response.statusCode != 200) {
59 if (matrix.core_is_healthy) { 59 if (matrix.core_is_healthy) {
60 logger.warn('CORE http response status code for pull task is not 200', {http_response_status: response.statusCode}); 60 logger.warn('CORE http response status code for pull task is not 200', {http_response_status: response.statusCode});
61 } 61 }
62 matrix.core_is_healthy = false; 62 matrix.core_is_healthy = false;
63 return; 63 return;
64 } 64 }
65 65
66 if (!matrix.core_is_healthy) { 66 if (!matrix.core_is_healthy) {
67 logger.verbose('CORE is healthy'); 67 logger.verbose('CORE is healthy');
68 } 68 }
69 matrix.core_is_healthy = true; 69 matrix.core_is_healthy = true;
70 70
71 if (body == 'NONE') { 71 if (body == 'NONE') {
72 return; 72 return;
73 } 73 }
74 74
75 forwardCoreTaskToPartner(body); 75 forwardCoreTaskToPartner(body);
76 }); 76 });
77 } 77 }
78 78
79 function forwardCoreTaskToPartner(coreMessage) { 79 function forwardCoreTaskToPartner(coreMessage) {
80 let task; 80 let task;
81 81
82 try { 82 try {
83 task = JSON.parse(coreMessage); 83 task = JSON.parse(coreMessage);
84 } 84 }
85 catch(e) { 85 catch(e) {
86 logger.warn('Exception on parsing CORE pull task response', {coreMessage: coreMessage, error: e}); 86 logger.warn('Exception on parsing CORE pull task response', {coreMessage: coreMessage, error: e});
87 } 87 }
88 88
89 task.remote_product = getRemoteProduct(task.product); 89 task.remote_product = getRemoteProduct(task.product);
90 90
91 partner.buy(task); 91 partner.buy(task);
92 } 92 }
93 93
94 function report(data) { 94 function report(data) {
95 95
96 let core_pull_report_url; 96 let core_pull_report_url;
97 97
98 if (config.core_url) { 98 if (config.core_url) {
99 core_pull_report_url = config.core_url + '/pull/report'; 99 core_pull_report_url = config.core_url + '/pull/report';
100 } else if (config.pull_url.report) { 100 } else if (config.pull_url.report) {
101 core_pull_report_url = config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey); 101 core_pull_report_url = config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey);
102 } 102 }
103 103
104 if (!core_pull_report_url) { 104 if (!core_pull_report_url) {
105 logger.warn('Unknown CORE report url'); 105 logger.warn('Unknown CORE report url');
106 return; 106 return;
107 } 107 }
108 108
109 let options = { 109 let options = {
110 url: core_pull_report_url, 110 url: core_pull_report_url,
111 form: { 111 form: {
112 trx_id: data.trx_id, 112 trx_id: data.trx_id,
113 rc: data.rc, 113 rc: data.rc,
114 message: data.message, 114 message: data.message,
115 handler: config.handler_name, 115 handler: config.handler_name,
116 sn: data.sn, 116 sn: data.sn,
117 amount: data.amount, 117 amount: data.amount,
118 handler: config.handler_name, 118 handler: config.handler_name,
119 raw: data.raw, 119 raw: data.raw,
120 misc: data.misc 120 misc: data.misc
121 } 121 }
122 } 122 }
123 123
124 if (!config.do_not_verbose_log_report) { 124 if (!config.do_not_verbose_log_report) {
125 logger.verbose('Report to CORE using HTTP POST');
126 }
127
125 logger.verbose('Report to CORE using HTTP POST'); 128 request.post(options, function(error, response, body) {
126 } 129 if (error) {
127 130 logger.warn('Error reporting to CORE', {error: error});
128 request.post(options, function(error, response, body) { 131 }
129 if (error) { 132 else if (response.statusCode != 200) {
130 logger.warn('Error reporting to CORE', {error: error}); 133 logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode});
131 } 134 }
132 else if (response.statusCode != 200) { 135 else if (!config.do_not_verbose_log_report) {
133 logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode}); 136 logger.verbose('Report has been sent to CORE', {requestOptions: options});
134 } 137 }
135 else if (!config.do_not_verbose_log_report) { 138 });
136 logger.verbose('Report has been sent to CORE', {requestOptions: options}); 139 }
137 } 140
138 }); 141 function resendReport(data) {
139 } 142 let sleepBeforeResend = 1000;
140 143 logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms')
141 function resendReport(data) { 144
142 let sleepBeforeResend = 1000; 145 setTimeout(
143 logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms') 146 function() {
144 147 report(data);
145 setTimeout( 148 },
146 function() { 149 sleepBeforeResend
147 report(data); 150 )
148 }, 151 }
149 sleepBeforeResend 152
150 ) 153 function isPaused() {
151 } 154 return matrix.paused;
152 155 }
153 function isPaused() { 156
154 return matrix.paused; 157 function pause() {
155 } 158 matrix.paused = true;
156 159 }
157 function pause() { 160
158 matrix.paused = true; 161 function resume() {
159 } 162 matrix.pause = false;
160 163 }
161 function resume() { 164
162 matrix.pause = false; 165 function initMatrix() {
163 } 166 if (!matrix) {
164 167 matrix = {};
165 function initMatrix() { 168 }
166 if (!matrix) { 169
167 matrix = {}; 170 matrix.counter = {
168 } 171 trx: 0
169 172 }
170 matrix.counter = { 173 }
171 trx: 0 174
172 } 175 function incrementCounterTrx() {
173 } 176 matrix.counter.trx++;
174 177 }
175 function incrementCounterTrx() { 178
176 matrix.counter.trx++; 179 function getRemoteProduct(product) {
177 } 180 let remoteProduct = config.remote_products[product];
178 181 return remoteProduct || product;
179 function getRemoteProduct(product) { 182 }
180 let remoteProduct = config.remote_products[product]; 183
181 return remoteProduct || product; 184 initMatrix();
182 } 185 setInterval(pullTask, config.pull_interval_ms || 1000);
183 186
184 initMatrix(); 187 exports.setPartner = setPartner;
185 setInterval(pullTask, config.pull_interval_ms || 1000); 188 exports.isPaused = isPaused;
186 189 exports.pause = pause;
187 exports.setPartner = setPartner; 190 exports.resume = resume;
188 exports.isPaused = isPaused; 191 exports.report = report;
189 exports.pause = pause; 192
1 { 1 {
2 "name": "komodo-sdk", 2 "name": "komodo-sdk",
3 "version": "1.13.9", 3 "version": "1.13.10",
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