Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: enable upgrade if no stripeId but higher plan
Marc Seitz97c92d282cd2
diff --git a/components/billing/upgrade-plan-modal-old.tsx b/components/billing/upgrade-plan-modal-old.tsxindex 15393fdb..7acbfc4a 100644--- a/components/billing/upgrade-plan-modal-old.tsx+++ b/components/billing/upgrade-plan-modal-old.tsx@@ -41,7 +41,7 @@ export function UpgradePlanModal({const [period, setPeriod] = useState<"yearly" | "monthly">("yearly");const [clicked, setClicked] = useState<boolean>(false);const teamInfo = useTeam();- const { plan: teamPlan, trial } = usePlan();+ const { plan: teamPlan, trial, isCustomer } = usePlan();const analytics = useAnalytics();const isTrial = !!trial;@@ -227,7 +227,7 @@ export function UpgradePlanModal({// @ts-ignore// prettier-ignore- if (teamPlan !== "free") {+ if (isCustomer && teamPlan !== "free") {fetch(`/api/teams/${teamInfo?.currentTeam?.id}/billing/manage`,{diff --git a/components/billing/upgrade-plan-modal.tsx b/components/billing/upgrade-plan-modal.tsxindex 4090dc2c..42604314 100644--- a/components/billing/upgrade-plan-modal.tsx+++ b/components/billing/upgrade-plan-modal.tsx@@ -43,7 +43,7 @@ export function UpgradePlanModal({const [period, setPeriod] = useState<"yearly" | "monthly">("yearly");const [clicked, setClicked] = useState<boolean>(false);const teamInfo = useTeam();- const { plan: teamPlan, trial } = usePlan();+ const { plan: teamPlan, trial, isCustomer } = usePlan();const analytics = useAnalytics();const isTrial = !!trial;@@ -234,58 +234,61 @@ export function UpgradePlanModal({? "bg-[#fb7a00] hover:bg-[#fb7a00]": "bg-gray-800 text-white hover:bg-gray-900 hover:text-white"}`}+ loading={clicked}onClick={() => {setClicked(true);- // @ts-ignore- // prettier-ignore-- if (teamPlan !== "free") {- fetch(- `/api/teams/${teamInfo?.currentTeam?.id}/billing/manage`,- {- method: "POST",- },- )- .then(async (res) => {- const url = await res.json();- router.push(url);- })- .catch((err) => {- alert(err);- setClicked(false);- });- } else {- fetch(- `/api/teams/${- teamInfo?.currentTeam?.id- }/billing/upgrade?priceId=${- PLANS.find((p) => p.name === planOption)!.price[period]- .priceIds[- process.env.NEXT_PUBLIC_VERCEL_ENV === "production"- ? "production"- : "test"- ]- }`,- {- method: "POST",- headers: {- "Content-Type": "application/json",+ if (isCustomer && teamPlan !== "free") {+ fetch(+ `/api/teams/${teamInfo?.currentTeam?.id}/billing/manage`,+ {+ method: "POST",+ },+ )+ .then(async (res) => {+ const url = await res.json();+ router.push(url);+ })+ .catch((err) => {+ alert(err);+ setClicked(false);+ });+ } else {+ fetch(+ `/api/teams/${+ teamInfo?.currentTeam?.id+ }/billing/upgrade?priceId=${+ PLANS.find((p) => p.name === planOption)!.price[+ period+ ].priceIds[+ process.env.NEXT_PUBLIC_VERCEL_ENV === "production"+ ? "production"+ : "test"+ ]+ }`,+ {+ method: "POST",+ headers: {+ "Content-Type": "application/json",+ },},- },- )- .then(async (res) => {- const data = await res.json();- const { id: sessionId } = data;- const stripe = await getStripe();- stripe?.redirectToCheckout({ sessionId });- })- .catch((err) => {- alert(err);- setClicked(false);- });+ )+ .then(async (res) => {+ const data = await res.json();+ const { id: sessionId } = data;+ const stripe = await getStripe();+ stripe?.redirectToCheckout({ sessionId });+ })+ .catch((err) => {+ alert(err);+ setClicked(false);+ });}}}- >{`Upgrade to ${planOption} ${capitalize(period)}`}</Button>+ >+ {clicked+ ? "Redirecting to Stripe..."+ : `Upgrade to ${planOption} ${capitalize(period)}`}+ </Button></div></div>))}diff --git a/lib/swr/use-billing.ts b/lib/swr/use-billing.tsindex 957bd9d2..5173d004 100644--- a/lib/swr/use-billing.ts+++ b/lib/swr/use-billing.ts@@ -48,6 +48,7 @@ type PlanWithTrial = `${BasePlan}+drtrial`;type PlanResponse = {plan: BasePlan | PlanWithTrial;+ isCustomer: boolean;};interface PlanDetails {@@ -81,6 +82,7 @@ export function usePlan() {return {plan: parsedPlan.plan,trial: parsedPlan.trial,+ isCustomer: plan?.isCustomer,loading: !plan && !error,error,};diff --git a/pages/api/teams/[teamId]/billing/index.ts b/pages/api/teams/[teamId]/billing/index.tsindex 84f0b3d8..7fead2d1 100644--- a/pages/api/teams/[teamId]/billing/index.ts+++ b/pages/api/teams/[teamId]/billing/index.ts@@ -13,7 +13,7 @@ export default async function handle(res: NextApiResponse,) {if (req.method === "GET") {- // POST /api/teams/:teamId/billing/manage – manage a user's subscription+ // POST /api/teams/:teamId/billing – get user's subscription infoconst session = await getServerSession(req, res, authOptions);if (!session) {res.status(401).end("Unauthorized");diff --git a/pages/api/teams/[teamId]/billing/plan.ts b/pages/api/teams/[teamId]/billing/plan.tsindex a6db9943..dc12cde7 100644--- a/pages/api/teams/[teamId]/billing/plan.ts+++ b/pages/api/teams/[teamId]/billing/plan.ts@@ -34,10 +34,13 @@ export default async function handle(},select: {plan: true,+ stripeId: true,},});- return res.status(200).json(team);+ const isCustomer = !!team?.stripeId;++ return res.status(200).json({ plan: team?.plan, isCustomer });} catch (error) {errorhandler(error, res);}diff --git a/pages/settings/billing.tsx b/pages/settings/billing.tsxindex 23e84c2e..599076ae 100644--- a/pages/settings/billing.tsx+++ b/pages/settings/billing.tsx@@ -30,7 +30,7 @@ const frequencies: {export default function Billing() {const router = useRouter();const analytics = useAnalytics();- const { plan } = usePlan();+ const { plan, isCustomer } = usePlan();const [clicked, setClicked] = useState<boolean>(false);const frequency = frequencies[1];const [toggleProYear, setToggleProYear] = useState<boolean>(true);@@ -298,9 +298,10 @@ export default function Billing() {<div className="p-6">{tier.id !=="tier-free" /** hide button on free tier */ ? (- tier.currentPlan ? (+ tier.currentPlan && isCustomer ? (<ButtonclassName="rounded-3xl"+ loading={clicked}onClick={() => {setClicked(true);fetch(@@ -318,16 +319,18 @@ export default function Billing() {setClicked(false);});}}- loading={clicked}>- Manage Subscription+ {clicked+ ? "Redirecting to Customer Portal..."+ : "Manage Subscription"}</Button>- ) : plan !== "free" ? (+ ) : plan !== "free" && isCustomer ? (<ButtonclassName="rounded-3xl"variant={tier.id === "tier-business" ? "orange" : "default"}+ loading={clicked}onClick={() => {setClicked(true);fetch(@@ -345,9 +348,10 @@ export default function Billing() {setClicked(false);});}}- loading={clicked}>- {tier.buttonText}+ {clicked+ ? "Redirecting to Customer Portal..."+ : tier.buttonText}</Button>) : (<UpgradePlanModaldiff --git a/pages/settings/upgrade.tsx b/pages/settings/upgrade.tsxindex bcbcab9d..ee9e0432 100644--- a/pages/settings/upgrade.tsx+++ b/pages/settings/upgrade.tsx@@ -20,7 +20,7 @@ export default function UpgradePage() {const [period, setPeriod] = useState<"yearly" | "monthly">("yearly");const [clicked, setClicked] = useState<boolean>(false);const teamInfo = useTeam();- const { plan: teamPlan, trial } = usePlan();+ const { plan: teamPlan, trial, isCustomer } = usePlan();const analytics = useAnalytics();const planFeatures = useMemo(@@ -149,12 +149,10 @@ export default function UpgradePage() {? "bg-[#fb7a00] hover:bg-[#fb7a00]/80": "bg-gray-800 text-white hover:bg-gray-900"}`}+ loading={clicked}onClick={() => {setClicked(true);- // @ts-ignore- // prettier-ignore-- if (teamPlan !== "free") {+ if (isCustomer && teamPlan !== "free") {fetch(`/api/teams/${teamInfo?.currentTeam?.id}/billing/manage`,{@@ -170,7 +168,6 @@ export default function UpgradePage() {setClicked(false);});} else {-fetch(`/api/teams/${teamInfo?.currentTeam?.id@@ -199,9 +196,13 @@ export default function UpgradePage() {alert(err);setClicked(false);});- }+ }}}- >{`Upgrade to ${planOption} ${capitalize(period)}`}</Button>+ >+ {clicked+ ? "Redirecting to Stripe..."+ : `Upgrade to ${planOption} ${capitalize(period)}`}+ </Button></div></div>))}