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
|
const config = require("./config");
const l = require("pino")();
module.exports = (auth, app, db, emit) => {
app.post("/buy", auth, async (req, res) => {
const stripe = require("stripe")(config.stripe);
const { token, amount, sat } = req.body;
let dollarAmount = parseInt(amount / 100);
if (dollarAmount > req.user.limit) return res.status(500).end();
try {
const charge = await stripe.charges.create({
amount,
currency: "cad",
description: "Bitcoin",
source: token
});
req.user.balance += parseInt(sat);
req.user.limit -= dollarAmount;
await req.user.save();
emit(req.user.username, "user", req.user);
await db.Payment.create({
user_id: req.user.id,
hash: charge.balance_transaction,
amount: parseInt(sat),
currency: "CAD",
rate: app.get("rates").ask,
received: true,
tip: 0,
confirmed: true,
});
res.send(`Bought ${amount}`);
} catch (e) {
l.error(e);
res.status(500).send(e);
}
});
};
|