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