diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex index a7fcf4814..c9c751856 100644 --- a/lib/plausible/billing/billing.ex +++ b/lib/plausible/billing/billing.ex @@ -1,7 +1,7 @@ defmodule Plausible.Billing do use Plausible.Repo require Plausible.Billing.Subscription.Status - alias Plausible.Billing.Subscription + alias Plausible.Billing.{Subscription, Plans, Quota} @spec active_subscription_for(integer()) :: Subscription.t() | nil def active_subscription_for(user_id) do @@ -39,7 +39,13 @@ defmodule Plausible.Billing do def change_plan(user, new_plan_id) do subscription = active_subscription_for(user.id) + plan = Plans.find(new_plan_id) + with :ok <- Quota.ensure_can_subscribe_to_plan(user, plan), + do: do_change_plan(subscription, new_plan_id) + end + + defp do_change_plan(subscription, new_plan_id) do res = paddle_api().update_subscription(subscription.paddle_subscription_id, %{ plan_id: new_plan_id diff --git a/lib/plausible/billing/plans.ex b/lib/plausible/billing/plans.ex index dd397db9c..2f9065dea 100644 --- a/lib/plausible/billing/plans.ex +++ b/lib/plausible/billing/plans.ex @@ -79,9 +79,9 @@ defmodule Plausible.Billing.Plans do do: yearly_product_id end - defp find(nil), do: nil + def find(nil), do: nil - defp find(product_id) do + def find(product_id) do Enum.find(all(), fn plan -> product_id in [plan.monthly_product_id, plan.yearly_product_id] end) diff --git a/lib/plausible/billing/quota.ex b/lib/plausible/billing/quota.ex index acf88ab87..b429524cd 100644 --- a/lib/plausible/billing/quota.ex +++ b/lib/plausible/billing/quota.ex @@ -8,6 +8,21 @@ defmodule Plausible.Billing.Quota do alias Plausible.Billing.{Plan, Plans, Subscription, EnterprisePlan, Feature} alias Plausible.Billing.Feature.{Goals, RevenueGoals, Funnels, Props} + def usage(user, opts \\ []) do + basic_usage = %{ + monthly_pageviews: monthly_pageview_usage(user), + team_members: team_member_usage(user), + sites: site_usage(user) + } + + if Keyword.get(opts, :with_features) == true do + basic_usage + |> Map.put(:features, features_usage(user)) + else + basic_usage + end + end + @limit_sites_since ~D[2021-05-05] @spec site_limit(Plausible.Auth.User.t()) :: non_neg_integer() | :unlimited @doc """ @@ -169,10 +184,30 @@ defmodule Plausible.Billing.Quota do ] Enum.reduce(queries, [], fn {feature, query}, acc -> - if Plausible.Repo.exists?(query), do: [feature | acc], else: acc + if Plausible.Repo.exists?(query), do: acc ++ [feature], else: acc end) end + def ensure_can_subscribe_to_plan(user, %Plan{} = plan) do + case exceeded_limits(usage(user), plan) do + [] -> :ok + exceeded_limits -> {:error, %{exceeded_limits: exceeded_limits}} + end + end + + def ensure_can_subscribe_to_plan(_user, nil), do: :ok + + def exceeded_limits(usage, %Plan{} = plan) do + for {usage_field, limit_field} <- [ + {:monthly_pageviews, :monthly_pageview_limit}, + {:team_members, :team_member_limit}, + {:sites, :site_limit} + ], + !within_limit?(Map.get(usage, usage_field), Map.get(plan, limit_field)) do + limit_field + end + end + @doc """ Returns a list of features the user can use. Trial users have the ability to use all features during their trial. diff --git a/lib/plausible_web/components/billing.ex b/lib/plausible_web/components/billing.ex index 1125d56b7..af6acc022 100644 --- a/lib/plausible_web/components/billing.ex +++ b/lib/plausible_web/components/billing.ex @@ -279,12 +279,28 @@ defmodule PlausibleWeb.Components.Billing do |> String.replace(".00", "") end + attr :id, :string, required: true + attr :paddle_product_id, :string, required: true + attr :checkout_disabled, :boolean, default: false + attr :user, :map, required: true + attr :confirm_message, :any, default: nil + slot :inner_block, required: true + def paddle_button(assigns) do + confirmed = + if assigns.confirm_message, do: "confirm(\"#{assigns.confirm_message}\")", else: "true" + + assigns = assign(assigns, :confirmed, confirmed) + ~H""" diff --git a/lib/plausible_web/controllers/billing_controller.ex b/lib/plausible_web/controllers/billing_controller.ex index 1bf953e88..e8a67de81 100644 --- a/lib/plausible_web/controllers/billing_controller.ex +++ b/lib/plausible_web/controllers/billing_controller.ex @@ -126,7 +126,15 @@ defmodule PlausibleWeb.BillingController do def change_plan_preview(conn, %{"plan_id" => new_plan_id}) do with {:ok, {subscription, preview_info}} <- preview_subscription(conn.assigns.current_user, new_plan_id) do + back_action = + if FunWithFlags.enabled?(:business_tier, for: conn.assigns.current_user) do + :choose_plan + else + :change_plan_form + end + render(conn, "change_plan_preview.html", + back_link: Routes.billing_path(conn, back_action), skip_plausible_tracking: true, subscription: subscription, preview_info: preview_info, @@ -139,17 +147,20 @@ defmodule PlausibleWeb.BillingController do end def change_plan(conn, %{"new_plan_id" => new_plan_id}) do - case Billing.change_plan(conn.assigns[:current_user], new_plan_id) do + case Billing.change_plan(conn.assigns.current_user, new_plan_id) do {:ok, _subscription} -> conn |> put_flash(:success, "Plan changed successfully") |> redirect(to: "/settings") {:error, e} -> - # https://developer.paddle.com/api-reference/intro/api-error-codes msg = case e do + %{exceeded_limits: exceeded_limits} -> + "Unable to subscribe to this plan because the following limits are exceeded: #{inspect(exceeded_limits)}" + %{"code" => 147} -> + # https://developer.paddle.com/api-reference/intro/api-error-codes "We were unable to charge your card. Click 'update billing info' to update your payment details and try again." %{"message" => msg} when not is_nil(msg) -> diff --git a/lib/plausible_web/live/choose_plan.ex b/lib/plausible_web/live/choose_plan.ex index 827de3c0e..55dda19d0 100644 --- a/lib/plausible_web/live/choose_plan.ex +++ b/lib/plausible_web/live/choose_plan.ex @@ -23,7 +23,7 @@ defmodule PlausibleWeb.Live.ChoosePlan do Users.with_subscription(user_id) end) |> assign_new(:usage, fn %{user: user} -> - Quota.monthly_pageview_usage(user) + Quota.usage(user, with_features: true) end) |> assign_new(:owned_plan, fn %{user: %{subscription: subscription}} -> Plans.get_regular_plan(subscription, only_non_expired: true) @@ -42,7 +42,7 @@ defmodule PlausibleWeb.Live.ChoosePlan do usage: usage, available_volumes: available_volumes } -> - default_selected_volume(owned_plan, usage, available_volumes) + default_selected_volume(owned_plan, usage.monthly_pageviews, available_volumes) end) |> assign_new(:selected_interval, fn %{current_interval: current_interval} -> current_interval || :monthly @@ -118,7 +118,8 @@ defmodule PlausibleWeb.Live.ChoosePlan do <.enterprise_plan_box benefits={@enterprise_benefits} />
- <.usage usage={@usage} /> + You have used <%= PlausibleWeb.AuthView.delimit_integer(@usage.monthly_pageviews) %> + billable pageviews in the last 30 days
<.pageview_limit_notice :if={!@owned_plan} /> <.help_links /> @@ -160,8 +161,8 @@ defmodule PlausibleWeb.Live.ChoosePlan do defp default_selected_volume(%Plan{monthly_pageview_limit: limit}, _, _), do: limit - defp default_selected_volume(_, usage, available_volumes) do - Enum.find(available_volumes, &(usage < &1)) || :enterprise + defp default_selected_volume(_, pageview_usage, available_volumes) do + Enum.find(available_volumes, &(pageview_usage < &1)) || :enterprise end defp current_user_subscription_interval(subscription) do @@ -261,30 +262,10 @@ defmodule PlausibleWeb.Live.ChoosePlan do+ <%= @disabled_message %> +
+ """ + end + + defp losing_features_message([]), do: nil + + defp losing_features_message(features_to_lose) do + features_list_str = + features_to_lose + |> Enum.map(& &1.display_name) + |> PlausibleWeb.TextHelpers.pretty_join() + + "This plan does not support #{features_list_str}, which you are currently using. Please note that by subscribing to this plan you will lose access to #{if length(features_to_lose) == 1, do: "this feature", else: "these features"}." + end + defp growth_grandfathering_notice(assigns) do ~H"""- Please update your billing details first -
""" end @@ -446,13 +469,6 @@ defmodule PlausibleWeb.Live.ChoosePlan do """ end - defp usage(assigns) do - ~H""" - You have used <%= PlausibleWeb.AuthView.delimit_integer(@usage) %> - billable pageviews in the last 30 days - """ - end - defp pageview_limit_notice(assigns) do ~H"""