On my node/express server I make a redirect to the mercadopago a pi so that the user can purchase the product. When doing the redirect , the api interface is opened where the user enters their user data, and decides the payment method (cash, card, etc..). After the user completes the purchase, how do you make a redirect to the frontend website ?
I tried to do this, but it does nothing:
try {
// return res.status(200).json("recibido");
// Crea un objeto de preferencia y cargo los productos
let preference = {
"back_urls": {
"success": "https://www.tu-sitio/success",
"failure": "https://www.goggle.com",
"pending": "https://www.goggle.com"
},
"auto_return": "approved",
items // items son productos que se van a comprar
};
mercadopago.preferences
.create(preference)
.then(function (response) {
console.log(response.body);
// redirije a pagina de mercadopago
res.redirect(response.body.init_point);
})
.catch(function (error) {
console.log(error);
});
} catch (e) {
console.log(e);
return res.status(500).json("Internal server error");
}
This is the code where the redirect to the api is made so that it makes the purchase.
export const prueba = async (
req: Request,
res: Response
): Promise<Response> => {
try {
// Crea un objeto de preferencia
let preference = {
items: [
{
// obteniendo los valores del formulario enviados desde Angular
title: req.body.title.toString(),
unit_price: parseInt(req.body.total.toString()),
quantity: 1,
},
],
};
mercadopago.preferences
.create(preference)
.then(function (response) {
console.log(response.body);
// hace un redirect a pagina de mercadopago
res.redirect(response.body.init_point);
})
.catch(function (error) {
console.log(error);
});
} catch (e) {
console.log(e);
return res.status(500).json("Internal server error");
}
};
Ready the way to do it is with the
back_urls
Here is the documentation