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

stripe settings star #5

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require('express-async-errors');
const path = require('path');
const express = require('express');
const app = express();

const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
const morgan = require('morgan');
const cookieParser = require('cookie-parser'); //to get access to the cookie received from the browser
const fileUpload = require('express-fileupload');
Expand Down
23 changes: 23 additions & 0 deletions controllers/orderController.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,28 @@ const getCurrentUserOrders = async (req, res) => {
res.status(StatusCodes.OK).json({ orders, count: orders.length });
};

const payOrder= async (req, res) => {
const { amount, currency, description, token } = req.body;
checkPermissions(req.user, order.user);

try {
const charge = await stripe.charges.create({
amount,
currency,
description,
source: token,
});

// Handle successful payment
res.json({ success: true, charge });
} catch (error) {
// Handle payment failure
res.json({ success: false, error: error.message });
}

};


const updatePaymentStatus = async (req, res) => {
const { id: orderId } = req.params;
const { paymentIntentId } = req.body;
Expand All @@ -215,4 +237,5 @@ module.exports = {
getCurrentUserOrders,
createOrder,
updatePaymentStatus ,
payOrder,
};
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"jsonwebtoken": "^8.5.1",
"mongoose": "^6.0.8",
"morgan": "^1.10.0",
"stripe": "^12.12.0",
"validator": "^13.6.0",
"xss-clean": "^0.1.1"
},
Expand Down
5 changes: 3 additions & 2 deletions routes/orderRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const {
getSingleOrder,
getCurrentUserOrders,
createOrder,
updatePaymentStatus ,
updatePaymentStatus,
payOrder,
} = require('../controllers/orderController');

router
Expand All @@ -27,5 +28,5 @@ router

router
.route('/:id/pay')
.patch(authenticateUser, updatePaymentStatus );
.patch(authenticateUser, payOrder, updatePaymentStatus );
module.exports = router;