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