Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds BE pay route with stripe #62

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions server/controllers/request.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Request = require("../models/Request");
const asyncHandler = require("express-async-handler");
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);

// @route GET /requests
// @desc Search for request of especific userId
Expand Down Expand Up @@ -59,3 +60,59 @@ exports.updateRequest = asyncHandler(async (req, res, next) => {
throw new Error("Invalid user Id data");
}
});

// @route PATCH /requests/:requestId/pay
// @desc Stripe pay route
// @access Private
xyecs marked this conversation as resolved.
Show resolved Hide resolved

exports.payRequest = asyncHandler(async (req, res) => {
const requestId = req.params.requestId;
const request = await Request.findById(requestId);
const { items } = req.body;
const todayDate = new Date();

/*TODO:
Eg.
const customer = await stripe.customers.create();
const ephemeralKey = await stripe.ephemeralKeys.create(
{customer: customer.id},
{apiVersion: '2020-08-27'
);
Need to add new customer and methods to save cc
but below should work for entering cc every time to pay
*/
xyecs marked this conversation as resolved.
Show resolved Hide resolved

const calculateOrderAmount = (items) => {
/*
TODO: Add something like following code to get line items
from frontend once integrated
xyecs marked this conversation as resolved.
Show resolved Hide resolved

Object.entries(items).forEach((entry) => {
const [key, value] = entry;
});*/
Comment on lines +90 to +92
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove unused code

return 1400;
};

if (todayDate > request.end && !request.paid) {
const paymentIntent = await stripe.paymentIntents.create({
amount: calculateOrderAmount(items),
currency: "cad",
});

const updatedRequest = await Request.findByIdAndUpdate(
requestId,
{ $set: { paid: true } },
{ new: true }
);

res.send({
success: updatedRequest,
clientSecret: paymentIntent.client_secret,
});
} else {
res.status(400);
throw new Error(
"Current date has to be after end date of request to pay and/or request has to be unpaid."
);
}
});
2 changes: 2 additions & 0 deletions server/routes/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ const {
getRequests,
postRequest,
updateRequest,
payRequest,
} = require("../controllers/request");

router.route("/").get(protect, getRequests);
router.route("/").post(protect, postRequest);
router.route("/:requestId").patch(protect, updateRequest);
router.route("/:requestId/pay").patch(payRequest);

module.exports = router;
3 changes: 2 additions & 1 deletion server/sample.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
JWT_SECRET=
MONGO_URI=
TEAM_NAMES=Bonnie,Sina, Kellen Bolger, juan, Gamaliel
STRIPE_SECRET_KEY=
TEAM_NAMES=Bonnie,Sina, Kellen Bolger, juan, Gamaliel