forked from DavidsonGomes/gerador-pix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
69 lines (58 loc) · 1.74 KB
/
index.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
import express from "express";
import bodyParser from "body-parser";
import helmet from "helmet";
import pino from "pino-http";
import exphbs from "express-handlebars";
import QRCode from "qrcode";
import BrCode from "./lib/br_code.js";
const app = express();
app.use(helmet());
app.use(pino());
app.use(bodyParser.json());
app.engine("handlebars", exphbs());
app.set("view engine", "handlebars");
const port = process.env.PORT || 8081;
const QR_CODE_SIZE = 400;
const article_links = [];
const contentSecurityPolicy = [
"script-src 'self' 'nonce-2726c7f26c' www.googletagmanager.com pagead2.googlesyndication.com",
].join(";");
app.use((req, res, next) => {
res.setHeader("Content-Security-Policy", contentSecurityPolicy);
next();
});
app.get("/", function (req, res) {
res.json({
message: "Welcome to generate pix server!",
});
});
app.post("/emvqr-static", (req, res) => {
var { key, amount, name, reference, key_type, city } = req.body;
if (key) {
var brCode = new BrCode(key, amount, name, reference, key_type, city);
var code = brCode.generate_qrcp();
QRCode.toDataURL(code, { width: QR_CODE_SIZE, height: QR_CODE_SIZE })
.then((qrcode) => {
res.json({
qrcode_base64: qrcode,
code: code,
key_type: brCode.key_type,
key: brCode.key,
amount: brCode.amount,
name: brCode.name,
city: brCode.city,
reference: brCode.reference,
formated_amount: brCode.formated_amount(),
});
})
.catch((err) => {
console.error(err);
});
} else {
res.status(422);
res.json({ error: "Campo Key não presente" });
}
});
app.listen(port, () => {
console.log(`Starting generate pix server on port ${port}!`);
});