From 0136add6ae69a7529f7d3c84ad890b99f66a1ad6 Mon Sep 17 00:00:00 2001 From: Arhan Ansari Date: Tue, 8 Oct 2024 10:15:59 +0530 Subject: [PATCH 1/3] Update page.tsx --- app/dashboard/upgrade/page.tsx | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/app/dashboard/upgrade/page.tsx b/app/dashboard/upgrade/page.tsx index a4e9e3e..b1ff5c6 100644 --- a/app/dashboard/upgrade/page.tsx +++ b/app/dashboard/upgrade/page.tsx @@ -23,9 +23,11 @@ const UpgradePage: React.FC = () => { const userData = await getUserData(session.user.email); if (userData) { setUserPlan(userData.plan); // Update the state with the user's plan + } else { + throw new Error("User data not found"); } } catch (error) { - console.error("Error fetching user data:", error); + console.error("Error fetching user data:", error); // Log the error to the console } } }; @@ -35,20 +37,31 @@ const UpgradePage: React.FC = () => { } }, [session]); - const getPriceFn = (plan: string) => { + const getPriceFn = async (plan: string) => { if (plan === "free") { // Redirect to dashboard if user selects the Free plan router.push("/dashboard"); return; } - fetch(`/api/checkout?plan=${plan}`) - .then((data) => data.json()) - .then(async (body) => { - const sessionId = body.sessionId; - const stripe = await getStripe(); - await stripe?.redirectToCheckout({ sessionId }); - }); + try { + const response = await fetch(`/api/checkout?plan=${plan}`); + if (!response.ok) { + throw new Error(`Failed to initiate checkout for plan: ${plan}`); + } + const body = await response.json(); + const sessionId = body.sessionId; + + const stripe = await getStripe(); + if (!stripe) { + throw new Error("Stripe initialization failed"); + } + + await stripe.redirectToCheckout({ sessionId }); + } catch (error) { + console.error("Error during checkout process:", error); // Log the error to the console + alert(`Error during checkout process: ${error.message}`); // Optionally show the error to the user + } }; const isCurrentPlan = (plan: string) => userPlan === plan; // Helper function to check if the user is on the current plan From 813e3e99c3bc3dff126d890f356fb032b4f56460 Mon Sep 17 00:00:00 2001 From: Arhan Ansari Date: Tue, 8 Oct 2024 10:20:14 +0530 Subject: [PATCH 2/3] Update page.tsx --- app/page.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/page.tsx b/app/page.tsx index cb8dc5d..51eff9d 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -19,7 +19,7 @@ export default function HomePage() { Leverage the power of AI with Google Gemini to create amazing content effortlessly. Whether you need help generating stories, articles, or creative ideas, InspireGem is here to assist you.

View Plans From 732ccaadeb8b568b26bb6b9efddd502df35a0be6 Mon Sep 17 00:00:00 2001 From: Arhan Ansari Date: Tue, 8 Oct 2024 10:22:34 +0530 Subject: [PATCH 3/3] Update page.tsx --- app/dashboard/upgrade/page.tsx | 46 +++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/app/dashboard/upgrade/page.tsx b/app/dashboard/upgrade/page.tsx index b1ff5c6..378878f 100644 --- a/app/dashboard/upgrade/page.tsx +++ b/app/dashboard/upgrade/page.tsx @@ -38,31 +38,37 @@ const UpgradePage: React.FC = () => { }, [session]); const getPriceFn = async (plan: string) => { - if (plan === "free") { - // Redirect to dashboard if user selects the Free plan - router.push("/dashboard"); - return; - } + if (plan === "free") { + // Redirect to dashboard if user selects the Free plan + router.push("/dashboard"); + return; + } - try { - const response = await fetch(`/api/checkout?plan=${plan}`); - if (!response.ok) { - throw new Error(`Failed to initiate checkout for plan: ${plan}`); - } - const body = await response.json(); - const sessionId = body.sessionId; + try { + const response = await fetch(`/api/checkout?plan=${plan}`); + if (!response.ok) { + throw new Error(`Failed to initiate checkout for plan: ${plan}`); + } + const body = await response.json(); + const sessionId = body.sessionId; - const stripe = await getStripe(); - if (!stripe) { - throw new Error("Stripe initialization failed"); - } + const stripe = await getStripe(); + if (!stripe) { + throw new Error("Stripe initialization failed"); + } - await stripe.redirectToCheckout({ sessionId }); - } catch (error) { + await stripe.redirectToCheckout({ sessionId }); + } catch (error) { + // Check if the error is an instance of Error + if (error instanceof Error) { console.error("Error during checkout process:", error); // Log the error to the console - alert(`Error during checkout process: ${error.message}`); // Optionally show the error to the user + alert(`Error during checkout process: ${error.message}`); // Show the error to the user + } else { + console.error("Unknown error during checkout process:", error); // Log unknown error + alert("An unknown error occurred during checkout."); } - }; + } +}; const isCurrentPlan = (plan: string) => userPlan === plan; // Helper function to check if the user is on the current plan