-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
106 lines (86 loc) · 2.81 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
const path = require("path");
const express = require("express");
const mercadopago = require("mercadopago");
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();
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 (_, 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 preference = {
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 = {
ip_address: req.ip
}
formData.callback_url = 'https://mercadopago.com/'
}
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");
});