Created
June 8, 2021 21:54
-
-
Save HectorBlisS/de67439e15eadd22c20b3e099165f760 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 conekta = require("conekta"); | |
//conekta.api_key = process.env.ENV === 'production' ? process.env.CONEKTA_KEY : process.env.CONEKTA_KEY_DEV | |
conekta.api_key = process.env.CONEKTA_KEY; | |
conekta.api_version = "2.0.0"; | |
conekta.locale = "es"; | |
controller.pay = (req, res) => { | |
//conekta payment | |
const { conektaToken, plazo, application, cupon } = req.body; | |
//let used = { [req.user._id]: true }; | |
const user = req.user; | |
// App.findById(application._id) | |
// .then((elapp) => { | |
const chargeObj = { | |
payment_method: { | |
type: "card", | |
token_id: conektaToken, | |
}, | |
// }; | |
//discounts | |
//let discount = 0; | |
// elapp.cost = elapp.cost * 100; | |
// if (plazo === "contado" && !cupon) { | |
// discount = elapp.cost * 0.1; | |
// } else if ((plazo === "contado" || plazo !== "contado") && cupon) { | |
// discount = (elapp.cost * cupon.value) / 100; | |
// } else { | |
// discount = 0; | |
// } | |
if (plazo !== "contado") | |
chargeObj.payment_method.monthly_installments = parseInt(plazo); | |
const conektaObject = { | |
currency: "MXN", | |
customer_info: { | |
name: "Nombre"//elapp.name, | |
phone: "7777777777"// elapp.tel, | |
email: "[email protected]" // elapp.email, | |
}, | |
line_items: [ | |
{ | |
name: "product name" //elapp.course, | |
unit_price: 10000 //elapp.cost - discount, | |
quantity: 1, | |
}, | |
], | |
charges: [chargeObj], | |
}; | |
conekta.Order.create(conektaObject, function (err, order) { | |
if (err) { | |
console.log("conektaerror", err); | |
return res.status(400).json(err); | |
} | |
//App.findByIdAndUpdate( | |
//elapp._id, | |
//{ $set: { paid: true } }, | |
//{ new: true } | |
//) | |
//.then((application) => { | |
//console.log(order.toObject()) | |
//cancelamos el cupon | |
//useCupon(cupon, used); | |
// | |
return res | |
.status(200) | |
.json({ application, order: order.toObject() }); | |
//}) | |
//.catch((e) => { | |
//console.log(e); | |
// return res.status(400).json(e); | |
//}); | |
}); | |
}) | |
.catch((e) => res.status(400).json(e)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment