-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
45 lines (39 loc) · 1.25 KB
/
app.js
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
const path = require('path');
class QuickPos {
constructor(config) {
this.providers = {};
this.loadProviders(config.providers);
}
loadProviders(providerConfigs) {
for (const [providerName, providerConfig] of Object.entries(providerConfigs)) {
try {
const ProviderClass = require(path.join(__dirname, 'lib', `${providerName}.js`));
this.providers[providerName] = new ProviderClass(providerConfig);
} catch (error) {
console.error(`Failed to load provider ${providerName}:`, error);
}
}
}
middleware() {
return async (req, res, next) => {
req.quickPos = this.providers;
next();
};
}
handleCallback(providerName) {
return async (req, res, next) => {
if (!this.providers[providerName]) {
return res.status(400).json({ error: 'Invalid payment provider' });
}
try {
let qProviders = ["paymaya"];
const result = await this.providers[providerName].handleCallback(!qProviders.includes(providerName) ? req.body : req.query);
req.paymentResult = result;
next();
} catch (error) {
res.status(500).json({ error: error.message });
}
};
}
}
module.exports = QuickPos;