From 6d6987d2bccc5e20cd4d377135ccdcdafc57598d Mon Sep 17 00:00:00 2001 From: Shubham Palriwala Date: Mon, 6 Nov 2023 14:23:49 +0530 Subject: [PATCH] feat: $199 pricing model for unlimited plans (#1564) --- .../settings/billing/actions.ts | 3 +- .../billing/components/PricingTable.tsx | 11 +++-- .../settings/billing/unlimited/page.tsx | 41 +++++++++++++++++++ .../handlers/checkoutSessionCompleted.ts | 13 +++--- packages/ee/billing/lib/constants.ts | 6 +-- 5 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 apps/web/app/(app)/environments/[environmentId]/settings/billing/unlimited/page.tsx diff --git a/apps/web/app/(app)/environments/[environmentId]/settings/billing/actions.ts b/apps/web/app/(app)/environments/[environmentId]/settings/billing/actions.ts index b49b6751ef..33f58caf76 100644 --- a/apps/web/app/(app)/environments/[environmentId]/settings/billing/actions.ts +++ b/apps/web/app/(app)/environments/[environmentId]/settings/billing/actions.ts @@ -23,8 +23,7 @@ export async function upgradePlanAction( if (!isAuthorized) throw new AuthorizationError("Not authorized"); const subscriptionSession = await createSubscription(teamId, environmentId, priceLookupKeys); - - return subscriptionSession.url; + return subscriptionSession; } export async function manageSubscriptionAction(teamId: string, environmentId: string) { diff --git a/apps/web/app/(app)/environments/[environmentId]/settings/billing/components/PricingTable.tsx b/apps/web/app/(app)/environments/[environmentId]/settings/billing/components/PricingTable.tsx index 6659b1428e..552335e83e 100644 --- a/apps/web/app/(app)/environments/[environmentId]/settings/billing/components/PricingTable.tsx +++ b/apps/web/app/(app)/environments/[environmentId]/settings/billing/components/PricingTable.tsx @@ -50,13 +50,18 @@ export default function PricingTableComponent({ const upgradePlan = async (priceLookupKeys: StripePriceLookupKeys[]) => { try { setUpgradingPlan(true); - const paymentUrl = await upgradePlanAction(team.id, environmentId, priceLookupKeys); + const { status, newPlan, url } = await upgradePlanAction(team.id, environmentId, priceLookupKeys); setUpgradingPlan(false); - if (!paymentUrl || paymentUrl === "") { + if (status != 200) { + throw new Error("Something went wrong"); + } + if (!newPlan) { toast.success("Plan upgraded successfully"); router.refresh(); + } else if (newPlan && url) { + router.push(url); } else { - router.push(paymentUrl); + throw new Error("Something went wrong"); } } catch (err) { toast.error("Unable to upgrade plan"); diff --git a/apps/web/app/(app)/environments/[environmentId]/settings/billing/unlimited/page.tsx b/apps/web/app/(app)/environments/[environmentId]/settings/billing/unlimited/page.tsx new file mode 100644 index 0000000000..611fe52d00 --- /dev/null +++ b/apps/web/app/(app)/environments/[environmentId]/settings/billing/unlimited/page.tsx @@ -0,0 +1,41 @@ +import { authOptions } from "@formbricks/lib/authOptions"; +import { IS_FORMBRICKS_CLOUD } from "@formbricks/lib/constants"; +import { getTeamByEnvironmentId } from "@formbricks/lib/team/service"; +import { upgradePlanAction } from "../actions"; +import { notFound, redirect } from "next/navigation"; +import { getServerSession } from "next-auth"; +import { StripePriceLookupKeys } from "@formbricks/ee/billing/lib/constants"; + +export default async function UnlimitedPage({ params }) { + if (!IS_FORMBRICKS_CLOUD) { + notFound(); + } + + const session = await getServerSession(authOptions); + + const team = await getTeamByEnvironmentId(params.environmentId); + + if (!session) { + throw new Error("Unauthorized"); + } + + if (!team) { + throw new Error("Team not found"); + } + + const { status, newPlan, url } = await upgradePlanAction(team.id, params.environmentId, [ + StripePriceLookupKeys.inAppSurveyUnlimited, + StripePriceLookupKeys.linkSurveyUnlimited, + StripePriceLookupKeys.userTargetingUnlimited, + ]); + if (status != 200) { + throw new Error("Something went wrong"); + } + if (newPlan && url) { + redirect(url); + } else if (!newPlan) { + redirect(`/billing-confirmation?environmentId=${params.environmentId}`); + } else { + throw new Error("Something went wrong"); + } +} diff --git a/packages/ee/billing/handlers/checkoutSessionCompleted.ts b/packages/ee/billing/handlers/checkoutSessionCompleted.ts index b3d2673554..73da611bd8 100644 --- a/packages/ee/billing/handlers/checkoutSessionCompleted.ts +++ b/packages/ee/billing/handlers/checkoutSessionCompleted.ts @@ -69,15 +69,18 @@ export const handleCheckoutSessionCompleted = async (event: Stripe.Event) => { } } - await stripe.customers.update(stripeCustomer.id, { - name: team.name, - metadata: { team: team.id }, - }); - await updateTeam(team.id, { billing: { stripeCustomerId: stripeCustomer.id, features: updatedFeatures, }, }); + + await stripe.customers.update(stripeCustomer.id, { + name: team.name, + metadata: { team: team.id }, + invoice_settings: { + default_payment_method: stripeSubscriptionObject.default_payment_method as string, + }, + }); }; diff --git a/packages/ee/billing/lib/constants.ts b/packages/ee/billing/lib/constants.ts index 78135b3fb8..da145ae452 100644 --- a/packages/ee/billing/lib/constants.ts +++ b/packages/ee/billing/lib/constants.ts @@ -13,7 +13,7 @@ export enum StripePriceLookupKeys { inAppSurvey = "inAppSurvey", linkSurvey = "linkSurvey", userTargeting = "userTargeting", - inAppSurveyUnlimited = "survey-unlimited-30102023", - linkSurveyUnlimited = "linkSurvey-unlimited-30102023", - userTargetingUnlimited = "userTargeting-unlimited-30102023", + inAppSurveyUnlimited = "survey-unlimited-03112023", + linkSurveyUnlimited = "linkSurvey-unlimited-03112023", + userTargetingUnlimited = "userTargeting-unlimited-03112023", }