-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
129 lines (107 loc) · 3.48 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
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
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.post("/process_payment", (req, res) => {
const { selectedPaymentMethod, formData } = req.body;
if ( formData?.payment_method_id === 'pse' ) {
// 'pse' is only available for Colombia
formData.additional_info = {
/*
If you are running behind a proxy, which is the case of this sample project (because we use localtunnel)
'req.ip' will only work because of the following express setup => app.set('trust proxy', true)
*/
ip_address: req.ip
}
formData.callback_url = `${host}/payment_status`
}
mercadopago.payment.create(formData)
.then(response => res.status(201).json(formatResponse(response)))
.catch(error => {
const { errorMessage, errorStatus } = validateError(error);
res.status(errorStatus).json({ error_message: errorMessage });
});
});
function formatResponse(response) {
const { response: data } = response;
return {
detail: data.status_detail,
status: data.status,
id: data.id
};
}
function validateError(error) {
let errorMessage = 'Unknown error cause';
let errorStatus = 500;
if (error.cause && error.cause.length) {
const sdkErrorMessage = error.cause[0].description;
errorMessage = sdkErrorMessage || errorMessage;
const sdkErrorStatus = error.status;
errorStatus = sdkErrorStatus || errorStatus;
}
return { errorMessage, errorStatus };
}
app.listen(8080, () => {
console.log("The server is now running on port 8080");
});