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

122 lines
3.8 KiB
JavaScript
Raw Permalink Normal View History

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;
2020-09-20 12:35:02 +02:00
let url = "";
let params = {};
let additionalData = JSON.stringify({
"callBackUrl": baseUrl+"/callback",
"callBackEmail":config.warningEmail
});
2020-09-10 19:18:42 +02:00
2020-09-20 12:35:02 +02:00
if (req.body.payment === "lyfPay") {
url = config.url + "/Payment.aspx?";
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: additionalData,
enforcedIdentification: false
};
} else if (req.body.payment === "creditCard") {
url = config.url + "/PaymentCb.aspx?";
params = {
lang: "fr",
posUuid: config.posUuid,
shopReference: payment.shopReference,
shopOrderReference: order.id,
deliveryFeesAmount: 0,
amount: order.price*100,
currency: "EUR",
onSuccess: baseUrl + "/success",
onError: baseUrl+"/error",
additionalData: additionalData,
callbackRequired: true,
mode: "IMMEDIATE"
};
}
2020-09-10 19:18:42 +02:00
params.mac = macCalculator(params, config.secureKey);
params.additionalDataEncoded = Buffer.from(params.additionalData).toString("base64");
delete params.additionalData;
2020-09-20 12:35:02 +02:00
if (req.body.payment === "creditCard")
params.version = "v2.0";
2020-09-10 19:18:42 +02:00
url += Object.keys(params)
.map(k => encodeURIComponent(k) + "=" + encodeURIComponent(params[k]))
.join("&");
req.session.lastOrder = [order, req.body.payment];
2020-09-11 11:10:24 +02:00
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
};