Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ejemplo backend express para MercadoPago
const mercadopago = require ('mercadopago');
const express = require('express');
const port = 3000;
mercadopago.configure({
access_token: 'PROD_ACCESS_TOKEN'
});
app.post('/api/orders', (req, res) => {
/* aquí crea tu orden en la DB para el usuario logeado */
const order = db.orders.create({ userId: req.userId, productId: req.body.productId }); // <--- pseudo-código
// Ahora le decimos a MP que cree la "preferencia". Asume que "order" tiene datos del producto
mercadopago.preferences.create({
items: [
{
title: order.product.name,
unit_price: order.price,
quantity: order.quantity,
}
]
}).then((preference) => {
// el front recibirá el preferenceId :)
res.json({ preferenceId: preference.id });
});
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment