Archived
1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
SOD/routes/utils/lyfPay.js
2020-09-11 11:10:24 +02:00

98 lines
2.9 KiB
JavaScript

const crypto = require("crypto");
const models = require("../../models");
const error = require("./error");
function macCalculator(params, key) {
return crypto.createHmac("sha1", key).update(Object.values(params).join("*")).digest("hex");
}
async function sendPayment(req, res, order) {
if (order.paid)
return error(req, res, "Order already paid !", 400);
let payment = await models.Payment.create();
await payment.setOrder(order);
await payment.reload();
let baseUrl = `https://${req.hostname}/order`;
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("&");
req.session.lastOrder = order;
res.redirect(307, url);
}
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);
}
module.exports = {
sendPayment: sendPayment,
checkPayment: checkPayment
};