-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·128 lines (106 loc) · 3.68 KB
/
server.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
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
import bodyParser from 'body-parser';
import express from 'express';
import dotenv from 'dotenv';
import stripe from 'stripe';
import axios from 'axios';
import cors from 'cors';
import { getTournaments, fetchOfficial, scrapeMetrix } from './scrapeTournaments.js';
import { handleCache } from './scrapeStores.js';
import { getRatings } from './scrapeRating.js';
dotenv.config();
const app = express();
app.use(bodyParser.text({ type: '*/*' }));
if (process.env.NODE_ENV === 'production') {
const allowedOrigin = process.env.ALLOWED_ORIGIN;
app.use(cors({
origin: function (origin, callback) {
// allow requests with no origin
// (like mobile apps or curl requests)
if (!origin) return callback(null, true);
if (allowedOrigin.indexOf(origin) === -1) {
var msg = `The CORS policy for this site does not allow access from origin ${origin}.`;
return callback(new Error(msg), false);
}
return callback(null, true);
}
}));
} else {
app.use(cors({ origin: '*' }));
}
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send({ message: err.message });
});
app.get('/tournaments', async (req, res, next) => getTournaments('official', fetchOfficial)(req, res, next));
app.get('/tournaments/metrix', async (req, res, next) => getTournaments('metrix', scrapeMetrix)(req, res, next));
app.get('/bagtag', async (req, res, next) => {
try {
const response = await fetch(process.env.BAGTAG_ENDPOINT);
const body = await response.json();
res.json(body);
} catch (error) {
console.error(error);
res.status(500).json({ message: 'An error occured' });
}
})
app.get('/products/:type/:query', async (req, res, next) => {
const { type, query } = req.params;
try {
const data = await handleCache(type, query)
res.json(data);
} catch (error) {
console.error(error);
res.status(500).json({ message: 'An error occured' });
}
});
app.get('/ratings', async (req, res, next) => {
try {
const data = await getRatings()
res.json(data);
} catch (error) {
console.error(error);
res.status(500).json({ message: 'An error occured' });
}
});
// Endpoint for stripe webhook
// Transforms the data for a Discord notification
const endpointSecret = process.env.STRIPE_WEBHOOK_SECRET;
app.post('/stripe-webhook', (request, response) => {
const sig = request.headers['stripe-signature'];
let event = null;
try {
event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
} catch (err) {
console.log('err:', err);
// invalid signature
response.status(400).end();
return;
}
let intent = null;
switch (event['type']) {
case 'payment_intent.succeeded':
intent = event.data.object;
console.log("Succeeded:", intent.id);
triggerDiscordNotification(intent);
break;
default:
console.log('Unhandled event type:', event['type']);
}
response.sendStatus(200);
});
const triggerDiscordNotification = async (intent) => {
const url = process.env.DISCORD_WEBHOOK_URL;
const channelId = process.env.DISCORD_CHANNEL_ID;
if (!url || !channelId) return;
const data = {
content: `New payment over ${intent?.amount / 100} ${intent?.currency?.toUpperCase()} received: Check stripe for details: https://dashboard.stripe.com/payments/${intent?.id}`,
channel_id: channelId,
};
try {
await axios.post(url, data);
} catch (error) {
console.error(error);
}
return;
}
app.listen(process.env.PORT || 8080, () => console.log(`Server has started on http://localhost:${process.env.PORT || 8080}`));