Skip to content

Commit

Permalink
Merge pull request dvjsharma#10 from Akashsah2003/master
Browse files Browse the repository at this point in the history
Payment
  • Loading branch information
dvjsharma authored Nov 25, 2023
2 parents 71f68d3 + d859f56 commit 28d8ce5
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 11 deletions.
9 changes: 9 additions & 0 deletions client/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 client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"react-burger-menu": "^3.0.9",
"react-dom": "^18.2.0",
"react-hot-toast": "^2.4.1",
"react-razorpay": "^2.0.1",
"react-redux": "^8.1.2",
"react-router-dom": "^6.16.0"
},
Expand Down
62 changes: 57 additions & 5 deletions client/src/pages/Cart/index.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
import React, { useEffect, useState } from "react";
import React, { useEffect, useState, useCallback } from "react";
import axios from "axios";
import Cartcard from "../../components/Cart-Card";
import useRazorpay from "react-razorpay";

const Cart = () => {
const [cartitems, setCartitems] = useState();
const [subtotal, setSubtotal] = useState(0);
const [Razorpay, isLoaded] = useRazorpay();

const handlePayment = useCallback(() => {

const options = {
key: "rzp_test_NuTUG6yrfovjY3",
amount: `${subtotal+110}00`,
currency: "INR",
name: "Acme Corp",
description: "Test Transaction",
image: "https://example.com/your_logo",
handler: (res) => {
console.log(res);
},
prefill: {
name: "Piyush Garg",
email: "[email protected]",
contact: "9999999999",
},
notes: {
address: "Razorpay Corporate Office",
},
theme: {
color: "#3399cc",
},
};

const rzpay = new Razorpay(options);
rzpay.open();
}, [subtotal]);

useEffect(() => {
if (isLoaded) {
handlePayment();
}
}, [isLoaded, handlePayment])


let toDisplay = [];
useEffect(() => {
const fetchcart = async () => {
Expand All @@ -24,6 +64,18 @@ const Cart = () => {
fetchcart();
}, [])

if (cartitems) {
let ntotal = 0;
for (let index = 0; index < cartitems.length; index++) {

ntotal = ntotal + parseInt(cartitems[index].price);
}
if (subtotal != ntotal)
{
setSubtotal(ntotal);
}
}

useEffect(() => {
if (cartitems)
{
Expand Down Expand Up @@ -153,25 +205,25 @@ const Cart = () => {
<div className="mt-6 h-full rounded-lg border bg-white p-6 shadow-md md:mt-0 md:w-1/3">
<div className="mb-2 flex justify-between">
<p className="text-gray-700">Subtotal</p>
<p className="text-gray-700">$129.99</p>
<p className="text-gray-700">{subtotal}</p>
</div>
<div className="flex justify-between">
<p className="text-gray-700">Shipping</p>
<p className="text-gray-700">$4.99</p>
<p className="text-gray-700">₹ 110</p>
</div>
<hr className="my-4" />
<div className="flex justify-between">
<p className="text-lg font-bold">Total</p>
<div className="">
<p className="mb-1 text-lg font-bold">
$134.98 USD
{subtotal + 110} INR
</p>
<p className="text-sm text-gray-700">
including VAT
</p>
</div>
</div>
<button className="mt-6 w-full rounded-md bg-blue-500 py-1.5 font-medium text-blue-50 hover:bg-blue-600">
<button className="mt-6 w-full rounded-md bg-blue-500 py-1.5 font-medium text-blue-50 hover:bg-blue-600" onClick={handlePayment}>
Check out
</button>
</div>
Expand Down
5 changes: 1 addition & 4 deletions client/src/pages/Product/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ const Product = () => {
// console.log(data)
try {
const { data } = await axios.post(
"http://localhost:3000/api/users/cart/add",
"http://localhost:3000/api/cart/add",
{
shoeImage,
shoeName,
brand,
pId,
price,
size: formData,
Expand Down
16 changes: 15 additions & 1 deletion server/controllers/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,19 @@ const getcart = (req, res) => {
});
}

const addcart=(req, res) => {
const {pId, price, size} = req.body;
con.query(`INSERT INTO cart VALUES (${pId}, '${req.user["email"]}', ${size}, 1)`, (err, result) => {
if (err)
{
throw err;
}
return res.status(200).json({
success: true,
message: "Added to cart"
})
});
}


export {getcart};
export {getcart, addcart};
4 changes: 3 additions & 1 deletion server/routes/cart.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import express from "express";
import { getcart } from "../controllers/cart.js";
import { getcart, addcart } from "../controllers/cart.js";
import { isAuthenticated } from "../middlewares/auth.js";

const router = express.Router();


router.get('/', isAuthenticated, getcart);

router.post('/add', isAuthenticated, addcart);


export default router;

0 comments on commit 28d8ce5

Please sign in to comment.