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