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