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 `isEligibleForTrial` check (#3914)

Steven Tey8eefa2f30f78
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
index 584ec383b5..8a118c2d8f 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/billing/upgrade/route.ts
@@ -2,6 +2,7 @@ import { DubApiError } from "@/lib/api/errors";
import { getDubAdminRole, withWorkspace } from "@/lib/auth";
import { getDubCustomer } from "@/lib/dub";
import { stripe } from "@/lib/stripe";
+import { isEligibleForTrial } from "@/lib/stripe/is-eligible-for-trial";
import { booleanQuerySchema } from "@/lib/zod/schemas/misc";
import { APP_DOMAIN, DUB_TRIAL_PERIOD_DAYS } from "@dub/utils";
import { NextResponse } from "next/server";
@@ -104,15 +105,6 @@ export const POST = withWorkspace(
} else {
const customer = await getDubCustomer(session.user.id);
- // Only apply trial if the customer is a:
- // - on the free plan
- // - new Stripe customer
- // - no prior/existing trial on workspace
- const shouldApplyCheckoutTrial =
- workspace.plan === "free" &&
- workspace.stripeId == null &&
- workspace.trialEndsAt == null;
-
const stripeSession = await stripe.checkout.sessions.create({
...(workspace.stripeId
? {
@@ -152,7 +144,7 @@ export const POST = withWorkspace(
enabled: true,
},
mode: "subscription",
- ...(shouldApplyCheckoutTrial
+ ...(isEligibleForTrial({ workspace, session })
? {
subscription_data: {
trial_period_days: DUB_TRIAL_PERIOD_DAYS,
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/upgrade/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/upgrade/page.tsx
index a92b03a830..bbdce70cdf 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/upgrade/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/upgrade/page.tsx
@@ -1,6 +1,7 @@
"use client";
import { clientAccessCheck } from "@/lib/client-access-check";
+import { isEligibleForTrial } from "@/lib/stripe/is-eligible-for-trial";
import useWorkspace from "@/lib/swr/use-workspace";
import { PageContent } from "@/ui/layout/page-content";
import { PageWidthWrapper } from "@/ui/layout/page-width-wrapper";
@@ -34,6 +35,7 @@ import {
import NumberFlow from "@number-flow/react";
import { ChevronLeft, ChevronRight } from "lucide-react";
import { motion } from "motion/react";
+import { useSession } from "next-auth/react";
import Link from "next/link";
import { useSearchParams } from "next/navigation";
import { CSSProperties, useEffect, useMemo, useState } from "react";
@@ -54,6 +56,8 @@ const COMPARE_FEATURE_ICONS: Record<
export default function WorkspaceBillingUpgradePage() {
const searchParams = useSearchParams();
+ const { data: session } = useSession();
+ const workspace = useWorkspace();
const {
slug,
role,
@@ -63,7 +67,7 @@ export default function WorkspaceBillingUpgradePage() {
stripeId,
partnersLimit,
trialEndsAt,
- } = useWorkspace();
+ } = workspace;
const { error: permissionsError } = clientAccessCheck({
action: "billing.write",
@@ -193,11 +197,6 @@ export default function WorkspaceBillingUpgradePage() {
}),
);
- const isEligibleForTrial =
- currentPlan === "free" &&
- stripeId == null &&
- trialEndsAt == null;
-
return (
<div
key={plan.name}
@@ -282,7 +281,7 @@ export default function WorkspaceBillingUpgradePage() {
disabledTooltip={permissionsError || undefined}
text={
currentPlan === "enterprise"
- ? "Contact support"
+ ? "Contact us"
: isCurrentPlanAndPeriod
? isWorkspaceBillingTrialActive(trialEndsAt)
? "Activate plan"
@@ -293,7 +292,10 @@ export default function WorkspaceBillingUpgradePage() {
? "Downgrade"
: isWorkspaceBillingTrialActive(trialEndsAt)
? "Switch trial"
- : isEligibleForTrial
+ : isEligibleForTrial({
+ workspace,
+ session,
+ })
? `Start ${DUB_TRIAL_PERIOD_DAYS}-day trial`
: `Upgrade to ${plan.name} ${capitalize(period)}`
}
diff --git a/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/plan/plan-selector.tsx b/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/plan/plan-selector.tsx
index 6e70675f86..8028504cc9 100644
--- a/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/plan/plan-selector.tsx
+++ b/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/plan/plan-selector.tsx
@@ -174,6 +174,7 @@ export function PlanSelector({ product }: { product: OnboardingProduct }) {
>
<Button
text="Contact us"
+ variant="secondary"
className="h-10 rounded-lg shadow-sm"
/>
</a>
diff --git a/apps/web/lib/stripe/is-eligible-for-trial.ts b/apps/web/lib/stripe/is-eligible-for-trial.ts
new file mode 100644
index 0000000000..2dd6f2d393
--- /dev/null
+++ b/apps/web/lib/stripe/is-eligible-for-trial.ts
@@ -0,0 +1,31 @@
+import { isGenericEmail } from "../is-generic-email";
+
+// User is only eligible for a trial if they:
+// - are on the free plan
+// - are a new Stripe customer
+// - have no prior/existing trial on workspace
+// - are not using a generic email
+export const isEligibleForTrial = ({
+ workspace,
+ session,
+}: {
+ workspace?: {
+ plan?: string | null;
+ stripeId?: string | null;
+ trialEndsAt?: Date | null;
+ } | null;
+ session?: {
+ user?: {
+ email?: string | null;
+ } | null;
+ } | null;
+}) => {
+ if (!workspace || !session?.user?.email) return false;
+
+ return (
+ workspace.plan === "free" &&
+ workspace.stripeId == null &&
+ workspace.trialEndsAt == null &&
+ !isGenericEmail(session.user.email)
+ );
+};
diff --git a/apps/web/ui/modals/manage-usage-modal.tsx b/apps/web/ui/modals/manage-usage-modal.tsx
index 5b4f23d082..a54a83c8d3 100644
--- a/apps/web/ui/modals/manage-usage-modal.tsx
+++ b/apps/web/ui/modals/manage-usage-modal.tsx
@@ -1,4 +1,5 @@
import { clientAccessCheck } from "@/lib/client-access-check";
+import { isEligibleForTrial } from "@/lib/stripe/is-eligible-for-trial";
import useWorkspace from "@/lib/swr/use-workspace";
import { CursorRays, Hyperlink, Modal, Slider, ToggleGroup } from "@dub/ui";
import {
@@ -11,6 +12,7 @@ import {
isDowngradePlan,
} from "@dub/utils";
import NumberFlow from "@number-flow/react";
+import { useSession } from "next-auth/react";
import Link from "next/link";
import {
Dispatch,
@@ -28,18 +30,10 @@ type ManageUsageModalProps = {
};
function ManageUsageModalContent({ type }: ManageUsageModalProps) {
+ const { data: session } = useSession();
const workspace = useWorkspace();
- const {
- slug,
- role,
- stripeId,
- plan,
- planPeriod,
- planTier,
- trialEndsAt,
- usageLimit,
- linksLimit,
- } = workspace;
+ const { slug, role, plan, planPeriod, planTier, usageLimit, linksLimit } =
+ workspace;
const { error: permissionsError } = clientAccessCheck({
action: "billing.write",
@@ -82,7 +76,7 @@ function ManageUsageModalContent({ type }: ManageUsageModalProps) {
},
);
- const isCurrentPlanSuggested =
+ const isCurrentPlanAndPeriod =
plan === suggestedPlan.name.toLowerCase() &&
planPeriod === period &&
suggestedPlanTier === (planTier ?? 1);
@@ -96,9 +90,6 @@ function ManageUsageModalContent({ type }: ManageUsageModalProps) {
newTier: suggestedPlanTier,
});
- const isEligibleForTrial =
- plan === "free" && stripeId == null && trialEndsAt == null;
-
if (usageSteps.length < 2) return null;
return (
@@ -192,16 +183,16 @@ function ManageUsageModalContent({ type }: ManageUsageModalProps) {
plan={suggestedPlan.name.toLowerCase()}
tier={suggestedPlanTier}
period={period}
- disabled={isCurrentPlanSuggested}
+ disabled={isCurrentPlanAndPeriod}
disabledTooltip={permissionsError || undefined}
text={
- isCurrentPlanSuggested
+ isCurrentPlanAndPeriod
? "Current plan"
: isDowngradeSuggested
? "Downgrade"
: planPeriod && planPeriod !== period
? `Switch to ${suggestedPlan.name} ${capitalize(period)}`
- : isEligibleForTrial
+ : isEligibleForTrial({ workspace, session })
? `Start ${DUB_TRIAL_PERIOD_DAYS}-day trial`
: `Upgrade to ${suggestedPlan.name} ${capitalize(period)}`
}
diff --git a/apps/web/ui/workspaces/upgrade-plan-button.tsx b/apps/web/ui/workspaces/upgrade-plan-button.tsx
index 9c07976685..575c0c35ad 100644
--- a/apps/web/ui/workspaces/upgrade-plan-button.tsx
+++ b/apps/web/ui/workspaces/upgrade-plan-button.tsx
@@ -3,6 +3,7 @@
import { wouldLosePartnerAccess } from "@/lib/plans/has-partner-access";
import { wouldLoseAdvancedFeatures } from "@/lib/plans/would-lose-advanced-features";
import { getStripe } from "@/lib/stripe/client";
+import { isEligibleForTrial } from "@/lib/stripe/is-eligible-for-trial";
import useWorkspace from "@/lib/swr/use-workspace";
import { Button, ButtonProps } from "@dub/ui";
import {
@@ -12,6 +13,7 @@ import {
isWorkspaceBillingTrialActive,
SELF_SERVE_PAID_PLANS,
} from "@dub/utils";
+import { useSession } from "next-auth/react";
import { usePlausible } from "next-plausible";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { useMemo, useState } from "react";
@@ -32,6 +34,9 @@ export function UpgradePlanButton({
const router = useRouter();
const pathname = usePathname();
const searchParams = useSearchParams();
+ const { data: session } = useSession();
+
+ const workspace = useWorkspace();
const {
slug: workspaceSlug,
plan: currentPlan,
@@ -39,7 +44,7 @@ export function UpgradePlanButton({
stripeId,
defaultProgramId,
trialEndsAt,
- } = useWorkspace();
+ } = workspace;
const plausible = usePlausible();
const product = searchParams.get("product");
@@ -54,7 +59,7 @@ export function UpgradePlanButton({
const queryString = searchParams.toString();
- const isCurrentPlan =
+ const isCurrentPlanAndPeriod =
currentPlan === selectedPlan.name.toLowerCase() &&
period === currentPlanPeriod;
@@ -142,7 +147,7 @@ export function UpgradePlanButton({
useStartPaidPlanModal();
const isSwitchingTrialPlan =
- isTrialActive && !isCurrentPlan && currentPlan !== "free";
+ isTrialActive && !isCurrentPlanAndPeriod && currentPlan !== "free";
const { SwitchTrialPlanModal, setShowSwitchTrialPlanModal } =
useSwitchTrialPlanModal({
@@ -153,7 +158,7 @@ export function UpgradePlanButton({
});
const handleClick = () => {
- if (isCurrentPlan && isTrialActive) {
+ if (isCurrentPlanAndPeriod && isTrialActive) {
setShowStartPaidPlanModal(true);
return;
}
@@ -179,18 +184,16 @@ export function UpgradePlanButton({
text={
!currentPlan
? "Loading..."
- : isCurrentPlan
+ : isCurrentPlanAndPeriod
? isTrialActive
? "Activate plan"
: "Current plan"
- : currentPlan === "free"
+ : isEligibleForTrial({ workspace, session })
? `Start ${DUB_TRIAL_PERIOD_DAYS}-day trial · ${selectedPlan.name} ${capitalize(period)}`
- : isTrialActive
- ? `Switch trial to ${selectedPlan.name} ${capitalize(period)}`
- : `Switch to ${selectedPlan.name} ${capitalize(period)}`
+ : `Upgrade to ${selectedPlan.name} ${capitalize(period)}`
}
loading={clicked || !currentPlan}
- disabled={!workspaceSlug || (isCurrentPlan && !isTrialActive)}
+ disabled={!workspaceSlug || (isCurrentPlanAndPeriod && !isTrialActive)}
onClick={handleClick}
{...rest}
/>