diff --git a/controller/connect.js b/controller/connect.js index d4be4d0..33eb760 100644 --- a/controller/connect.js +++ b/controller/connect.js @@ -91,7 +91,7 @@ const mrUpdate = (collectionName, input, callFunc) => { { $set: data }, (findErr, addResult) => { if (findErr) throw findErr; - callFunc({...input.selector}); + callFunc({ ...input.selector }); } ); client.close(); diff --git a/controller/payment.js b/controller/payment.js index 679f5cf..9dbcc4f 100644 --- a/controller/payment.js +++ b/controller/payment.js @@ -2,6 +2,8 @@ const dateTime = require("node-datetime"); const serverdateTime = dateTime.create(); const { v4: uuidv4 } = require("uuid"); +const gatewayGetFactory = require("../factory"); + const { mrInitCollections, mrFindAll, @@ -10,104 +12,18 @@ const { } = require("./connect"); const userController = require("./user"); class PaymentController { + newGateway = (req) => { + const { gateWayName } = req.body; + return gatewayGetFactory.createGateway(gateWayName); + }; postPayRequest = (req, res) => { - const { - orderId, - callBackUrl, - amount, - terminalId, - localDate, - localTime, - payerId, - additionalData, - userName, - userPassword, - } = req.body; - - mrFindAll("users", (data) => { - const user = userController.getOneUser(data, { - userName, - userPassword, - terminalId, - }); - if (!user) { - return res.status(404).json({ - success: "false", - message: "Username or userPassword incorrect", - data: {}, - }); - } - const inputData = { - orderId, - callBackUrl, - amount, - terminalId, - localDate, - localTime, - payerId, - additionalData, - refId: uuidv4(), - saleOrderId: orderId, - saleReferenceId: Math.floor(Math.random() * 10000000), - }; - mrInsertOne("transactions", inputData, (data) => { - return res.status(200).json({ - success: "true", - message: "Create refId successfully", - data: { - resCode: 0, - refId: data.refId, - }, - }); - }); - }); + this.newGateway(req).postPayRequest(req, res); }; postPayAction = (req, res) => { - const { refId } = req.body; - - mrFindAll("transactions", (data) => { - const transaction = data.find((item) => { - return item.refId === refId; - }); - if (!transaction) { - return res.status(404).json({ - success: "false", - message: "transaction not found", - data: {}, - }); - } - return res.status(200).render("payAction", { - success: "true", - message: "Please select your request", - data: { - title: "Please select your request", - result: transaction, - }, - }); - }); + this.newGateway(req).postPayAction(req, res); }; postCompletePayment = (req, res) => { - const { refId, resCode } = req.body; - const inputDataUpdate = { - selector: { refId }, - data: { resCode }, - }; - mrUpdate("transactions", inputDataUpdate, (transData) => { - mrFindAll("transactions", (data) => { - const transaction = data.find((item) => { - if (item.refId === transData.refId) { - return res.status(200).render("completePayment", { - success: "true", - message: "Complete payment", - data: { - title: "Complete payment", - result: item, - }, - }); - } - }); - }); - }); + this.newGateway(req).postCompletePayment(req, res); }; } diff --git a/factory/bPardakht.js b/factory/bPardakht.js index 9740eb9..d0d79cb 100644 --- a/factory/bPardakht.js +++ b/factory/bPardakht.js @@ -10,7 +10,7 @@ const { } = require("../controller/connect"); const userController = require("../controller/user"); class BPardakhtController { - postPayRequest = (req, res) => { + postPayRequest = (req,res) => { const { orderId, callBackUrl, @@ -22,6 +22,7 @@ class BPardakhtController { additionalData, userName, userPassword, + gateWayName } = req.body; mrFindAll("users", (data) => { @@ -46,6 +47,7 @@ class BPardakhtController { localTime, payerId, additionalData, + gateWayName, refId: uuidv4(), saleOrderId: orderId, saleReferenceId: Math.floor(Math.random() * 10000000), @@ -57,6 +59,7 @@ class BPardakhtController { data: { resCode: 0, refId: data.refId, + gateWayName: data.gateWayName }, }); }); diff --git a/routes/index.js b/routes/index.js index 8312090..ab703b9 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,9 +1,7 @@ const express = require("express"); const dotenv = require("dotenv"); const userController = require("../controller/user"); - -const gatewayGetFactory = require("../factory"); -const bPardakht = gatewayGetFactory.createGateway("BPardakht"); +const paymentController = require("../controller/payment"); const router = express.Router(); dotenv.config(); @@ -21,12 +19,12 @@ router.get(`${process.env.API_URL}/transactions`, userController.getAllTransacti router.post(`${process.env.API_URL}/login`, userController.loginUser); // Post Pay request from user website -router.post(`${process.env.API_URL}/payRequest`, bPardakht.postPayRequest); +router.post(`${process.env.API_URL}/payRequest`, paymentController.postPayRequest); // Post Pay Action from user => success or failed -router.post(`${process.env.API_URL}/payAction`, bPardakht.postPayAction); +router.post(`${process.env.API_URL}/payAction`, paymentController.postPayAction); // Auto redirect to user website -router.post(`${process.env.API_URL}/completePayment`, bPardakht.postCompletePayment); +router.post(`${process.env.API_URL}/completePayment`, paymentController.postCompletePayment); module.exports = router; diff --git a/views/payAction.jade b/views/payAction.jade index 9947c9f..97307da 100644 --- a/views/payAction.jade +++ b/views/payAction.jade @@ -15,10 +15,12 @@ block content .col form(action="/completePayment",method="post", name="bank") input(type="hidden", name="refId", value="#{result.refId}") + input(type="hidden", name="gateWayName", value="#{result.gateWayName}") input(type="hidden", name="resCode", value="0") button.btn.btn-success(type="submit") success .col form(action="/completePayment",method="post", name="bank") input(type="hidden", name="refId", value="#{result.refId}") + input(type="hidden", name="gateWayName", value="#{result.gateWayName}") input(type="hidden", name="resCode", value="17") button.btn.btn-danger(type="submit") failed