Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

working on upgrade subscription feature #297

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/api/upgrade-subscription/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ async function upgradeSubscription(request: NextRequest & { user: User }) {
);
}

const data = await response.json();
return NextResponse.json({ data });
const { checkoutUrl, sessionId } = await response.json();
return NextResponse.json({ checkoutUrl, sessionId });
} catch (error) {
let errMsg = "Error upgrading subscription: ";
if (error instanceof Error) {
Expand Down
31 changes: 23 additions & 8 deletions components/dashboard/subscription-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import {
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { useState } from "react";
import { useCancelSubscription } from "@/hooks/useCancelSubscription";
import { User } from "@supabase/supabase-js";
import { Info } from "lucide-react";
import { UsageType } from "../dashboard";
import { toast } from "sonner";
import { useUpgradeSubscription } from "@/hooks/useUpgradeSubscription";
import { useState } from "react";

type SubscriptionCardProps = {
subscription: Subscription | null;
Expand Down Expand Up @@ -73,12 +73,27 @@ export default function SubscriptionCard({
const handleUpgradeSubscriptionClick = async () => {
try {
if (subscription?.subscription_id) {
await handleUpgradeSubscription();
return;
const response = await fetch("/api/upgrade-subscription", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
subscriptionId: subscription.subscription_id,
}),
});
if (!response.ok) {
throw new Error("Failed to create checkout session");
}

const { checkoutUrl } = await response.json();
window.location.href = checkoutUrl;
} else {
toast.error(
"Failed to upgrade subscription. Subscription ID not found.",
);
}
toast.error("Failed to upgrade subscription. Subscription ID not found.");
} catch (error) {
console.error("Error upgrading subscription:", error);
toast.error("Failed to upgrade subscription.");
} finally {
setIsUpgradeDialogOpen(false);
Expand Down Expand Up @@ -165,7 +180,7 @@ export default function SubscriptionCard({
<p className="text-muted-foreground">
{capitalizeInital(subscription.pricing_tier)}
</p>
{/* {subscription.pricing_tier == "monthly" && (
{subscription.pricing_tier === "monthly" && (
<Dialog
open={isUpgradeDialogOpen}
onOpenChange={setIsUpgradeDialogOpen}
Expand Down Expand Up @@ -217,12 +232,12 @@ export default function SubscriptionCard({
handleUpgradeSubscriptionClick();
}}
>
{isUpgrading ? "Upgrading..." : "Confirm Upgrade"}
{isUpgrading ? "Upgrading..." : "Go to Upgrade"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)} */}
)}
</div>
</div>
</div>
Expand Down
19 changes: 12 additions & 7 deletions hooks/useUpgradeSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { toast } from "sonner";
import { useRouter } from "next/navigation";
import { Subscription } from "@/types/subscription";

const PEARAI_SERVER_URL = process.env.PEARAI_SERVER_URL;
const STRIPE_ANNUAL_PEARAI_PRO_PRODUCT_ID =
process.env.STRIPE_ANNUAL_PEARAI_PRO_PRODUCT_ID;

export const useUpgradeSubscription = (
user: User | null,
subscription: Subscription | null,
Expand All @@ -18,27 +22,28 @@ export const useUpgradeSubscription = (
return;
}
if (isUpgrading) return;

setIsUpgrading(true);

try {
const response = await fetch("/api/upgrade-subscription", {
const response = await fetch("/api/update-subscription", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
subscriptionId: subscription?.subscription_id,
annualPriceId: STRIPE_ANNUAL_PEARAI_PRO_PRODUCT_ID,
}),
});

if (!response.ok) {
const errorData = await response.json();
console.error("Error data", errorData);
throw new Error(errorData);
throw new Error(errorData.detail);
}

const { data } = await response.json();

if (data.status === "success") {
toast.success("Your subscription has been upgraded successfully.");
const { url, sessionId } = await response.json();
if (url && sessionId) {
// Redirect the user to the Stripe checkout session
window.location.href = url;
} else {
toast.error("Failed to upgrade subscription. Please try again.");
}
Expand Down