//api/stripe
import { auth, currentUser } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
import { prismadb } from "@/lib/prismadb";
import { stripe } from "@/lib/stripe";
import { absoluteUrl } from "@/lib/utils";
const billingUrl = absoluteUrl("/billing");
export async function GET() {
try {
const { userId } = auth();
const user = await currentUser();
if (!userId || !user) {
return new NextResponse("Unauthorized", { status: 401 });
}
const userSubscription = await prismadb.userSubscription.findUnique({
where: {
userId,
},
});
if (userSubscription && userSubscription.stripeCustomerId) {
const stripeSession = await stripe.billingPortal.sessions.create({
customer: userSubscription.stripeCustomerId,
return_url: billingUrl,
});
return new NextResponse(JSON.stringify({ url: stripeSession.url }));
}
const stripeSession = await stripe.checkout.sessions.create({
success_url: billingUrl,
cancel_url: billingUrl,
payment_method_types: ["card", "Paypal"],
mode: "subscription",
billing_address_collection: "auto",
customer_email: user.emailAddresses[0].emailAddress,
line_items: [
{
price_data: {
currency: "USD",
product_data: {
name: "Plume Pro",
description: "Gain Full Access",
},
unit_amount: 7999,
recurring: {
interval: "month",
},
},
quantity: 1,
},
{
price_data: {
currency: "USD",
product_data: {
name: "Plume Plus",
description: "Gain Full Access",
},
unit_amount: 3999,
recurring: {
interval: "month",
},
},
quantity: 1,
},
],
metadata: {
userId,
},
});
return new NextResponse(JSON.stringify({ url: stripeSession.url }));
} catch (error) {
console.log("[STRIPE_GET]", error);
return new NextResponse("Internal Error", { status: 500 });
}
}
"use client";
import { usePlanModal } from "@/hooks/use-plan-modal";
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
} from "../ui/dialog";
import { Separator } from "../ui/separator";
import { Button } from "../ui/button";
import { useToast } from "../ui/use-toast";
import axios from "axios";
import { useState } from "react";
export const PlanModal = () => {
const planModal = usePlanModal();
const { toast } = useToast();
const [loading, setLoading] = useState(false);
const onSubscribe = async () => {
try {
setLoading(true);
const response = await axios.get("/api/stripe");
window.location.href = response.data.url;
} catch (error) {
toast({
variant: "destructive",
description: "Oops! Something went wrong.",
});
} finally {
setLoading(false);
}
};
return (
<Dialog open={planModal.isOpen} onOpenChange={planModal.onClose}>
<DialogContent>
<DialogHeader className="space-y-4">
<DialogTitle className="text-center">Upgrade your Plan</DialogTitle>
<DialogDescription className="text-center space-y-2">
Choose a plan that meets your needs.
</DialogDescription>
</DialogHeader>
<Separator />
<div className="flex items-center justify-between">
<p className="text-2xl font-plus font-medium">
$39
<span className="text-sm font-normal">.99</span>
</p>
<Button size="md" onClick={onSubscribe}>
Subscribe
</Button>
</div>
</DialogContent>
</Dialog>
);
};
I am building my app using Nextjs and Stripe, which have multiple plans for users to subscribe to. This is the first app I have built with various tiers and I am failing to find a way to make it work. So, the help I am seeking is to know if there is a way I can make my plans in a single API folder(if so, how), or if I will have to create different API files for each tier. The way I did it above just adds up the two tiers and the checkout is $119.98.