-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
132 lines (115 loc) · 3.58 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
130
131
132
const express = require("express");
const cors = require("cors");
const Stripe = require("stripe");
const app = express();
const PORT = 3001;
const connection = require("./db.js");
require("dotenv").config();
// Middleware para manejar JSON
app.use(express.json());
// Uso de cors
app.use(cors());
// Declaramos stripe
const stripe = new Stripe(process.env.STRIPE_PRIVATE_KEY);
// ----- Funciones -----
async function getAllProducts() {
const selectQuery = "SELECT * FROM products";
return new Promise((resolve, reject) => {
connection.query(selectQuery, (err, results) => {
if (err) {
console.error("Error al ejecutar la consulta SELECT: " + err);
reject(err);
return;
}
resolve(results.rows);
});
});
}
async function getSpecificProduct(id) {
const selectQuery = "SELECT * FROM products WHERE id = " + id;
return new Promise((resolve, reject) => {
connection.query(selectQuery, (err, results) => {
if (err) {
console.error("Error al ejecutar la consulta SELECT: " + err);
reject(err);
return;
}
resolve(results.rows);
});
});
}
async function getCategory(category) {
const selectQuery =
"SELECT * FROM products WHERE category = '" + category + "'";
console.log(selectQuery);
return new Promise((resolve, reject) => {
connection.query(selectQuery, (err, results) => {
if (err) {
console.error("Error al ejecutar la consulta SELECT: " + err);
reject(err);
return;
}
resolve(results.rows);
});
});
}
// ----- Rutas -----
app.get("/products", async function (request, response) {
try {
const result = await getAllProducts();
response.json(result);
} catch (error) {
response.status(500).send("Error en el servidor");
}
});
app.get("/products/:id", async function (request, response) {
try {
const id = request.params.id;
const result = await getSpecificProduct(id);
response.json(result);
} catch (error) {
response.status(500).send("Error en el servidor");
}
});
app.get("/categorias/:category", async function (request, response) {
try {
const category = request.params.category;
const result = await getCategory(category);
response.json(result);
} catch (error) {
response.status(500).send("Error en el servidor");
}
});
app.get("/", function (request, response) {
response.send("Home");
});
app.post("/create-checkout-session", async (request, response) => {
const cart = request.body;
// Mapeamos el carrito para configurar los line_items para Stripe
const line_items = cart.map((item) => ({
price_data: {
currency: "eur",
product_data: {
name: item.name,
description: item.description, // Asegúrate de que tu objeto `item` tenga una propiedad `description`
},
unit_amount: Math.round(item.price * 100), // Stripe trabaja con centavos, así que multiplicamos por 100
},
quantity: item.quantity, // Asegúrate de que tu objeto `item` tenga una propiedad `quantity`
}));
console.log(line_items);
const session = await stripe.checkout.sessions.create({
payment_method_types: ["card"],
line_items: line_items,
mode: "payment",
success_url: "https://ecommerce-api-react.vercel.app/",
cancel_url: "https://ecommerce-api-react.vercel.app/",
});
return response.json(session);
});
app.get("/success", (request, response) => response.send("success"));
app.get("/cancel", (request, response) => response.send("cancel"));
// Iniciar el servidor
app.listen(PORT, () => {
console.log(`Servidor corriendo en el puerto ${PORT}`);
});