-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
82 lines (68 loc) · 2.12 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
70
71
72
73
74
75
76
77
78
79
80
81
82
const path = require("path");
const express = require("express");
const mercadopago = require("mercadopago");
const host = process.env.HOST;
if (!host) {
console.log("Error: host not defined");
process.exit(1);
}
const mercadoPagoPublicKey = process.env.MERCADO_PAGO_SAMPLE_PUBLIC_KEY;
if (!mercadoPagoPublicKey) {
console.log("Error: public key not defined");
process.exit(1);
}
const mercadoPagoAccessToken = process.env.MERCADO_PAGO_SAMPLE_ACCESS_TOKEN;
if (!mercadoPagoAccessToken) {
console.log("Error: access token not defined");
process.exit(1);
}
mercadopago.configurations.setAccessToken(mercadoPagoAccessToken);
const app = express();
/*
If you are going to use this code as a quick start for your project,
make sure you trust the proxy in order to keep the following line.
*/
app.set('trust proxy', true);
app.set("view engine", "html");
app.engine("html", require("hbs").__express);
app.set("views", path.join(__dirname, "views"))
app.use(express.urlencoded({ extended: false }));
app.use(express.static("./static"));
app.use(express.json());
app.get("/", function (req, res) {
res.status(200).render("index", { mercadoPagoPublicKey });
});
app.get("/payment_status", (req, res) => {
const { payment_id: paymentId } = req.query;
res.status(200).render("status", { mercadoPagoPublicKey, paymentId });
});
app.get("/preference_id", async function (req, res) {
const { unitPrice, quantity } = req.query;
const backUrl = `${host}/payment_status`;
const preference = {
back_urls: {
success: backUrl,
failure: backUrl,
pending: backUrl
},
auto_return: "approved",
items: [
{
id: "item-ID-1234",
title: "White t-shirt",
unit_price: unitPrice ? Number(unitPrice) : 100,
quantity: quantity ? parseInt(quantity) : 10,
},
],
};
try {
const response = await mercadopago.preferences.create(preference);
const preferenceId = response.body.id;
res.status(201).json({ preferenceId });
} catch (error) {
res.status(500).json({ error });
}
});
app.listen(8080, () => {
console.log("The server is now running on port 8080");
});