UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.
🐛 fix: update billing portal issues
Dillion Verma548a25d30b9c
diff --git a/app/(dashboard)/dashboard/billing/create-billing-portal.action.ts b/app/(dashboard)/dashboard/billing/create-billing-portal.action.tsnew file mode 100644index 00000000..8c1d9727--- /dev/null+++ b/app/(dashboard)/dashboard/billing/create-billing-portal.action.ts@@ -0,0 +1,40 @@+"use server";++import { getCurrentUser } from "@/lib/session";+import { stripe } from "@/lib/stripe";+import { getStripeCustomerByEmail } from "@/lib/stripe/get-customer";+import { getPortalSession } from "@/lib/stripe/get-portal-session";+// import { getSubscriptionByUserId } from "@/lib/stripe/get-subscription-by-user-id";+import { redirect } from "next/navigation";+import type Stripe from "stripe";++export const createBillingPortal = async () => {+ const user = await getCurrentUser();++ if (!user) {+ redirect("/");+ }++ let stripeCustomer: Stripe.Customer | null = null;++ // Fallback to check if a Stripe customer already exists for the current user email.+ if (!stripeCustomer) {+ stripeCustomer = await getStripeCustomerByEmail(user.email!);+ }++ // Create a Stripe customer if it does not exist for the current user.+ if (!stripeCustomer) {+ stripeCustomer = await stripe.customers.create({+ name: user.name ?? undefined,+ email: user.email!,+ metadata: {+ userId: user.id,+ },+ });+ }++ return getPortalSession({+ customerId: stripeCustomer.id,+ returnUrl: `${process.env.NEXT_PUBLIC_APP_URL}/settings/billing`,+ });+};diff --git a/app/(dashboard)/dashboard/billing/page.tsx b/app/(dashboard)/dashboard/billing/page.tsxindex 2101a7aa..45e5eda9 100644--- a/app/(dashboard)/dashboard/billing/page.tsx+++ b/app/(dashboard)/dashboard/billing/page.tsx@@ -1,9 +1,8 @@import { DashboardHeader } from "@/components/dashboard/header";-import { OrderList } from "@/components/dashboard/order-list";import { DashboardShell } from "@/components/dashboard/shell";+import { BillingPortalButton } from "@/components/stripe-portal-button";import { authOptions } from "@/lib/auth";import { getCurrentUser } from "@/lib/session";-import { getUserPayments } from "@/lib/stripe-utils";import { redirect } from "next/navigation";export const metadata = {@@ -18,14 +17,15 @@ export default async function BillingPage() {redirect(authOptions?.pages?.signIn || "/login");}- const payments = await getUserPayments(user.id);+ // const payments = await getUserPayments(user.id);return (<DashboardShell><DashboardHeader heading="Billing" text="Manage billing information" />- <div className="grid gap-8">+ <BillingPortalButton>Manage Billing</BillingPortalButton>+ {/* <div className="grid gap-8"><OrderList payments={payments} />- </div>+ </div> */}</DashboardShell>);}diff --git a/components/stripe-portal-button.tsx b/components/stripe-portal-button.tsxnew file mode 100644index 00000000..645eeb37--- /dev/null+++ b/components/stripe-portal-button.tsx@@ -0,0 +1,60 @@+"use client";++import { createBillingPortal } from "@/app/(dashboard)/dashboard/billing/create-billing-portal.action";+import { Icons } from "@/components/icons";+import { Button } from "@/components/ui/button";+import { toast } from "@/components/ui/use-toast";+import { useState } from "react";++export const BillingPortalButton = ({+ children = "Manage Subscription",+}: {+ children?: React.ReactNode;+}) => {+ const [isLoading, setIsLoading] = useState(false);++ const handleFetchPortalUrl = async () => {+ if (isLoading) {+ return;+ }++ setIsLoading(true);++ try {+ const sessionUrl = await createBillingPortal();++ if (!sessionUrl) {+ throw new Error("NO_SESSION");+ }++ window.open(sessionUrl, "_blank");+ } catch (e: any) {+ let description =+ "We are unable to proceed to the billing portal at this time. Please try again, or contact support.";++ if (e.message === "CUSTOMER_NOT_FOUND") {+ description =+ "You do not currently have a customer record, this should not happen. Please contact support for assistance.";+ }++ toast({+ title: "Something went wrong",+ description,+ variant: "destructive",+ });+ }++ setIsLoading(false);+ };++ return (+ <Button+ onClick={async () => handleFetchPortalUrl()}+ disabled={isLoading}+ className="flex w-full max-w-[400px] gap-2"+ >+ {children}+ {isLoading && <Icons.spinner className="mr-2 h-4 w-4 animate-spin" />}+ </Button>+ );+};diff --git a/lib/stripe/get-customer.ts b/lib/stripe/get-customer.tsnew file mode 100644index 00000000..fa0e5621--- /dev/null+++ b/lib/stripe/get-customer.ts@@ -0,0 +1,19 @@+import { stripe } from "@/lib/stripe";++export const getStripeCustomerByEmail = async (email: string) => {+ const foundStripeCustomers = await stripe.customers.list({+ email,+ });++ return foundStripeCustomers.data[0] ?? null;+};++export const getStripeCustomerById = async (stripeCustomerId: string) => {+ try {+ const stripeCustomer = await stripe.customers.retrieve(stripeCustomerId);++ return !stripeCustomer.deleted ? stripeCustomer : null;+ } catch {+ return null;+ }+};diff --git a/lib/stripe/get-portal-session.ts b/lib/stripe/get-portal-session.tsnew file mode 100644index 00000000..25654fa3--- /dev/null+++ b/lib/stripe/get-portal-session.ts@@ -0,0 +1,22 @@+"use server";++import { stripe } from "@/lib/stripe";++export type GetPortalSessionOptions = {+ customerId: string;+ returnUrl: string;+};++export const getPortalSession = async ({+ customerId,+ returnUrl,+}: GetPortalSessionOptions) => {+ "use server";++ const session = await stripe.billingPortal.sessions.create({+ customer: customerId,+ return_url: returnUrl,+ });++ return session.url;+};diff --git a/registry/components/magicui/shimmer-button.tsx b/registry/components/magicui/shimmer-button.tsxindex d225c9e8..4db805fb 100644--- a/registry/components/magicui/shimmer-button.tsx+++ b/registry/components/magicui/shimmer-button.tsx@@ -56,7 +56,7 @@ const ShimmerButton = React.forwardRef<HTMLButtonElement, ShimmerButtonProps>({/* spark */}<div className="absolute inset-0 h-[100cqh] animate-slide [aspect-ratio:1] [border-radius:0] [mask:none]">{/* spark before */}- <div className="absolute inset-[-100%] w-auto rotate-0 animate-spin [background:conic-gradient(from_calc(270deg-(var(--spread)*0.5)),transparent_0,var(--shimmer-color)_var(--spread),transparent_var(--spread))] [translate:0_0]" />+ <div className="animate-spin-around absolute inset-[-100%] w-auto rotate-0 [background:conic-gradient(from_calc(270deg-(var(--spread)*0.5)),transparent_0,var(--shimmer-color)_var(--spread),transparent_var(--spread))] [translate:0_0]" /></div></div>{children}diff --git a/tailwind.config.ts b/tailwind.config.tsindex 65016705..41c263b0 100644--- a/tailwind.config.ts+++ b/tailwind.config.ts@@ -69,7 +69,7 @@ module.exports = {"marquee-vertical": "marquee-vertical var(--duration) linear infinite",flip: "flip calc(var(--spark) * 2) infinite steps(2, end)",rotate: "rotate var(--spark) linear infinite both",- spin: "spin calc(var(--speed) * 2) infinite linear",+ "spin-around": "spin-around calc(var(--speed) * 2) infinite linear",slide: "slide var(--speed) ease-in-out infinite alternate",ripple: "ripple 3400ms ease infinite",line: "line 2s linear infinite",@@ -117,7 +117,7 @@ module.exports = {transform: "rotate(90deg)",},},- spin: {+ "spin-around": {"0%": {transform: "translateZ(0) rotate(0)",},