Created
October 20, 2019 17:46
-
-
Save AlissonAp/7c3ad0148f2fc250e013aeeb68a5ac8b to your computer and use it in GitHub Desktop.
Payments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MercadoPago = require('mercadopago'); | |
const getFullUrl = (req) =>{ | |
const url = req.protocol + '://' + req.get('host'); | |
console.log(url) | |
return url; | |
} | |
module.exports = { | |
async checkout(req, res){ | |
console.log(process.env) | |
MercadoPago.configure({ | |
sandbox: process.env.SANDBOX == 'true' ? true : false, | |
access_token: process.env.MP_ACCESS_TOKEN | |
}); | |
const { id, email, description, amount } = req.params; | |
//Create purchase item object template | |
const purchaseOrder = { | |
items: [ | |
item = { | |
id: id, | |
title: description, | |
description : description, | |
quantity: 1, | |
currency_id: 'BRL', | |
unit_price: parseFloat(amount) | |
} | |
], | |
payer : { | |
email: email | |
}, | |
auto_return : "all", | |
external_reference : id, | |
back_urls : { | |
success : getFullUrl(req) + "/payments/success", | |
pending : getFullUrl(req) + "/payments/pending", | |
failure : getFullUrl(req) + "/payments/failure", | |
} | |
} | |
//Generate init_point to checkout | |
try { | |
const preference = await MercadoPago.preferences.create(purchaseOrder); | |
return res.redirect(`${preference.body.init_point}`); | |
}catch(err){ | |
return res.send(err.message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment