Setup callback routes
This commit is contained in:
parent
7defdeb153
commit
7707ab611d
5 changed files with 89 additions and 7 deletions
|
@ -8,7 +8,7 @@ module.exports = (sequelize, DataTypes) => {
|
||||||
Order.belongsToMany(models.Sandwich, {through: {model: models.SandwichOrder, unique: false}});
|
Order.belongsToMany(models.Sandwich, {through: {model: models.SandwichOrder, unique: false}});
|
||||||
Order.belongsTo(models.Department);
|
Order.belongsTo(models.Department);
|
||||||
Order.belongsTo(models.User);
|
Order.belongsTo(models.User);
|
||||||
Order.belongsTo(models.Payment, {foreignKey: "ShopOrderReference"});
|
Order.hasMany(models.Payment, {foreignKey: "ShopOrderReference"});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Order.init({
|
Order.init({
|
||||||
|
@ -28,6 +28,11 @@ module.exports = (sequelize, DataTypes) => {
|
||||||
price: {
|
price: {
|
||||||
type: DataTypes.FLOAT,
|
type: DataTypes.FLOAT,
|
||||||
allowNull: false
|
allowNull: false
|
||||||
|
},
|
||||||
|
paid: {
|
||||||
|
type: DataTypes.BOOLEAN,
|
||||||
|
allowNull: false,
|
||||||
|
defaultValue: false
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
sequelize,
|
sequelize,
|
||||||
|
|
|
@ -18,7 +18,7 @@ const {
|
||||||
module.exports = (sequelize, DataTypes) => {
|
module.exports = (sequelize, DataTypes) => {
|
||||||
class Payment extends Model {
|
class Payment extends Model {
|
||||||
static associate(models) {
|
static associate(models) {
|
||||||
Payment.hasOne(models.Order);
|
Payment.belongsTo(models.Order);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Payment.init({
|
Payment.init({
|
||||||
|
@ -31,6 +31,18 @@ module.exports = (sequelize, DataTypes) => {
|
||||||
type: DataTypes.DATE,
|
type: DataTypes.DATE,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
defaultValue: DataTypes.NOW
|
defaultValue: DataTypes.NOW
|
||||||
|
},
|
||||||
|
status: {
|
||||||
|
type: DataTypes.BOOLEAN
|
||||||
|
},
|
||||||
|
amount: {
|
||||||
|
type: DataTypes.INTEGER
|
||||||
|
},
|
||||||
|
discount: {
|
||||||
|
type: DataTypes.INTEGER
|
||||||
|
},
|
||||||
|
transactionUuid: {
|
||||||
|
type: DataTypes.UUID,
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
sequelize,
|
sequelize,
|
||||||
|
|
|
@ -2,7 +2,7 @@ let express = require("express");
|
||||||
let router = express.Router();
|
let router = express.Router();
|
||||||
let models = require("../models");
|
let models = require("../models");
|
||||||
let error = require("./utils/error");
|
let error = require("./utils/error");
|
||||||
let sendPayment = require("./utils/lyfPay").sendPayment;
|
let lyfPay = require("./utils/lyfPay");
|
||||||
|
|
||||||
|
|
||||||
router.post("/", async (req, res) => {
|
router.post("/", async (req, res) => {
|
||||||
|
@ -69,7 +69,15 @@ router.post("/", async (req, res) => {
|
||||||
throw e;
|
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;
|
module.exports = router;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
const crypto = require("crypto");
|
const crypto = require("crypto");
|
||||||
const models = require("../../models");
|
const models = require("../../models");
|
||||||
|
const error = require("./error");
|
||||||
|
|
||||||
|
|
||||||
function macCalculator(params, key) {
|
function macCalculator(params, key) {
|
||||||
|
@ -8,10 +9,10 @@ function macCalculator(params, key) {
|
||||||
|
|
||||||
async function sendPayment(req, res, order) {
|
async function sendPayment(req, res, order) {
|
||||||
let payment = await models.Payment.create();
|
let payment = await models.Payment.create();
|
||||||
await order.setPayment(payment);
|
await payment.setOrder(order);
|
||||||
await payment.reload();
|
await payment.reload();
|
||||||
|
|
||||||
let baseUrl = `${req.protocol}://${req.hostname}/order`;
|
let baseUrl = `https://${req.hostname}/order`;
|
||||||
let config = req.app.get("config").lyfPay;
|
let config = req.app.get("config").lyfPay;
|
||||||
let url = config.url + "/Payment.aspx?";
|
let url = config.url + "/Payment.aspx?";
|
||||||
|
|
||||||
|
@ -47,5 +48,47 @@ async function sendPayment(req, res, order) {
|
||||||
res.redirect(307, url);
|
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
|
||||||
|
};
|
||||||
|
|
14
views/order.pug
Normal file
14
views/order.pug
Normal file
|
@ -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
|
Reference in a new issue