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 ability to compare plans

Steven Tey593adb5b930b
diff --git a/apps/web/app/api/projects/[slug]/billing/upgrade/route.ts b/apps/web/app/api/projects/[slug]/billing/upgrade/route.ts
index a979853c37..5c7f089e44 100644
--- a/apps/web/app/api/projects/[slug]/billing/upgrade/route.ts
+++ b/apps/web/app/api/projects/[slug]/billing/upgrade/route.ts
@@ -4,7 +4,7 @@ import { APP_DOMAIN } from "@dub/utils";
import { NextResponse } from "next/server";
export const POST = withAuth(async ({ req, project, session }) => {
- let { plan, period, baseUrl } = await req.json();
+ let { plan, period, baseUrl, comparePlans } = await req.json();
if (!plan || !period) {
return new Response("Invalid plan or period", { status: 400 });
@@ -16,6 +16,7 @@ export const POST = withAuth(async ({ req, project, session }) => {
lookup_keys: [`${plan}_${period}`],
});
+ // if the user already has a subscription, create billing portal to upgrade
if (project.stripeId) {
const subscription = await stripe.subscriptions.list({
customer: project.stripeId,
@@ -25,21 +26,30 @@ export const POST = withAuth(async ({ req, project, session }) => {
const { url } = await stripe.billingPortal.sessions.create({
customer: project.stripeId,
return_url: `${baseUrl}?upgrade=${plan}`,
- flow_data: {
- type: "subscription_update_confirm",
- subscription_update_confirm: {
- subscription: subscription.data[0].id,
- items: [
- {
- id: subscription.data[0].items.data[0].id,
- quantity: 1,
- price: prices.data[0].id,
+ flow_data: comparePlans
+ ? {
+ type: "subscription_update",
+ subscription_update: {
+ subscription: subscription.data[0].id,
},
- ],
- },
- },
+ }
+ : {
+ type: "subscription_update_confirm",
+ subscription_update_confirm: {
+ subscription: subscription.data[0].id,
+ items: [
+ {
+ id: subscription.data[0].items.data[0].id,
+ quantity: 1,
+ price: prices.data[0].id,
+ },
+ ],
+ },
+ },
});
return NextResponse.json(url);
+
+ // if the user does not have a subscription, create a new checkout session
} else {
const stripeSession = await stripe.checkout.sessions.create({
customer_email: session.user.email,
diff --git a/apps/web/ui/modals/upgrade-plan-modal.tsx b/apps/web/ui/modals/upgrade-plan-modal.tsx
index e06a0c524e..cb7e2f912a 100644
--- a/apps/web/ui/modals/upgrade-plan-modal.tsx
+++ b/apps/web/ui/modals/upgrade-plan-modal.tsx
@@ -5,6 +5,7 @@ import {
Badge,
Button,
IconMenu,
+ LoadingSpinner,
Logo,
Modal,
Popover,
@@ -65,6 +66,7 @@ function UpgradePlanModal({
const [openPeriodSelector, setOpenPeriodSelector] = useState(false);
const [clicked, setClicked] = useState(false);
+ const [clickedCompare, setClickedCompare] = useState(false);
const { queryParams } = useRouterStuff();
return (
@@ -294,13 +296,53 @@ function UpgradePlanModal({
}}
/>
<div className="mt-2 flex items-center justify-center space-x-2">
- <a
- href={`${HOME_DOMAIN}/pricing`}
- target="_blank"
- className="text-center text-xs text-gray-500 underline-offset-4 transition-all hover:text-gray-800 hover:underline"
- >
- Compare plans
- </a>
+ {currentPlan === "free" ? (
+ <a
+ href={`${HOME_DOMAIN}/pricing`}
+ target="_blank"
+ className="text-center text-xs text-gray-500 underline-offset-4 transition-all hover:text-gray-800 hover:underline"
+ >
+ Compare plans
+ </a>
+ ) : (
+ <button
+ onClick={() => {
+ setClickedCompare(true);
+ fetch(`/api/projects/${slug}/billing/upgrade`, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ plan,
+ period,
+ baseUrl: `${APP_DOMAIN}${pathname}`,
+ comparePlans: true,
+ }),
+ })
+ .then(async (res) => {
+ const url = await res.json();
+ router.push(url);
+ })
+ .catch((err) => {
+ alert(err);
+ setClicked(false);
+ });
+ }}
+ disabled={clickedCompare}
+ className={cn(
+ "flex items-center space-x-2 text-center text-xs text-gray-500",
+ clickedCompare
+ ? "cursor-not-allowed"
+ : "underline-offset-4 transition-all hover:text-gray-800 hover:underline",
+ )}
+ >
+ {clickedCompare && (
+ <LoadingSpinner className="h-4 w-4" aria-hidden="true" />
+ )}
+ <p>Compare plans</p>
+ </button>
+ )}
<p className="text-gray-500">•</p>
{welcomeFlow ? (
<Link