From d66322e12dffe395d283c5132d240b8e5f793661 Mon Sep 17 00:00:00 2001 From: RobertJoonas <56999674+RobertJoonas@users.noreply.github.com> Date: Thu, 16 Nov 2023 15:40:50 +0000 Subject: [PATCH] Limit grandfathering to *active* subscribers and trials (#3524) * refactor asserting plan generation in plans_test.exs * stop grandfathering old expired trials For users who registered before the business tiers release, we want to offer a chance to subscribe to a grandfathered plan. However, if they let their trial expire and don't subscribe in the next 10 days, they'll lose that opportunity. * stop grandfathering expired subscriptions * remove default title and icon from Generic.notice * fix bug with dismissable notice classList is null when dismissable_id is not given * alias Plausible.Auth.User * Refactor Generic.notice component Make it easy to apply different colors * move subscription_cancelled_notice across the app And remove from user settings > subscription box. Also, include a note about losing grandfathered status when letting the subscription expire. * allow full width in Generic.notice * use Generic.notice for subscription_past_due_notice * use Generic.notice for subscription_paused_notice * prevent two notices clashing into each other with gap-y-2 * define attrs for phx components * optimize for light mode * make subscription cancelled notice dismissable but if it's dismiss, show it in the place where it was before in the account settings > subscription box * make function private * replace function doc with regular comment to avoid compile warning * use array for classnames Co-authored-by: Vinicius Brasil * fix typos in function doc --------- Co-authored-by: Vinicius Brasil --- lib/plausible/billing/billing.ex | 4 + lib/plausible/billing/plans.ex | 24 +- lib/plausible_web/components/billing.ex | 219 ++++++++++++------ lib/plausible_web/components/generic.ex | 39 +++- .../templates/auth/user_settings.html.heex | 39 +--- .../templates/layout/_notice.html.heex | 22 +- test/plausible/billing/plans_test.exs | 132 ++++++++--- .../controllers/auth_controller_test.exs | 62 +++++ test/plausible_web/live/choose_plan_test.exs | 2 +- test/support/test_utils.ex | 7 +- 10 files changed, 389 insertions(+), 161 deletions(-) diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex index e4d1089d1..0ae80dcf8 100644 --- a/lib/plausible/billing/billing.ex +++ b/lib/plausible/billing/billing.ex @@ -281,6 +281,10 @@ defmodule Plausible.Billing do def paddle_api(), do: Application.fetch_env!(:plausible, :paddle_api) + def cancelled_subscription_notice_dismiss_id(%Plausible.Auth.User{} = user) do + "subscription_cancelled__#{user.id}" + end + defp active_subscription_query(user_id) do from(s in Subscription, where: s.user_id == ^user_id and s.status == ^Subscription.Status.active(), diff --git a/lib/plausible/billing/plans.ex b/lib/plausible/billing/plans.ex index d0df2f556..469705fef 100644 --- a/lib/plausible/billing/plans.ex +++ b/lib/plausible/billing/plans.ex @@ -38,16 +38,17 @@ defmodule Plausible.Billing.Plans do still choose from old plans. """ # credo:disable-for-next-line Credo.Check.Refactor.CyclomaticComplexity - def growth_plans_for(%User{} = user) do + def growth_plans_for(%User{} = user, now \\ Timex.now()) do user = Plausible.Users.with_subscription(user) v4_available = FunWithFlags.enabled?(:business_tier, for: user) owned_plan = get_regular_plan(user.subscription) cond do Application.get_env(:plausible, :environment) == "dev" -> @sandbox_plans - is_nil(owned_plan) && Timex.before?(user.inserted_at, @business_tier_launch) -> @plans_v3 + is_nil(owned_plan) && grandfathered_trial?(user.trial_expiry_date, now) -> @plans_v3 is_nil(owned_plan) && v4_available -> @plans_v4 is_nil(owned_plan) -> @plans_v3 + user.subscription && Subscriptions.expired?(user.subscription) -> @plans_v4 owned_plan.kind == :business -> @plans_v4 owned_plan.generation == 1 -> @plans_v1 owned_plan.generation == 2 -> @plans_v2 @@ -57,19 +58,34 @@ defmodule Plausible.Billing.Plans do |> Enum.filter(&(&1.kind == :growth)) end - def business_plans_for(%User{} = user) do + def business_plans_for(%User{} = user, now \\ Timex.now()) do user = Plausible.Users.with_subscription(user) owned_plan = get_regular_plan(user.subscription) cond do Application.get_env(:plausible, :environment) == "dev" -> @sandbox_plans - is_nil(owned_plan) && Timex.before?(user.inserted_at, @business_tier_launch) -> @plans_v3 + is_nil(owned_plan) && grandfathered_trial?(user.trial_expiry_date, now) -> @plans_v3 + user.subscription && Subscriptions.expired?(user.subscription) -> @plans_v4 owned_plan && owned_plan.generation < 4 -> @plans_v3 true -> @plans_v4 end |> Enum.filter(&(&1.kind == :business)) end + # Takes a Date struct argument representing the trial end date of a user. + # If the `trial_expiry` is `nil`, it means that the user has not started + # their trial yet (i.e. invited user), and this function returns false. + defp grandfathered_trial?(nil, _now), do: false + + defp grandfathered_trial?(trial_expiry, now) do + trial_start = Timex.shift(trial_expiry, days: -30) + + joined_before_business_tiers = Timex.before?(trial_start, @business_tier_launch) + trial_active_or_expired_less_than_10d_ago = Timex.diff(now, trial_expiry, :days) <= 10 + + joined_before_business_tiers && trial_active_or_expired_less_than_10d_ago + end + def available_plans_for(%User{} = user, opts \\ []) do plans = growth_plans_for(user) ++ business_plans_for(user) diff --git a/lib/plausible_web/components/billing.ex b/lib/plausible_web/components/billing.ex index 6628478b4..073ef1f42 100644 --- a/lib/plausible_web/components/billing.ex +++ b/lib/plausible_web/components/billing.ex @@ -4,13 +4,14 @@ defmodule PlausibleWeb.Components.Billing do use Phoenix.Component import PlausibleWeb.Components.Generic require Plausible.Billing.Subscription.Status + alias Plausible.Auth.User alias Plausible.Billing.Feature.{RevenueGoals, Funnels} alias Plausible.Billing.Feature.{Props, StatsAPI} alias PlausibleWeb.Router.Helpers, as: Routes alias Plausible.Billing.{Subscription, Plans, Plan, Subscriptions} - attr(:billable_user, Plausible.Auth.User, required: true) - attr(:current_user, Plausible.Auth.User, required: true) + attr(:billable_user, User, required: true) + attr(:current_user, User, required: true) attr(:feature_mod, :atom, required: true, values: Plausible.Billing.Feature.list()) attr(:grandfathered?, :boolean, default: false) attr(:size, :atom, default: :sm) @@ -33,7 +34,7 @@ defmodule PlausibleWeb.Components.Billing do not has_access? -> ~H""" - <.notice class="rounded-t-md rounded-b-none" size={@size} {@rest}> + <.notice class="rounded-t-md rounded-b-none" size={@size} {@rest} title="Notice"> <%= account_label(@current_user, @billable_user) %> does not have access to <%= assigns.feature_mod.display_name() %>. To get access to this feature, <.upgrade_call_to_action current_user={@current_user} billable_user={@billable_user} />. @@ -56,8 +57,8 @@ defmodule PlausibleWeb.Components.Billing do end end - attr(:billable_user, Plausible.Auth.User, required: true) - attr(:current_user, Plausible.Auth.User, required: true) + attr(:billable_user, User, required: true) + attr(:current_user, User, required: true) attr(:limit, :integer, required: true) attr(:resource, :string, required: true) attr(:rest, :global) @@ -65,7 +66,7 @@ defmodule PlausibleWeb.Components.Billing do # credo:disable-for-next-line Credo.Check.Refactor.CyclomaticComplexity def limit_exceeded_notice(assigns) do ~H""" - <.notice {@rest}> + <.notice {@rest} title="Notice"> <%= account_label(@current_user, @billable_user) %> is limited to <%= @limit %> <%= @resource %>. To increase this limit, <.upgrade_call_to_action current_user={@current_user} billable_user={@billable_user} />. @@ -215,86 +216,120 @@ defmodule PlausibleWeb.Components.Billing do """ end + attr(:user, :map, required: true) + attr(:dismissable, :boolean, default: true) + + @doc """ + Given a user with a cancelled subscription, this component renders a cancelled + subscription notice. If the given user does not have a subscription or it has a + different status, this function returns an empty template. + + It also takes a dismissable argument which renders the notice dismissable (with + the help of JavaScript and localStorage). We show a dismissable notice about a + cancelled subscription across the app, but when the user dismisses it, we will + start displaying it in the account settings > subscription section instead. + + So it's either shown across the app, or only on the /settings page. Depending + on whether the localStorage flag to dismiss it has been set or not. + """ + def subscription_cancelled_notice(assigns) + + def subscription_cancelled_notice( + %{ + dismissable: true, + user: %User{subscription: %Subscription{status: Subscription.Status.deleted()}} + } = + assigns + ) do + ~H""" + + """ + end + + def subscription_cancelled_notice( + %{ + dismissable: false, + user: %User{subscription: %Subscription{status: Subscription.Status.deleted()}} + } = + assigns + ) do + assigns = assign(assigns, :container_id, "local-subscription-cancelled-notice") + + ~H""" + + + """ + end + + def subscription_cancelled_notice(assigns), do: ~H"" + + attr(:class, :string, default: "") + attr(:subscription, :any, default: nil) + def subscription_past_due_notice( %{subscription: %Subscription{status: Subscription.Status.past_due()}} = assigns ) do ~H""" """ end def subscription_past_due_notice(assigns), do: ~H"" + attr(:class, :string, default: "") + attr(:subscription, :any, default: nil) + def subscription_paused_notice( %{subscription: %Subscription{status: Subscription.Status.paused()}} = assigns ) do ~H""" """ end @@ -322,10 +357,11 @@ defmodule PlausibleWeb.Components.Billing do ~H"""
<.notice class="shadow-md dark:shadow-none" + title="Notice" dismissable_id={"premium_features_private_preview_end__#{@user.id}"} > Business plans are now live! The private preview of <%= PlausibleWeb.TextHelpers.pretty_join( @@ -432,6 +468,47 @@ defmodule PlausibleWeb.Components.Billing do """ end + defp subscription_cancelled_notice_body(assigns) do + if Plausible.Billing.Subscriptions.expired?(assigns.user.subscription) do + ~H""" + <.link class="underline inline-block" href={Plausible.Billing.upgrade_route_for(@user)}> + Upgrade your subscription + +

to get access to your stats again.

+ """ + else + ~H""" +

+ You have access to your stats until <%= Timex.format!(@user.subscription.next_bill_date, "{Mshort} {D}, {YYYY}") %>. + <.link class="underline inline-block" href={Plausible.Billing.upgrade_route_for(@user)}> + Upgrade your subscription + + to make sure you don't lose access. +

+ <.lose_grandfathering_warning user={@user} /> + """ + end + end + + defp lose_grandfathering_warning(%{user: %{subscription: subscription} = user} = assigns) do + business_tiers_available? = FunWithFlags.enabled?(:business_tier, for: user) + plan = Plans.get_regular_plan(subscription, only_non_expired: true) + loses_grandfathering = business_tiers_available? && plan && plan.generation < 4 + + assigns = assign(assigns, :loses_grandfathering, loses_grandfathering) + + ~H""" +

+ Please also note that by letting your subscription expire, you lose access to our grandfathered terms. If you want to subscribe again after that, your account will be offered the <.link + href="https://plausible.io/#pricing" + target="_blank" + rel="noopener noreferrer" + class="underline" + >latest pricing. +

+ """ + end + defp change_plan_or_upgrade_text(nil), do: "Upgrade" defp change_plan_or_upgrade_text(%Subscription{status: Subscription.Status.deleted()}), diff --git a/lib/plausible_web/components/generic.ex b/lib/plausible_web/components/generic.ex index f7fcbc955..ffeb3fcd8 100644 --- a/lib/plausible_web/components/generic.ex +++ b/lib/plausible_web/components/generic.ex @@ -4,6 +4,21 @@ defmodule PlausibleWeb.Components.Generic do """ use Phoenix.Component + @notice_themes %{ + yellow: %{ + bg: "bg-yellow-50 dark:bg-yellow-100", + icon: "text-yellow-400", + title_text: "text-yellow-800 dark:text-yellow-900", + body_text: "text-yellow-700 dark:text-yellow-800" + }, + red: %{ + bg: "bg-red-100", + icon: "text-red-700", + title_text: "text-red-800 dark:text-red-900", + body_text: "text-red-700 dark:text-red-800" + } + } + attr(:type, :string, default: "button") attr(:class, :string, default: "") attr(:disabled, :boolean, default: false) @@ -58,28 +73,31 @@ defmodule PlausibleWeb.Components.Generic do """ end - attr(:title, :string, default: "Notice") + attr(:title, :any, default: nil) attr(:size, :atom, default: :sm) + attr(:theme, :atom, default: :yellow) attr(:dismissable_id, :any, default: nil) attr(:class, :string, default: "") attr(:rest, :global) slot(:inner_block) def notice(assigns) do + assigns = assign(assigns, :theme, Map.fetch!(@notice_themes, assigns.theme)) + ~H"""
-
+
-
+
-
-

+
+

<%= @title %>

-
+

<%= render_slot(@inner_block) %>

@@ -107,7 +122,7 @@ defmodule PlausibleWeb.Components.Generic do
-