Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

add manage payment method flow

Steven Teyb22c709c6673
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/billing/payment-methods/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/billing/payment-methods/route.ts
index 4eca1e3bca..f3e7d1e836 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/billing/payment-methods/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/billing/payment-methods/route.ts
@@ -1,5 +1,6 @@
import { withWorkspace } from "@/lib/auth";
import { stripe } from "@/lib/stripe";
+import { APP_DOMAIN } from "@dub/utils";
import { NextResponse } from "next/server";
export const GET = withWorkspace(async ({ workspace }) => {
@@ -18,3 +19,19 @@ export const GET = withWorkspace(async ({ workspace }) => {
return NextResponse.json([]);
}
});
+
+export const POST = withWorkspace(async ({ workspace }) => {
+ if (!workspace.stripeId) {
+ return NextResponse.json({ error: "Workspace does not have a Stripe ID" });
+ }
+
+ const { url } = await stripe.billingPortal.sessions.create({
+ customer: workspace.stripeId,
+ return_url: `${APP_DOMAIN}/${workspace.slug}/settings/billing`,
+ flow_data: {
+ type: "payment_method_update",
+ },
+ });
+
+ return NextResponse.json({ url });
+});
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/payment-methods.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/payment-methods.tsx
index 205cbef50f..eb767596cd 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/payment-methods.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/payment-methods.tsx
@@ -1,19 +1,38 @@
"use client";
+import useWorkspace from "@/lib/swr/use-workspace";
import { AnimatedEmptyState } from "@/ui/shared/animated-empty-state";
+import { Button, TooltipContent } from "@dub/ui";
import { CreditCard, GreekTemple, StripeLink } from "@dub/ui/icons";
import { cn, fetcher } from "@dub/utils";
-import { useParams } from "next/navigation";
+import { useRouter } from "next/navigation";
+import { useState } from "react";
import { Stripe } from "stripe";
import useSWR from "swr";
export default function PaymentMethods() {
- const { slug } = useParams();
+ const { slug, stripeId } = useWorkspace();
const { data: paymentMethods } = useSWR<Stripe.PaymentMethod[]>(
- `/api/workspaces/${slug}/billing/payment-methods`,
+ slug && `/api/workspaces/${slug}/billing/payment-methods`,
fetcher,
);
+ const router = useRouter();
+ const [isLoading, setIsLoading] = useState(false);
+
+ const managePaymentMethods = async () => {
+ setIsLoading(true);
+ const { url } = await fetch(
+ `/api/workspaces/${slug}/billing/payment-methods`,
+ {
+ method: "POST",
+ },
+ ).then((res) => res.json());
+
+ router.push(url);
+ setIsLoading(false);
+ };
+
return (
<div className="rounded-lg border border-neutral-200 bg-white">
<div className="flex flex-col items-start justify-between gap-y-4 p-6 md:p-8 xl:flex-row">
@@ -23,6 +42,22 @@ export default function PaymentMethods() {
Manage your payment methods on Dub
</p>
</div>
+ <Button
+ variant="secondary"
+ text="Manage"
+ className="h-8 w-fit"
+ disabledTooltip={
+ !stripeId && (
+ <TooltipContent
+ title="You must upgrade to a paid plan to manage your payment methods."
+ cta="Upgrade"
+ href={`/${slug}/upgrade`}
+ />
+ )
+ }
+ onClick={managePaymentMethods}
+ loading={isLoading}
+ />
</div>
<div className="grid gap-4 border-t border-neutral-200 p-6">
{paymentMethods ? (
@@ -70,7 +105,7 @@ const PaymentMethodsDetails = (paymentMethod: Stripe.PaymentMethod) =>
},
{
type: "us_bank_account",
- title: "ACH Debit",
+ title: "ACH",
icon: GreekTemple,
description: paymentMethod.us_bank_account
? `Connected ${paymentMethod.us_bank_account.account_holder_type} account ending in ${paymentMethod.us_bank_account.last4}`