Compare View
Commits (2)
Changes
Showing 2 changed files Inline Diff
gateway/pull.js
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 replaceRc(original_rc) { | ||
95 | if (!config || !config.replace_rc || !config.replace_rc.length) { | ||
96 | return original_rc; | ||
97 | } | ||
98 | |||
99 | return config.replace_rc[original_rc] || original_rc; | ||
100 | } | ||
101 | |||
94 | function replaceRc(original_rc) { | 102 | function report(data) { |
95 | if (!config || !config.replace_rc || !config.replace_rc.length) { | 103 | |
96 | return original_rc; | 104 | let core_pull_report_url; |
97 | } | 105 | |
98 | 106 | if (config.core_url) { | |
99 | return config.replace_rc[original_rc] || original_rc; | 107 | core_pull_report_url = config.core_url + '/pull/report'; |
100 | } | 108 | } else if (config.pull_url.report) { |
101 | 109 | core_pull_report_url = config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey); | |
102 | function report(data) { | 110 | } |
103 | 111 | ||
104 | let core_pull_report_url; | 112 | if (!core_pull_report_url) { |
105 | 113 | logger.warn('Unknown CORE report url'); | |
106 | if (config.core_url) { | 114 | return; |
107 | core_pull_report_url = config.core_url + '/pull/report'; | 115 | } |
108 | } else if (config.pull_url.report) { | 116 | |
109 | core_pull_report_url = config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey); | 117 | let options = { |
110 | } | 118 | url: core_pull_report_url, |
111 | 119 | form: { | |
112 | if (!core_pull_report_url) { | 120 | trx_id: data.trx_id, |
113 | logger.warn('Unknown CORE report url'); | 121 | rc: replaceRc(original_rc), |
114 | return; | 122 | message: data.message, |
115 | } | 123 | handler: config.handler_name, |
116 | 124 | sn: data.sn, | |
117 | let options = { | 125 | amount: data.amount, |
118 | url: core_pull_report_url, | 126 | raw: data.raw, |
119 | form: { | 127 | misc: data.misc |
120 | trx_id: data.trx_id, | 128 | } |
121 | rc: replaceRc(original_rc), | 129 | } |
122 | message: data.message, | 130 | |
123 | handler: config.handler_name, | 131 | if (!config.do_not_verbose_log_report) { |
124 | sn: data.sn, | 132 | logger.verbose('Report to CORE using HTTP POST'); |
125 | amount: data.amount, | 133 | } |
126 | raw: data.raw, | 134 | |
127 | misc: data.misc | 135 | request.post(options, function(error, response, body) { |
128 | } | 136 | if (error) { |
129 | } | 137 | logger.warn('Error reporting to CORE', {error: error}); |
130 | 138 | } | |
131 | if (!config.do_not_verbose_log_report) { | 139 | else if (response.statusCode != 200) { |
132 | logger.verbose('Report to CORE using HTTP POST'); | 140 | logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode}); |
133 | } | 141 | } |
134 | 142 | else if (!config.do_not_verbose_log_report) { | |
135 | request.post(options, function(error, response, body) { | 143 | logger.verbose('Report has been sent to CORE', {requestOptions: options}); |
136 | if (error) { | 144 | } |
137 | logger.warn('Error reporting to CORE', {error: error}); | 145 | }); |
138 | } | 146 | } |
139 | else if (response.statusCode != 200) { | 147 | |
140 | logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode}); | 148 | function resendReport(data) { |
141 | } | 149 | let sleepBeforeResend = 1000; |
142 | else if (!config.do_not_verbose_log_report) { | 150 | logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms') |
143 | logger.verbose('Report has been sent to CORE', {requestOptions: options}); | 151 | |
144 | } | 152 | setTimeout( |
145 | }); | 153 | function() { |
146 | } | 154 | report(data); |
147 | 155 | }, | |
148 | function resendReport(data) { | 156 | sleepBeforeResend |
149 | let sleepBeforeResend = 1000; | 157 | ) |
150 | logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms') | 158 | } |
151 | 159 | ||
152 | setTimeout( | 160 | function isPaused() { |
153 | function() { | 161 | return matrix.paused; |
154 | report(data); | 162 | } |
155 | }, | 163 | |
156 | sleepBeforeResend | 164 | function pause() { |
157 | ) | 165 | matrix.paused = true; |
158 | } | 166 | } |
159 | 167 | ||
160 | function isPaused() { | 168 | function resume() { |
161 | return matrix.paused; | 169 | matrix.pause = false; |
162 | } | 170 | } |
163 | 171 | ||
164 | function pause() { | 172 | function initMatrix() { |
165 | matrix.paused = true; | 173 | if (!matrix) { |
166 | } | 174 | matrix = {}; |
167 | 175 | } | |
168 | function resume() { | 176 | |
169 | matrix.pause = false; | 177 | matrix.counter = { |
170 | } | 178 | trx: 0 |
171 | 179 | } | |
172 | function initMatrix() { | 180 | } |
173 | if (!matrix) { | 181 | |
174 | matrix = {}; | 182 | function incrementCounterTrx() { |
175 | } | 183 | matrix.counter.trx++; |
176 | 184 | } | |
177 | matrix.counter = { | 185 | |
178 | trx: 0 | 186 | function getRemoteProduct(product) { |
179 | } | 187 | let remoteProduct = config.remote_products[product]; |
180 | } | 188 | return remoteProduct || product; |
181 | 189 | } | |
182 | function incrementCounterTrx() { | 190 | |
183 | matrix.counter.trx++; | 191 | initMatrix(); |
184 | } | 192 | setInterval(pullTask, config.pull_interval_ms || 1000); |
185 | 193 | ||
186 | function getRemoteProduct(product) { | 194 | exports.setPartner = setPartner; |
187 | let remoteProduct = config.remote_products[product]; | 195 | exports.isPaused = isPaused; |
188 | return remoteProduct || product; | 196 | exports.pause = pause; |
189 | } | 197 | exports.resume = resume; |
190 | 198 | exports.report = report; | |
191 | initMatrix(); | 199 |
package.json
1 | { | 1 | { |
2 | "name": "komodo-sdk", | 2 | "name": "komodo-sdk", |
3 | "version": "1.13.12", | 3 | "version": "1.14.0", |
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 |