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 813e3e9 commit 732ccaa
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions app/dashboard/upgrade/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 732ccaa

Please sign in to comment.