From b10424d158e80d90715a6d57a0cd3ae75f7f26f6 Mon Sep 17 00:00:00 2001 From: flifloo Date: Sun, 20 Sep 2020 12:35:02 +0200 Subject: [PATCH] Add credit card payment method --- locales/en.json | 4 ++- locales/fr.json | 4 ++- public/stylesheets/style.css | 7 ++-- routes/order.js | 3 ++ routes/utils/lyfPay.js | 65 ++++++++++++++++++++++++------------ views/index.pug | 7 ++++ 6 files changed, 64 insertions(+), 26 deletions(-) diff --git a/locales/en.json b/locales/en.json index 0c7ca11..2889d7c 100644 --- a/locales/en.json +++ b/locales/en.json @@ -29,7 +29,9 @@ "index": { "welcome": "Welcome to Sandwiches Order Doua", "day": "Day", - "pay": "Pay" + "pay": "Pay", + "payment": "Payment method", + "creditCard": "Credit card" }, "login": { "title": "Login", diff --git a/locales/fr.json b/locales/fr.json index 874426e..3e19a57 100644 --- a/locales/fr.json +++ b/locales/fr.json @@ -29,7 +29,9 @@ "index": { "welcome": "Bienvenue sur Sandwiches Order Doua", "day": "Jour", - "pay": "Payer" + "pay": "Payer", + "payment": "Mode de paiement", + "creditCard": "CB" }, "login": { "title": "Se connecter", diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index 161d141..c1f2317 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -54,9 +54,6 @@ a { margin: auto; width: 100%; font-size: 100%; -} - -.field input { height: 2.5vh; border-top: none; border-left: none; @@ -64,6 +61,10 @@ a { border-bottom: 1px gray solid; } +.field input[type="radio"], .field input[type="checkbox"] { + box-shadow: none; +} + .field input[type="submit"] { height: 4vh; border: none; diff --git a/routes/order.js b/routes/order.js index c8a27a3..9ad4754 100644 --- a/routes/order.js +++ b/routes/order.js @@ -7,6 +7,9 @@ let addOrder = require("./utils/addOrder"); router.post("/", async (req, res) => { + if (!req.body.payment || ["lyfPay", "creditCard"].indexOf(req.body.payment) < 0) + return error(req, res, "Missing args !", 400); + let order = await addOrder(req, res, { department: req.body.department, firstName: req.body.firstName, diff --git a/routes/utils/lyfPay.js b/routes/utils/lyfPay.js index e812bad..4f87145 100644 --- a/routes/utils/lyfPay.js +++ b/routes/utils/lyfPay.js @@ -17,32 +17,55 @@ async function sendPayment(req, res, order) { let baseUrl = `https://${req.hostname}/order`; let config = req.app.get("config").lyfPay; - let url = config.url + "/Payment.aspx?"; + let url = ""; + let params = {}; + let additionalData = JSON.stringify({ + "callBackUrl": baseUrl+"/callback", + "callBackEmail":config.warningEmail + }); - 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 - }; + 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" + }; + } params.mac = macCalculator(params, config.secureKey); params.additionalDataEncoded = Buffer.from(params.additionalData).toString("base64"); params.additionalData = undefined; + if (req.body.payment === "creditCard") + params.version = "v2.0"; url += Object.keys(params) .map(k => encodeURIComponent(k) + "=" + encodeURIComponent(params[k])) diff --git a/views/index.pug b/views/index.pug index 33298b1..57752db 100644 --- a/views/index.pug +++ b/views/index.pug @@ -32,6 +32,13 @@ block content a#add-order + a#remove-order.hide - + h3=__("index.payment") + div.field.buttons + label(for="lyf_pay") LyfPay + input#lyf_pay(type="radio" name="payment" value="lyfPay" checked) + label(for="credit_card")=__("index.creditCard") + input#credit_card(type="radio" name="payment" value="creditCard") + div.field +submit(value=__("index.pay"))