diff --git a/lib/plausible/billing/plans.ex b/lib/plausible/billing/plans.ex
index bd7860fdf..6a175338e 100644
--- a/lib/plausible/billing/plans.ex
+++ b/lib/plausible/billing/plans.ex
@@ -27,7 +27,7 @@ defmodule Plausible.Billing.Plans do
Module.put_attribute(__MODULE__, :external_resource, path)
end
- @business_tier_launch ~D[2023-11-07]
+ @business_tier_launch ~N[2023-12-01 12:00:00]
@spec growth_plans_for(User.t()) :: [Plan.t()]
@doc """
@@ -44,7 +44,9 @@ defmodule Plausible.Billing.Plans do
cond do
Application.get_env(:plausible, :environment) == "dev" -> @sandbox_plans
- !owned_plan -> if v4_available, do: @plans_v4, else: @plans_v3
+ is_nil(owned_plan) && Timex.before?(user.inserted_at, @business_tier_launch) -> @plans_v3
+ is_nil(owned_plan) && v4_available -> @plans_v4
+ is_nil(owned_plan) -> @plans_v3
owned_plan.kind == :business -> @plans_v4
owned_plan.generation == 1 -> @plans_v1
owned_plan.generation == 2 -> @plans_v2
@@ -60,6 +62,7 @@ defmodule Plausible.Billing.Plans do
cond do
Application.get_env(:plausible, :environment) == "dev" -> @sandbox_plans
+ is_nil(owned_plan) && Timex.before?(user.inserted_at, @business_tier_launch) -> @plans_v3
owned_plan && owned_plan.generation < 4 -> @plans_v3
true -> @plans_v4
end
@@ -242,6 +245,10 @@ defmodule Plausible.Billing.Plans do
end
defp all() do
- @legacy_plans ++ @plans_v1 ++ @plans_v2 ++ @plans_v3 ++ @plans_v4
+ @legacy_plans ++ @plans_v1 ++ @plans_v2 ++ @plans_v3 ++ @plans_v4 ++ sandbox_plans()
+ end
+
+ defp sandbox_plans() do
+ if Application.get_env(:plausible, :environment) == "dev", do: @sandbox_plans, else: []
end
end
diff --git a/lib/plausible/billing/quota.ex b/lib/plausible/billing/quota.ex
index d11f7333e..348e66354 100644
--- a/lib/plausible/billing/quota.ex
+++ b/lib/plausible/billing/quota.ex
@@ -230,7 +230,7 @@ defmodule Plausible.Billing.Quota do
case Plans.get_subscription_plan(user.subscription) do
%EnterprisePlan{} -> Feature.list()
%Plan{features: features} -> features
- :free_10k -> [Goals]
+ :free_10k -> [Goals, Props, StatsAPI]
nil -> Feature.list()
end
end
@@ -241,11 +241,21 @@ defmodule Plausible.Billing.Quota do
select: %{site_id: sm.site_id}
end
+ @spec below_limit?(non_neg_integer(), non_neg_integer() | :unlimited) :: boolean()
+ @doc """
+ Returns whether the usage is below the limit or not.
+ Returns false if usage is equal to the limit.
+ """
+ def below_limit?(usage, limit) do
+ if limit == :unlimited, do: true, else: usage < limit
+ end
+
@spec within_limit?(non_neg_integer(), non_neg_integer() | :unlimited) :: boolean()
@doc """
- Returns whether the limit has been exceeded or not.
+ Returns whether the usage is within the limit or not.
+ Returns true if usage is equal to the limit.
"""
def within_limit?(usage, limit) do
- if limit == :unlimited, do: true, else: usage < limit
+ if limit == :unlimited, do: true, else: usage <= limit
end
end
diff --git a/lib/plausible/site/memberships/create_invitation.ex b/lib/plausible/site/memberships/create_invitation.ex
index 91244d8d3..8bbe99ee4 100644
--- a/lib/plausible/site/memberships/create_invitation.ex
+++ b/lib/plausible/site/memberships/create_invitation.ex
@@ -117,14 +117,14 @@ defmodule Plausible.Site.Memberships.CreateInvitation do
site_usage = Plausible.Repo.aggregate(Quota.team_member_usage_query(site.owner, site), :count)
usage_after_transfer = current_usage + site_usage
- Quota.within_limit?(usage_after_transfer, limit)
+ Quota.below_limit?(usage_after_transfer, limit)
end
defp within_site_limit_after_transfer?(new_owner) do
limit = Quota.site_limit(new_owner)
usage_after_transfer = Quota.site_usage(new_owner) + 1
- Quota.within_limit?(usage_after_transfer, limit)
+ Quota.below_limit?(usage_after_transfer, limit)
end
defp has_access_to_site_features?(site, new_owner) do
@@ -174,7 +174,7 @@ defmodule Plausible.Site.Memberships.CreateInvitation do
limit = Quota.team_member_limit(site.owner)
usage = Quota.team_member_usage(site.owner)
- if Quota.within_limit?(usage, limit),
+ if Quota.below_limit?(usage, limit),
do: :ok,
else: {:error, {:over_limit, limit}}
end
diff --git a/lib/plausible/sites.ex b/lib/plausible/sites.ex
index 1df4b9b7e..3532e3b9d 100644
--- a/lib/plausible/sites.ex
+++ b/lib/plausible/sites.ex
@@ -87,7 +87,7 @@ defmodule Plausible.Sites do
limit = Quota.site_limit(user)
usage = Quota.site_usage(user)
- if Quota.within_limit?(usage, limit), do: {:ok, usage}, else: {:error, limit}
+ if Quota.below_limit?(usage, limit), do: {:ok, usage}, else: {:error, limit}
end)
|> Ecto.Multi.insert(:site, site_changeset)
|> Ecto.Multi.insert(:site_membership, fn %{site: site} ->
diff --git a/lib/plausible_web/controllers/billing_controller.ex b/lib/plausible_web/controllers/billing_controller.ex
index e8a67de81..1c46395db 100644
--- a/lib/plausible_web/controllers/billing_controller.ex
+++ b/lib/plausible_web/controllers/billing_controller.ex
@@ -124,14 +124,12 @@ defmodule PlausibleWeb.BillingController do
end
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
+ user = conn.assigns.current_user
+ business_tier_enabled? = FunWithFlags.enabled?(:business_tier, for: user)
+
+ with {:ok, {subscription, preview_info}} <- preview_subscription(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
+ if business_tier_enabled?, do: :choose_plan, else: :change_plan_form
render(conn, "change_plan_preview.html",
back_link: Routes.billing_path(conn, back_action),
@@ -142,7 +140,22 @@ defmodule PlausibleWeb.BillingController do
)
else
_ ->
- redirect(conn, to: Routes.billing_path(conn, :upgrade))
+ redirect_to = if business_tier_enabled?, do: :choose_plan, else: :upgrade
+
+ msg =
+ "Something went wrong with loading your plan change information. Please try again, or contact us at support@plausible.io if the issue persists."
+
+ Sentry.capture_message("Error loading change plan preview",
+ extra: %{
+ message: msg,
+ new_plan_id: new_plan_id,
+ user_id: user.id
+ }
+ )
+
+ conn
+ |> put_flash(:error, msg)
+ |> redirect(to: Routes.billing_path(conn, redirect_to))
end
end
diff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.ex
index a41179734..c4a493c01 100644
--- a/lib/plausible_web/controllers/site_controller.ex
+++ b/lib/plausible_web/controllers/site_controller.ex
@@ -13,12 +13,12 @@ defmodule PlausibleWeb.SiteController do
limit = Plausible.Billing.Quota.site_limit(current_user)
usage = Plausible.Billing.Quota.site_usage(current_user)
- within_limit? = Plausible.Billing.Quota.within_limit?(usage, limit)
+ below_limit? = Plausible.Billing.Quota.below_limit?(usage, limit)
render(conn, "new.html",
changeset: Plausible.Site.changeset(%Plausible.Site{}),
is_first_site: usage == 0,
- is_at_limit: !within_limit?,
+ is_at_limit: !below_limit?,
site_limit: limit,
layout: {PlausibleWeb.LayoutView, "focus.html"}
)
diff --git a/lib/plausible_web/live/choose_plan.ex b/lib/plausible_web/live/choose_plan.ex
index 58a87028e..2dfaaacb6 100644
--- a/lib/plausible_web/live/choose_plan.ex
+++ b/lib/plausible_web/live/choose_plan.ex
@@ -285,7 +285,7 @@ defmodule PlausibleWeb.Live.ChoosePlan do
<.contact_button class="bg-indigo-600 hover:bg-indigo-500 text-white" />
<% end %>
- <%= if @kind == :growth && @plan_to_render.generation < 4 do %>
+ <%= if @owned && @kind == :growth && @plan_to_render.generation < 4 do %>
<.growth_grandfathering_notice />
<% else %>
<%= @change_plan_link_text %>
-
+
"""
end
diff --git a/lib/workers/check_usage.ex b/lib/workers/check_usage.ex
index a276202b1..58d987210 100644
--- a/lib/workers/check_usage.ex
+++ b/lib/workers/check_usage.ex
@@ -69,7 +69,7 @@ defmodule Plausible.Workers.CheckUsage do
site_limit = check_site_limit_for_enterprise(subscriber)
case {pageview_limit, site_limit} do
- {{:within_limit, _}, {:within_limit, _}} ->
+ {{:below_limit, _}, {:below_limit, _}} ->
nil
{{_, {last_cycle, last_cycle_usage}}, {_, {site_usage, site_allowance}}} ->
@@ -126,15 +126,15 @@ defmodule Plausible.Workers.CheckUsage do
{last_last_cycle_usage, last_cycle_usage} =
billing_mod.last_two_billing_months_usage(subscriber)
- exceeded_last_cycle? = not Plausible.Billing.Quota.within_limit?(last_cycle_usage, limit)
+ exceeded_last_cycle? = not Plausible.Billing.Quota.below_limit?(last_cycle_usage, limit)
exceeded_last_last_cycle? =
- not Plausible.Billing.Quota.within_limit?(last_last_cycle_usage, limit)
+ not Plausible.Billing.Quota.below_limit?(last_last_cycle_usage, limit)
if exceeded_last_last_cycle? && exceeded_last_cycle? do
{:over_limit, {last_cycle, last_cycle_usage}}
else
- {:within_limit, {last_cycle, last_cycle_usage}}
+ {:below_limit, {last_cycle, last_cycle_usage}}
end
end
@@ -142,8 +142,8 @@ defmodule Plausible.Workers.CheckUsage do
limit = subscriber.enterprise_plan.site_limit
usage = Plausible.Billing.Quota.site_usage(subscriber)
- if Plausible.Billing.Quota.within_limit?(usage, limit) do
- {:within_limit, {usage, limit}}
+ if Plausible.Billing.Quota.below_limit?(usage, limit) do
+ {:below_limit, {usage, limit}}
else
{:over_limit, {usage, limit}}
end
diff --git a/test/plausible/billing/plans_test.exs b/test/plausible/billing/plans_test.exs
index c34bf7d55..f45e12c3b 100644
--- a/test/plausible/billing/plans_test.exs
+++ b/test/plausible/billing/plans_test.exs
@@ -5,6 +5,7 @@ defmodule Plausible.Billing.PlansTest do
@legacy_plan_id "558746"
@v1_plan_id "558018"
@v2_plan_id "654177"
+ @v3_plan_id "749342"
@v4_plan_id "857097"
@v3_business_plan_id "857481"
@v4_business_plan_id "857105"
@@ -25,8 +26,13 @@ defmodule Plausible.Billing.PlansTest do
assert List.first(Plans.growth_plans_for(user)).monthly_product_id == @v2_plan_id
end
- test "growth_plans_for/1 returns v4 plans for everyone else" do
- user = insert(:user)
+ test "growth_plans_for/1 shows v3 pricing for users who signed up before the business tier" do
+ user = insert(:user, inserted_at: ~U[2023-10-01T00:00:00Z])
+ assert List.first(Plans.growth_plans_for(user)).monthly_product_id == @v3_plan_id
+ end
+
+ test "growth_plans_for/1 shows v4 plans for everyone else" do
+ user = insert(:user, inserted_at: ~U[2024-01-01T00:00:00Z])
assert List.first(Plans.growth_plans_for(user)).monthly_product_id == @v4_plan_id
end
@@ -60,8 +66,13 @@ defmodule Plausible.Billing.PlansTest do
assert List.first(business_plans).monthly_product_id == @v3_business_plan_id
end
+ test "business_plans_for/1 returns v3 business plans for users who signed up before the business tier release" do
+ user = insert(:user, inserted_at: ~U[2023-10-01T00:00:00Z])
+ assert List.first(Plans.business_plans_for(user)).monthly_product_id == @v3_business_plan_id
+ end
+
test "business_plans_for/1 returns v4 business plans for everyone else" do
- user = insert(:user)
+ user = insert(:user, inserted_at: ~U[2024-01-01T00:00:00Z])
business_plans = Plans.business_plans_for(user)
assert Enum.all?(business_plans, &(&1.kind == :business))
diff --git a/test/plausible/billing/quota_test.exs b/test/plausible/billing/quota_test.exs
index 157be54b3..9af82292f 100644
--- a/test/plausible/billing/quota_test.exs
+++ b/test/plausible/billing/quota_test.exs
@@ -97,21 +97,21 @@ defmodule Plausible.Billing.QuotaTest do
assert Quota.site_usage(user) == 3
end
- describe "within_limit?/2" do
+ describe "below_limit?/2" do
test "returns true when quota is not exceeded" do
- assert Quota.within_limit?(3, 5)
+ assert Quota.below_limit?(3, 5)
end
test "returns true when limit is :unlimited" do
- assert Quota.within_limit?(10_000, :unlimited)
+ assert Quota.below_limit?(10_000, :unlimited)
end
test "returns false when usage is at limit" do
- refute Quota.within_limit?(3, 3)
+ refute Quota.below_limit?(3, 3)
end
test "returns false when usage exceeds the limit" do
- refute Quota.within_limit?(10, 3)
+ refute Quota.below_limit?(10, 3)
end
end
@@ -127,6 +127,18 @@ defmodule Plausible.Billing.QuotaTest do
assert Quota.exceeded_limits(usage, plan) == [:monthly_pageview_limit, :site_limit]
end
+
+ test "if limits are reached, they're not exceeded" do
+ usage = %{
+ monthly_pageviews: 10_000,
+ team_members: 2,
+ sites: 50
+ }
+
+ plan = Plans.find(@v3_plan_id)
+
+ assert Quota.exceeded_limits(usage, plan) == []
+ end
end
describe "monthly_pageview_limit/1" do
@@ -474,9 +486,9 @@ defmodule Plausible.Billing.QuotaTest do
assert [Goals, StatsAPI, Props] == Quota.allowed_features_for(user_on_v3)
end
- test "returns [Goals] when user is on free_10k plan" do
+ test "returns [Goals, Props, StatsAPI] when user is on free_10k plan" do
user = insert(:user, subscription: build(:subscription, paddle_plan_id: "free_10k"))
- assert [Goals] == Quota.allowed_features_for(user)
+ assert [Goals, Props, StatsAPI] == Quota.allowed_features_for(user)
end
test "returns all features when user is on an enterprise plan" do
diff --git a/test/plausible_web/controllers/billing_controller_test.exs b/test/plausible_web/controllers/billing_controller_test.exs
index 60b3ec58f..88ca8110b 100644
--- a/test/plausible_web/controllers/billing_controller_test.exs
+++ b/test/plausible_web/controllers/billing_controller_test.exs
@@ -5,6 +5,7 @@ defmodule PlausibleWeb.BillingControllerTest do
alias Plausible.Billing.Subscription
@v4_growth_plan "857097"
+ @v4_business_plan "857105"
describe "GET /upgrade" do
setup [:create_user, :log_in]
@@ -218,6 +219,35 @@ defmodule PlausibleWeb.BillingControllerTest do
end
end
+ describe "GET /billing/change-plan/preview/:plan_id" do
+ setup [:create_user, :log_in]
+
+ test "renders preview information about the plan change", %{conn: conn, user: user} do
+ insert(:subscription, user: user, paddle_plan_id: @v4_growth_plan)
+
+ html_response =
+ conn
+ |> get(Routes.billing_path(conn, :change_plan_preview, @v4_business_plan))
+ |> html_response(200)
+
+ assert html_response =~
+ "Your card will be charged a pro-rated amount for the current billing period"
+
+ assert html_response =~ "€-72.6"
+ assert html_response =~ "Back"
+ assert html_response =~ ~s[ render_change(%{slider: 6})
doc = element(lv, @monthly_interval_button) |> render_click()
business_checkout_button = find(doc, @business_checkout_button)
- assert text_of_attr(business_checkout_button, "href") =~
- Routes.billing_path(conn, :change_plan_preview, @v4_business_5m_monthly_plan_id)
+ assert text_of_attr(business_checkout_button, "onclick") =~
+ "if (true) {window.location = '#{Routes.billing_path(conn, :change_plan_preview, @v4_business_5m_monthly_plan_id)}'}"
end
end
@@ -417,6 +448,7 @@ defmodule PlausibleWeb.Live.ChoosePlanTest do
build(:site_membership, user: user, role: :owner),
build(:site_membership, user: build(:user)),
build(:site_membership, user: build(:user)),
+ build(:site_membership, user: build(:user)),
build(:site_membership, user: build(:user))
]
)
@@ -449,7 +481,7 @@ defmodule PlausibleWeb.Live.ChoosePlanTest do
{:ok, _lv, doc} = get_liveview(conn)
assert text_of_attr(find(doc, @growth_checkout_button), "onclick") =~
- "if (!confirm(\"This plan does not support Custom Properties, Revenue Goals and Stats API, which you are currently using. Please note that by subscribing to this plan you will lose access to these features.\")) {e.preventDefault()}"
+ "if (confirm(\"This plan does not support Custom Properties, Revenue Goals and Stats API, which you are currently using. Please note that by subscribing to this plan you will lose access to these features.\")) {window.location = "
end
end
diff --git a/test/support/paddle_api_mock.ex b/test/support/paddle_api_mock.ex
index 64505f960..a2aee7f07 100644
--- a/test/support/paddle_api_mock.ex
+++ b/test/support/paddle_api_mock.ex
@@ -26,6 +26,25 @@ defmodule Plausible.PaddleApi.Mock do
}}
end
+ def update_subscription_preview(_user, _new_plan_id) do
+ {:ok,
+ %{
+ "immediate_payment" => %{
+ "amount" => -72.6,
+ "currency" => "EUR",
+ "date" => "2023-11-05"
+ },
+ "next_payment" => %{
+ "amount" => 47.19,
+ "currency" => "EUR",
+ "date" => "2023-12-05"
+ },
+ "plan_id" => 63_852,
+ "subscription_id" => 600_279,
+ "user_id" => 666_317
+ }}
+ end
+
def get_invoices(nil), do: {:error, :no_invoices}
def get_invoices(%{paddle_subscription_id: nil}), do: {:error, :no_invoices}
diff --git a/test/support/test_utils.ex b/test/support/test_utils.ex
index b0987a860..3bdf74cfa 100644
--- a/test/support/test_utils.ex
+++ b/test/support/test_utils.ex
@@ -35,7 +35,7 @@ defmodule Plausible.TestUtils do
end
def create_user(_) do
- {:ok, user: Factory.insert(:user)}
+ {:ok, user: Factory.insert(:user, inserted_at: ~U[2024-01-01T00:00:00Z])}
end
def create_site(%{user: user}) do