Skip to content

Commit

Permalink
feat: Enable only logged-in users to place orders
Browse files Browse the repository at this point in the history
  • Loading branch information
builtbysuraj committed Feb 13, 2024
1 parent 8c07eea commit 3fbf9bb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
4 changes: 3 additions & 1 deletion client/src/pages/cart/components/place-order/PlaceOrder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ declare global {
}
}

axios.defaults.withCredentials = true

export default function PlaceOrder() {
const cartData = useAppSelector((state) => state.cart)
const serverStatus = useServerStatus()
Expand All @@ -36,7 +38,7 @@ export default function PlaceOrder() {
currency: 'INR',
name: 'RazorPay',
description: 'Secure payment through RazorPay',
// image: 'https://avatars.githubusercontent.com/u/25058652?v=4',
// image: '',
order_id: order.id,
callback_url: 'http://localhost:5000/api/v1/paymentverification',
notes: {
Expand Down
2 changes: 2 additions & 0 deletions server/src/controller/payment/payment.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Payment } from '../../model/payment.model'
export const paymentVerification = async (req, res) => {
const { razorpay_order_id, razorpay_payment_id, razorpay_signature } =
req.body
const userId = req.user.id

const body = razorpay_order_id + '|' + razorpay_payment_id

Expand All @@ -21,6 +22,7 @@ export const paymentVerification = async (req, res) => {
razorpay_order_id,
razorpay_payment_id,
razorpay_signature,
user: userId,
})

res.redirect(
Expand Down
5 changes: 5 additions & 0 deletions server/src/model/payment.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const paymentSchema = new mongoose.Schema(
type: String,
required: true,
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
},
{ timestamps: true }
)
Expand Down
7 changes: 5 additions & 2 deletions server/src/routes/payment.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import {
paymentVerification,
} from '../controller/payment/payment.controller'
import { verifyPayment } from '../controller/payment/verifyPayment.controller'
import { verifyToken } from '../middleware/verifyToken.middleware'

const paymentRoute = Router()

paymentRoute.route('/checkout').post(checkout)
paymentRoute.route('/paymentverification').post(paymentVerification)
paymentRoute.route('/checkout').post(verifyToken, checkout)
paymentRoute
.route('/paymentverification')
.post(verifyToken, paymentVerification)
paymentRoute.route('/verify-payment').get(verifyPayment)
paymentRoute.route('/get-key').get(getKey)

Expand Down

0 comments on commit 3fbf9bb

Please sign in to comment.