Skip to content

Commit

Permalink
Update page.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
ArhanAnsari authored Oct 8, 2024
1 parent 99a9a8f commit 6aa3dec
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions app/dashboard/upgrade/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//app/dashboard/upgrade/page.tsx
"use client";
import React, { useEffect, useState } from "react";
import getStripe from "@/lib/stripe-js"; // Ensure this utility is set up correctly
Expand Down Expand Up @@ -38,37 +37,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;
}

try {
const response = await fetch(`/api/checkout?plan=${plan}`);
if (!response.ok) {
throw new Error(`Failed to initiate checkout for plan: ${plan}`);
if (plan === "free") {
// Redirect to dashboard if user selects the Free plan
router.push("/dashboard");
return;
}
const body = await response.json();
const sessionId = body.sessionId;

const stripe = await getStripe();
if (!stripe) {
throw new Error("Stripe initialization failed");
}
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;

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}`); // 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 stripe = await getStripe();
if (!stripe) {
throw new Error("Stripe initialization failed");
}

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}`); // 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

Expand Down

0 comments on commit 6aa3dec

Please sign in to comment.