refactor: resolve payment iossue
This commit is contained in:
@@ -8,7 +8,7 @@ export default async function handler(req, res) {
|
|||||||
const body = razorpay_order_id + "|" + razorpay_payment_id;
|
const body = razorpay_order_id + "|" + razorpay_payment_id;
|
||||||
|
|
||||||
const expectedSignature = crypto
|
const expectedSignature = crypto
|
||||||
.createHmac("sha256", "8CTcXrodJqKQ3cfmic84Ffdl")
|
.createHmac("sha256", "439mV3nOC1nCRiruq4xcf5gP")
|
||||||
.update(body.toString())
|
.update(body.toString())
|
||||||
.digest("hex");
|
.digest("hex");
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
import React, { useContext, useState, useEffect } from "react";
|
import React, { useContext, useState, useEffect } from "react";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { Menu, ShoppingBag, UserRound } from "lucide-react";
|
import { Menu, ShoppingBag, UserRound } from "lucide-react";
|
||||||
|
import authAxios, { backendUrl } from "@/utils/axios";
|
||||||
import { IoMdClose } from "react-icons/io";
|
import { IoMdClose } from "react-icons/io";
|
||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
import SearchComponent from "../search/searchComponent";
|
import SearchComponent from "../search/searchComponent";
|
||||||
@@ -16,6 +17,7 @@ const Navbar = () => {
|
|||||||
const [isScrolled, setIsScrolled] = useState(false);
|
const [isScrolled, setIsScrolled] = useState(false);
|
||||||
const { category } = useContext(ProductContext);
|
const { category } = useContext(ProductContext);
|
||||||
const { token } = useContext(MainContext);
|
const { token } = useContext(MainContext);
|
||||||
|
const [cartItemCount, setCartItemCount] = useState(0);
|
||||||
const { selectedCurrency, setSelectedCurrency, SUPPORTED_CURRENCIES } =
|
const { selectedCurrency, setSelectedCurrency, SUPPORTED_CURRENCIES } =
|
||||||
useCurrency();
|
useCurrency();
|
||||||
|
|
||||||
@@ -30,6 +32,49 @@ const Navbar = () => {
|
|||||||
return () => window.removeEventListener("scroll", handleScroll);
|
return () => window.removeEventListener("scroll", handleScroll);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const getCart = async () => {
|
||||||
|
try {
|
||||||
|
const response = await authAxios.get("/orders/cart/");
|
||||||
|
const cartData = response.data;
|
||||||
|
console.log(cartData)
|
||||||
|
console.log(cartData.length)
|
||||||
|
|
||||||
|
// Calculate total items in cart
|
||||||
|
if (cartData && cartData.length > 0 && cartData[0].items) {
|
||||||
|
const totalItems = cartData[0].items.reduce((sum, item) => sum + item.quantity, 0);
|
||||||
|
setCartItemCount(totalItems);
|
||||||
|
} else {
|
||||||
|
setCartItemCount(0);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching cart:", error);
|
||||||
|
setCartItemCount(0);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Fetch cart data when component mounts and when token changes
|
||||||
|
useEffect(() => {
|
||||||
|
if (token) {
|
||||||
|
getCart();
|
||||||
|
} else {
|
||||||
|
setCartItemCount(0);
|
||||||
|
}
|
||||||
|
}, [token]);
|
||||||
|
|
||||||
|
// Set up an interval to periodically check for cart updates
|
||||||
|
useEffect(() => {
|
||||||
|
if (token) {
|
||||||
|
// Initial fetch
|
||||||
|
getCart();
|
||||||
|
|
||||||
|
// Set up interval to check for updates every 3 seconds
|
||||||
|
const intervalId = setInterval(getCart, 3000);
|
||||||
|
|
||||||
|
// Clean up interval on component unmount
|
||||||
|
return () => clearInterval(intervalId);
|
||||||
|
}
|
||||||
|
}, [token]);
|
||||||
|
|
||||||
const categoryItems = category
|
const categoryItems = category
|
||||||
? category.map((category) => ({
|
? category.map((category) => ({
|
||||||
label: category.category_name,
|
label: category.category_name,
|
||||||
@@ -70,8 +115,11 @@ const Navbar = () => {
|
|||||||
|
|
||||||
<div className="flex items-center space-x-4">
|
<div className="flex items-center space-x-4">
|
||||||
<SearchComponent isScrolled={isScrolled} />
|
<SearchComponent isScrolled={isScrolled} />
|
||||||
<Link href="/pages/shopping-cart">
|
<Link href="/pages/shopping-cart" className="relative">
|
||||||
<ShoppingBag size={20} className="cursor-pointer" />
|
<ShoppingBag size={20} className="cursor-pointer" />
|
||||||
|
<span className="absolute -top-2 -right-2 bg-red font-bold text-white text-xs rounded-full h-5 w-5 flex items-center justify-center">
|
||||||
|
{cartItemCount}
|
||||||
|
</span>
|
||||||
</Link>
|
</Link>
|
||||||
<div className="cursor-pointer">
|
<div className="cursor-pointer">
|
||||||
{token ? (
|
{token ? (
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ const PaymentComponent = ({ amount, onSuccess }) => {
|
|||||||
|
|
||||||
console.log("Razorpay Client ID:", process.env.NEXT_PUBLIC_RAZORPAY_CLIENT_ID);
|
console.log("Razorpay Client ID:", process.env.NEXT_PUBLIC_RAZORPAY_CLIENT_ID);
|
||||||
const options = {
|
const options = {
|
||||||
key: "rzp_test_1SbLmNX2nCKRZA",
|
key: "rzp_test_Xf4wnkvQQeUnCq",
|
||||||
amount: order.amount,
|
amount: order.amount,
|
||||||
currency: order.currency,
|
currency: order.currency,
|
||||||
name: "Rudraksha",
|
name: "Rudraksha",
|
||||||
|
|||||||
Reference in New Issue
Block a user