From 7707ab611d2576d651aa54c84c157a3cd7d316f6 Mon Sep 17 00:00:00 2001 From: flifloo Date: Fri, 11 Sep 2020 10:58:12 +0200 Subject: [PATCH] Setup callback routes --- models/order.js | 7 +++++- models/payment.js | 14 +++++++++++- routes/order.js | 12 +++++++++-- routes/utils/lyfPay.js | 49 +++++++++++++++++++++++++++++++++++++++--- views/order.pug | 14 ++++++++++++ 5 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 views/order.pug diff --git a/models/order.js b/models/order.js index 9426281..08d36cc 100644 --- a/models/order.js +++ b/models/order.js @@ -8,7 +8,7 @@ module.exports = (sequelize, DataTypes) => { Order.belongsToMany(models.Sandwich, {through: {model: models.SandwichOrder, unique: false}}); Order.belongsTo(models.Department); Order.belongsTo(models.User); - Order.belongsTo(models.Payment, {foreignKey: "ShopOrderReference"}); + Order.hasMany(models.Payment, {foreignKey: "ShopOrderReference"}); } } Order.init({ @@ -28,6 +28,11 @@ module.exports = (sequelize, DataTypes) => { price: { type: DataTypes.FLOAT, allowNull: false + }, + paid: { + type: DataTypes.BOOLEAN, + allowNull: false, + defaultValue: false } }, { sequelize, diff --git a/models/payment.js b/models/payment.js index 713c420..30537cd 100644 --- a/models/payment.js +++ b/models/payment.js @@ -18,7 +18,7 @@ const { module.exports = (sequelize, DataTypes) => { class Payment extends Model { static associate(models) { - Payment.hasOne(models.Order); + Payment.belongsTo(models.Order); } } Payment.init({ @@ -31,6 +31,18 @@ module.exports = (sequelize, DataTypes) => { type: DataTypes.DATE, allowNull: false, defaultValue: DataTypes.NOW + }, + status: { + type: DataTypes.BOOLEAN + }, + amount: { + type: DataTypes.INTEGER + }, + discount: { + type: DataTypes.INTEGER + }, + transactionUuid: { + type: DataTypes.UUID, } }, { sequelize, diff --git a/routes/order.js b/routes/order.js index 2c7a3a1..1b0029e 100644 --- a/routes/order.js +++ b/routes/order.js @@ -2,7 +2,7 @@ let express = require("express"); let router = express.Router(); let models = require("../models"); let error = require("./utils/error"); -let sendPayment = require("./utils/lyfPay").sendPayment; +let lyfPay = require("./utils/lyfPay"); router.post("/", async (req, res) => { @@ -69,7 +69,15 @@ router.post("/", async (req, res) => { throw e; } - await sendPayment(req, res, order); + await lyfPay.sendPayment(req, res, order); +}).get("/success", (req, res) => { + res.render("order", {title: "SOD - Payment", state: "success"}); +}).get("/cancel", (req, res) => { + res.render("order", {title: "SOD - Payment", state: "cancel"}); +}).get("/error", (req, res) => { + res.render("order", {title: "SOD - Payment", state: "error"}); +}).post("/callback", async (req, res) => { + await lyfPay.checkPayment(req, res); }); module.exports = router; diff --git a/routes/utils/lyfPay.js b/routes/utils/lyfPay.js index 6d16f89..1f2ec8f 100644 --- a/routes/utils/lyfPay.js +++ b/routes/utils/lyfPay.js @@ -1,5 +1,6 @@ const crypto = require("crypto"); const models = require("../../models"); +const error = require("./error"); function macCalculator(params, key) { @@ -8,10 +9,10 @@ function macCalculator(params, key) { async function sendPayment(req, res, order) { let payment = await models.Payment.create(); - await order.setPayment(payment); + await payment.setOrder(order); await payment.reload(); - let baseUrl = `${req.protocol}://${req.hostname}/order`; + let baseUrl = `https://${req.hostname}/order`; let config = req.app.get("config").lyfPay; let url = config.url + "/Payment.aspx?"; @@ -47,5 +48,47 @@ async function sendPayment(req, res, order) { res.redirect(307, url); } +async function checkPayment(req, res) { + let config = req.app.get("config").lyfPay; -module.exports = {sendPayment: sendPayment}; + 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) { + console.log(payment); + payment.Order.paid = payment.status; + await payment.Order.save(); + } + + res.send("OK", 200); +} + + +module.exports = { + sendPayment: sendPayment, + checkPayment: checkPayment +}; diff --git a/views/order.pug b/views/order.pug new file mode 100644 index 0000000..8b527ac --- /dev/null +++ b/views/order.pug @@ -0,0 +1,14 @@ +extends layout + +block content + div.card + h1 Payment + if (state === "success") + h2 Payment successful ! + else if (state === "cancel") + h2 Payment canceled + else if (state === "error") + h2 An error occurred with the payment + if (state !== "success") + a + button Retry