The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
payment method failed CTA
Steven Teyca3f81600248
diff --git a/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts b/apps/web/app/api/stripe/webhook/customer-subscription-updated.tsindex 4310417b81..a11af4d044 100644--- a/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts+++ b/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts@@ -27,6 +27,7 @@ export async function customerSubscriptionUpdated(event: Stripe.Event) {select: {id: true,plan: true,+ paymentFailedAt: true,users: {select: {user: {@@ -74,6 +75,7 @@ export async function customerSubscriptionUpdated(event: Stripe.Event) {aiLimit: plan.limits.ai!,tagsLimit: plan.limits.tags!,usersLimit: plan.limits.users!,+ paymentFailedAt: null,},}),prisma.restrictedToken.updateMany({@@ -85,6 +87,15 @@ export async function customerSubscriptionUpdated(event: Stripe.Event) {},}),]);+ } else if (workspace.paymentFailedAt) {+ await prisma.project.update({+ where: {+ id: workspace.id,+ },+ data: {+ paymentFailedAt: null,+ },+ });}const subscriptionCanceled =diff --git a/apps/web/app/api/stripe/webhook/invoice-payment-failed.tsx b/apps/web/app/api/stripe/webhook/invoice-payment-failed.tsxindex 0b8af8bab5..86d6986b4c 100644--- a/apps/web/app/api/stripe/webhook/invoice-payment-failed.tsx+++ b/apps/web/app/api/stripe/webhook/invoice-payment-failed.tsx@@ -24,6 +24,7 @@ export async function invoicePaymentFailed(event: Stripe.Event) {stripeId: stripeId.toString(),},select: {+ id: true,name: true,slug: true,users: {@@ -52,8 +53,16 @@ export async function invoicePaymentFailed(event: Stripe.Event) {return;}- await Promise.allSettled(- workspace.users.map(({ user }) =>+ await Promise.allSettled([+ prisma.project.update({+ where: {+ id: workspace.id,+ },+ data: {+ paymentFailedAt: new Date(),+ },+ }),+ ...workspace.users.map(({ user }) =>sendEmail({email: user.email as string,from: "[email protected]",@@ -77,5 +86,5 @@ export async function invoicePaymentFailed(event: Stripe.Event) {),}),),- );+ ]);}diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/page-client.tsxindex 98e93defa2..9e90617d5b 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/page-client.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/page-client.tsx@@ -3,9 +3,9 @@import useTags from "@/lib/swr/use-tags";import useUsers from "@/lib/swr/use-users";import useWorkspace from "@/lib/swr/use-workspace";+import ManageSubscriptionButton from "@/ui/workspaces/manage-subscription-button";import PlanBadge from "@/ui/workspaces/plan-badge";import {- Button,buttonVariants,Icon,InfoTooltip,@@ -15,16 +15,11 @@ import {import { CircleDollar, CursorRays, Hyperlink } from "@dub/ui/src/icons";import { cn, getFirstAndLastDay, nFormatter } from "@dub/utils";import Link from "next/link";-import { useRouter } from "next/navigation";import { CSSProperties, useMemo, useState } from "react";-import { toast } from "sonner";import { UsageChart } from "./usage-chart";export default function WorkspaceBillingClient() {- const router = useRouter();-const {- id: workspaceId,slug,plan,stripeId,@@ -91,32 +86,7 @@ export default function WorkspaceBillingClient() {)}</p></div>- {stripeId && (- <div>- <Button- text="Manage Subscription"- variant="secondary"- className="h-9"- onClick={() => {- setClicked(true);- fetch(`/api/workspaces/${workspaceId}/billing/manage`, {- method: "POST",- }).then(async (res) => {- if (res.ok) {- const url = await res.json();- console.log({ url });- router.push(url);- } else {- const { error } = await res.json();- toast.error(error.message);- setClicked(false);- }- });- }}- loading={clicked}- />- </div>- )}+ {stripeId && <ManageSubscriptionButton className="w-fit" />}</div><div className="grid grid-cols-[minmax(0,1fr)] divide-y divide-gray-200 border-y border-gray-200"><div>diff --git a/apps/web/lib/zod/schemas/workspaces.ts b/apps/web/lib/zod/schemas/workspaces.tsindex 0e093ef13f..4a4ef5d53e 100644--- a/apps/web/lib/zod/schemas/workspaces.ts+++ b/apps/web/lib/zod/schemas/workspaces.ts@@ -26,6 +26,10 @@ export const WorkspaceSchema = z.nullable().default(null).describe("The logo of the workspace."),+ inviteCode: z+ .string()+ .nullable()+ .describe("The invite code of the workspace."),plan: planSchema,stripeId: z.string().nullable().describe("The Stripe ID of the workspace."),@@ -34,14 +38,14 @@ export const WorkspaceSchema = z.describe("The date and time when the billing cycle starts for the workspace.",),+ paymentFailedAt: z+ .date()+ .nullable()+ .describe("The date and time when the payment failed for the workspace."),stripeConnectId: z.string().nullable().describe("[BETA]: The Stripe Connect ID of the workspace."),- inviteCode: z- .string()- .nullable()- .describe("The invite code of the workspace."),usage: z.number().describe("The usage of the workspace."),usageLimit: z.number().describe("The usage limit of the workspace."),diff --git a/apps/web/prisma/schema/workspace.prisma b/apps/web/prisma/schema/workspace.prismaindex 99d6e18892..e8a5291a37 100644--- a/apps/web/prisma/schema/workspace.prisma+++ b/apps/web/prisma/schema/workspace.prisma@@ -3,12 +3,13 @@ model Project {name Stringslug String @uniquelogo String?+ inviteCode String? @uniqueplan String @default("free")stripeId String? @unique // Stripe subscription IDbillingCycleStart Int // day of the month when the billing cycle starts+ paymentFailedAt DateTime?stripeConnectId String? @unique // for Stripe Connect- inviteCode String? @uniqueusage Int @default(0)usageLimit Int @default(1000)diff --git a/apps/web/ui/layout/sidebar/usage.tsx b/apps/web/ui/layout/sidebar/usage.tsxindex 9c5fc6f831..a2091346b5 100644--- a/apps/web/ui/layout/sidebar/usage.tsx+++ b/apps/web/ui/layout/sidebar/usage.tsx@@ -1,6 +1,7 @@"use client";import useWorkspace from "@/lib/swr/use-workspace";+import ManageSubscriptionButton from "@/ui/workspaces/manage-subscription-button";import { AnimatedSizeContainer, Icon } from "@dub/ui";import { buttonVariants } from "@dub/ui/src/button";import { CursorRays, Hyperlink } from "@dub/ui/src/icons";@@ -25,6 +26,7 @@ function UsageInner() {billingCycleStart,plan,slug,+ paymentFailedAt,loading,} = useWorkspace();@@ -87,30 +89,39 @@ function UsageInner() {</div><div className="mt-3">- {billingEnd ? (- <p className="text-xs text-neutral-900/40">- Usage will reset {billingEnd}- </p>- ) : loading ? (+ {loading ? (<div className="h-4 w-2/3 animate-pulse rounded-md bg-neutral-500/10" />) : (- <div className="h-4" />- )}- </div>-- {(warning || plan === "free") && (- <div className="pt-4">- <Link- href={`/${slug}/upgrade`}+ <pclassName={cn(- buttonVariants(),- "flex h-9 items-center justify-center rounded-md border px-4 text-sm",+ "text-xs text-neutral-900/40",+ paymentFailedAt && "text-red-600",)}>- {plan === "free" ? "Get Dub Pro" : "Upgrade plan"}- </Link>- </div>- )}+ {paymentFailedAt+ ? "Your last payment failed. Please update your payment method to continue using Dub."+ : `Usage will reset ${billingEnd}`}+ </p>+ )}+ </div>++ {paymentFailedAt ? (+ <ManageSubscriptionButton+ text="Update Payment Method"+ variant="primary"+ className="mt-4 w-full"+ />+ ) : warning || plan === "free" ? (+ <Link+ href={`/${slug}/upgrade`}+ className={cn(+ buttonVariants(),+ "mt-4 flex h-9 items-center justify-center rounded-md border px-4 text-sm",+ )}+ >+ {plan === "free" ? "Get Dub Pro" : "Upgrade plan"}+ </Link>+ ) : null}</div></AnimatedSizeContainer>) : null;diff --git a/apps/web/ui/workspaces/manage-subscription-button.tsx b/apps/web/ui/workspaces/manage-subscription-button.tsxnew file mode 100644index 0000000000..6751b2a7ff--- /dev/null+++ b/apps/web/ui/workspaces/manage-subscription-button.tsx@@ -0,0 +1,40 @@+"use client";++import useWorkspace from "@/lib/swr/use-workspace";+import { Button, ButtonProps } from "@dub/ui";+import { cn } from "@dub/utils";+import { useRouter } from "next/navigation";+import { useState } from "react";+import { toast } from "sonner";++export default function ManageSubscriptionButton(props: ButtonProps) {+ const { id: workspaceId } = useWorkspace();+ const [clicked, setClicked] = useState(false);+ const router = useRouter();++ return (+ <Button+ {...props}+ text={props.text || "Manage Subscription"}+ variant={props.variant || "secondary"}+ className={cn(props.className, "h-9")}+ onClick={() => {+ setClicked(true);+ fetch(`/api/workspaces/${workspaceId}/billing/manage`, {+ method: "POST",+ }).then(async (res) => {+ if (res.ok) {+ const url = await res.json();+ console.log({ url });+ router.push(url);+ } else {+ const { error } = await res.json();+ toast.error(error.message);+ setClicked(false);+ }+ });+ }}+ loading={clicked}+ />+ );+}