This repository has been archived by the owner on Aug 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
donate.js
152 lines (122 loc) · 4.19 KB
/
donate.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const http = require("http");
const url = require("url");
require("dotenv").config();
const stripe = require("stripe")(process.env.STRIPE_SECRET);
const allowed_origins = process.env.PROCA_DONATE_CORS_DOMAINS
? new Set(process.env.PROCA_DONATE_CORS_DOMAINS.split(',').filter(x => x !== ''))
: true;
if (!process.env.PROCA_DONATE_CORS_DOMAINS)
console.warn ('PROCA_DONATE_CORS_DOMAINS is empty, accepting donations from any domain' );
// log(`CORS DOMAINS ${allowed_origins.keys()}`);
// log(`allowed_origins ${allowed_origins.has("http")}`);
const log = (request, msg, ...args) => {
console.log(`[proca-donate] [${new Date().toUTCString()}] [${request.url}] ${msg}`, ...args);
}
const paymentIntent = async (request, param) => {
// TODO: reject if actionPage missing?
param.meta = {
actionPage: param.actionPage || "?",
referer: request.headers.referer
};
const r = await stripe.paymentIntents.create({
amount: param.amount * 100,
currency: (param.currency && param.currency.toLowerCase()) || 'eur',
payment_method_types: param.payment_method_types || ['card'],
});
log(request, `stripe response`, r);
if (r.client_secret)
return { "secret": r.client_secret };
return {};
};
const checkOrigin = (request) => {
if (allowed_origins === true) return true;
let origin = request.headers['origin'];
return allowed_origins.has(origin);
}
const corsResponse = (request, response) => {
if (!checkOrigin(request)) {
response.statusCode = 401;
return response.end();
}
response.statusCode = 204;
response.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
response.setHeader("Access-Control-Allow-Headers", "*");
response.setHeader("Access-Control-Allow-Origin", origin);
response.setHeader("Access-Control-Max_age", "86400");
response.setHeader("Vary", "Origin");
return response.end();
}
const httpServer = http.createServer(async (request, response) => {
const getBody = async (req) => {
return new Promise((resolve, reject) => {
try {
let body = "";
req.on("data", (chunk) => {
body += chunk.toString(); // convert Buffer to string
});
req.on("end", () => {
//resolve(parse(body));
resolve(body);
});
} catch (e) {
reject(e);
}
});
};
switch (request.method) {
case "GET":
response.statusCode = 500;
response.end(
JSON.stringify({ error: true, message: "no-get" })
);
break;
case "OPTIONS":
corsResponse(request, response);
break;
default:
break; // empty blocks hurt my eyes. =)
}
// any other request.method is treated as a request to process ?
if (request.method !== "POST") {
response.statusCode = 400;
log(request, `oops, method is ${request.method}`);
response.end(JSON.stringify({ error: true, message: "Please use POST." }))
return;
}
const parsedUrl = new URL(request.url, `http://${request.headers.host}`);
// the browser should never let this happen, but hey
if (!checkOrigin(request)) {
response.statusCode = 401;
return response.end();
}
response.setHeader("Content-Type", "application/json");
response.setHeader("Access-Control-Allow-Origin", request.headers['origin']);
if (parsedUrl.pathname === "/stripe/oneoff") {
let data = null;
const body = await getBody(request);
try {
to_charge = JSON.parse(body);
// TODO: validation
// NOTE: Stripe returns a payment intent without valid parameters, what's going on there?
return response.end(JSON.stringify(await paymentIntent(request, to_charge)));
} catch (e) {
if (e instanceof SyntaxError) {
response.statusCode = 400;
return response.end(
JSON.stringify({ error: true, message: "no-json", received: body })
);
}
log(request, `Exception ${e} ${e.stack}`);
response.statusCode = 500;
return response.end();
}
}
response.statusCode = 404;
response.end(
JSON.stringify({ error: true, message: "404", "path": parsedUrl.path })
);
});
const port = process.env.PROCA_DONATE_PORT || 8888;
httpServer.listen(port, () => {
console.log(`[proca-donate] Listening on ${port}`);
});