2020-09-10 19:18:42 +02:00
|
|
|
const crypto = require("crypto");
|
|
|
|
const models = require("../../models");
|
2020-09-11 10:58:12 +02:00
|
|
|
const error = require("./error");
|
2020-09-10 19:18:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
function macCalculator(params, key) {
|
|
|
|
return crypto.createHmac("sha1", key).update(Object.values(params).join("*")).digest("hex");
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sendPayment(req, res, order) {
|
2020-09-11 11:10:24 +02:00
|
|
|
if (order.paid)
|
|
|
|
return error(req, res, "Order already paid !", 400);
|
|
|
|
|
2020-09-10 19:18:42 +02:00
|
|
|
let payment = await models.Payment.create();
|
2020-09-11 10:58:12 +02:00
|
|
|
await payment.setOrder(order);
|
2020-09-10 19:18:42 +02:00
|
|
|
await payment.reload();
|
|
|
|
|
2020-09-11 10:58:12 +02:00
|
|
|
let baseUrl = `https://${req.hostname}/order`;
|
2020-09-10 19:18:42 +02:00
|
|
|
let config = req.app.get("config").lyfPay;
|
|
|
|
let url = config.url + "/Payment.aspx?";
|
|
|
|
|
|
|
|
let params = {
|
|
|
|
lang: "fr",
|
|
|
|
version: "v2.0",
|
|
|
|
timestamp: Math.floor(payment.date/1000),
|
|
|
|
posUuid: config.posUuid,
|
|
|
|
shopReference: payment.shopReference,
|
|
|
|
shopOrderReference: order.id,
|
|
|
|
deliveryFeesAmount: 0,
|
|
|
|
amount: order.price*100,
|
|
|
|
currency: "EUR",
|
|
|
|
mode: "IMMEDIATE",
|
|
|
|
onSuccess: baseUrl + "/success",
|
|
|
|
onCancel: baseUrl+"/cancel",
|
|
|
|
onError: baseUrl+"/error",
|
|
|
|
additionalData: JSON.stringify({
|
|
|
|
"callBackUrl": baseUrl+"/callback",
|
|
|
|
"callBackEmail":config.warningEmail
|
|
|
|
}),
|
|
|
|
enforcedIdentification: false
|
|
|
|
};
|
|
|
|
|
|
|
|
params.mac = macCalculator(params, config.secureKey);
|
|
|
|
params.additionalDataEncoded = Buffer.from(params.additionalData).toString("base64");
|
|
|
|
params.additionalData = undefined;
|
|
|
|
|
|
|
|
url += Object.keys(params)
|
|
|
|
.map(k => encodeURIComponent(k) + "=" + encodeURIComponent(params[k]))
|
|
|
|
.join("&");
|
|
|
|
|
2020-09-11 11:10:24 +02:00
|
|
|
req.session.lastOrder = order;
|
|
|
|
|
2020-09-10 19:18:42 +02:00
|
|
|
res.redirect(307, url);
|
|
|
|
}
|
|
|
|
|
2020-09-11 10:58:12 +02:00
|
|
|
async function checkPayment(req, res) {
|
|
|
|
let config = req.app.get("config").lyfPay;
|
|
|
|
|
|
|
|
let params = {
|
|
|
|
posUuid: req.body.posUuid,
|
|
|
|
shopReference: req.body.shopReference,
|
|
|
|
shopOrderReference: req.body.shopOrderReference,
|
|
|
|
amount: req.body.amount,
|
|
|
|
discount: req.body.discount,
|
|
|
|
currency: req.body.currency,
|
|
|
|
status: req.body.status,
|
|
|
|
creationDate: req.body.creationDate,
|
|
|
|
transactionUuid: req.body.transactionUuid,
|
|
|
|
additionalData: req.body.additionalData,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (macCalculator(params, config.secureKey).toUpperCase() !== req.body.mac)
|
|
|
|
return error(req, res, "Invalid MAC", 400);
|
|
|
|
|
|
|
|
let payment = await models.Payment.findByPk(params.shopReference, {include: models.Order});
|
|
|
|
if (!payment)
|
|
|
|
return error(req, res, "Invalid shopReference !", 400);
|
|
|
|
|
|
|
|
payment.amount = params.amount;
|
|
|
|
payment.discount = params.discount;
|
|
|
|
payment.status = params.status === "VALIDATED";
|
|
|
|
payment.date = params.creationDate;
|
|
|
|
payment.transactionUuid = params.transactionUuid;
|
|
|
|
await payment.save();
|
|
|
|
|
|
|
|
if (payment.status) {
|
|
|
|
payment.Order.paid = payment.status;
|
|
|
|
await payment.Order.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
res.send("OK", 200);
|
|
|
|
}
|
|
|
|
|
2020-09-10 19:18:42 +02:00
|
|
|
|
2020-09-11 10:58:12 +02:00
|
|
|
module.exports = {
|
|
|
|
sendPayment: sendPayment,
|
|
|
|
checkPayment: checkPayment
|
|
|
|
};
|