From 6c67927e5ce858e56884f0ccad0cf167754cb4f0 Mon Sep 17 00:00:00 2001 From: flifloo Date: Fri, 11 Sep 2020 11:10:24 +0200 Subject: [PATCH] Setup retry route --- routes/order.js | 7 +++++++ routes/utils/lyfPay.js | 6 +++++- views/order.pug | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/routes/order.js b/routes/order.js index 1b0029e..053f874 100644 --- a/routes/order.js +++ b/routes/order.js @@ -78,6 +78,13 @@ router.post("/", async (req, res) => { res.render("order", {title: "SOD - Payment", state: "error"}); }).post("/callback", async (req, res) => { await lyfPay.checkPayment(req, res); +}).get("/retry", async (req, res) => { + let order = await models.Order.findByPk(req.session.lastOrder.id); + if (!order) + return error(req, res, "Can't retrieve last order", 400); + else + await lyfPay.sendPayment(req, res, order); + }); module.exports = router; diff --git a/routes/utils/lyfPay.js b/routes/utils/lyfPay.js index 1f2ec8f..e812bad 100644 --- a/routes/utils/lyfPay.js +++ b/routes/utils/lyfPay.js @@ -8,6 +8,9 @@ function macCalculator(params, key) { } 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(); @@ -45,6 +48,8 @@ async function sendPayment(req, res, order) { .map(k => encodeURIComponent(k) + "=" + encodeURIComponent(params[k])) .join("&"); + req.session.lastOrder = order; + res.redirect(307, url); } @@ -79,7 +84,6 @@ async function checkPayment(req, res) { await payment.save(); if (payment.status) { - console.log(payment); payment.Order.paid = payment.status; await payment.Order.save(); } diff --git a/views/order.pug b/views/order.pug index 8b527ac..b11f9ef 100644 --- a/views/order.pug +++ b/views/order.pug @@ -10,5 +10,5 @@ block content else if (state === "error") h2 An error occurred with the payment if (state !== "success") - a + a(href="/order/retry") button Retry