Skip to content

Commit

Permalink
Add Factory pattern for behPardakht
Browse files Browse the repository at this point in the history
  • Loading branch information
MostafaRastegar committed Jun 21, 2020
1 parent a115c70 commit 80e67f4
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 101 deletions.
2 changes: 1 addition & 1 deletion controller/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
102 changes: 9 additions & 93 deletions controller/payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
};
}

Expand Down
5 changes: 4 additions & 1 deletion factory/bPardakht.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {
} = require("../controller/connect");
const userController = require("../controller/user");
class BPardakhtController {
postPayRequest = (req, res) => {
postPayRequest = (req,res) => {
const {
orderId,
callBackUrl,
Expand All @@ -22,6 +22,7 @@ class BPardakhtController {
additionalData,
userName,
userPassword,
gateWayName
} = req.body;

mrFindAll("users", (data) => {
Expand All @@ -46,6 +47,7 @@ class BPardakhtController {
localTime,
payerId,
additionalData,
gateWayName,
refId: uuidv4(),
saleOrderId: orderId,
saleReferenceId: Math.floor(Math.random() * 10000000),
Expand All @@ -57,6 +59,7 @@ class BPardakhtController {
data: {
resCode: 0,
refId: data.refId,
gateWayName: data.gateWayName
},
});
});
Expand Down
10 changes: 4 additions & 6 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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;
2 changes: 2 additions & 0 deletions views/payAction.jade
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 80e67f4

Please sign in to comment.