diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex index c9c751856..e4d1089d1 100644 --- a/lib/plausible/billing/billing.ex +++ b/lib/plausible/billing/billing.ex @@ -3,6 +3,11 @@ defmodule Plausible.Billing do require Plausible.Billing.Subscription.Status alias Plausible.Billing.{Subscription, Plans, Quota} + def upgrade_route_for(user) do + action = if FunWithFlags.enabled?(:business_tier, for: user), do: :choose_plan, else: :upgrade + PlausibleWeb.Router.Helpers.billing_path(PlausibleWeb.Endpoint, action) + end + @spec active_subscription_for(integer()) :: Subscription.t() | nil def active_subscription_for(user_id) do user_id |> active_subscription_query() |> Repo.one() diff --git a/lib/plausible/billing/feature.ex b/lib/plausible/billing/feature.ex index 211039286..4d55cb17b 100644 --- a/lib/plausible/billing/feature.ex +++ b/lib/plausible/billing/feature.ex @@ -124,6 +124,8 @@ defmodule Plausible.Billing.Feature do {:error, :upgrade_required} -> {:error, :upgrade_required} end end + + defoverridable check_availability: 1 end end end @@ -165,4 +167,27 @@ defmodule Plausible.Billing.Feature.StatsAPI do use Plausible.Billing.Feature, name: :stats_api, display_name: "Stats API" + + @impl true + @doc """ + Checks whether the user has access to Stats API or not. + + Before the the business tier, users who had not yet started their trial had + access to Stats API. With the business tier work, access is blocked and they + must either start their trial or subscribe to a plan. This is common when a + site owner invites a new user. In such cases, using the owner's API key is + recommended. + """ + def check_availability(%Plausible.Auth.User{} = user) do + unlimited_trial? = is_nil(user.trial_expiry_date) + + pre_business_tier_account? = + Timex.before?(user.inserted_at, Plausible.Billing.Plans.business_tier_launch()) + + cond do + unlimited_trial? && pre_business_tier_account? -> :ok + unlimited_trial? && !pre_business_tier_account? -> {:error, :upgrade_required} + true -> super(user) + end + end end diff --git a/lib/plausible/billing/subscriptions.ex b/lib/plausible/billing/subscriptions.ex index 6c0bc9825..13cfc4d61 100644 --- a/lib/plausible/billing/subscriptions.ex +++ b/lib/plausible/billing/subscriptions.ex @@ -30,4 +30,13 @@ defmodule Plausible.Billing.Subscriptions do Subscription.Status.paused() ] end + + def halted?(nil), do: false + + def halted?(%Subscription{status: status}) do + status in [ + Subscription.Status.past_due(), + Subscription.Status.paused() + ] + end end diff --git a/lib/plausible_web/components/billing.ex b/lib/plausible_web/components/billing.ex index ee3a385d4..f980038b4 100644 --- a/lib/plausible_web/components/billing.ex +++ b/lib/plausible_web/components/billing.ex @@ -5,7 +5,7 @@ defmodule PlausibleWeb.Components.Billing do import PlausibleWeb.Components.Generic require Plausible.Billing.Subscription.Status alias PlausibleWeb.Router.Helpers, as: Routes - alias Plausible.{Billing.Subscription, Billing.Plans} + alias Plausible.Billing.{Subscription, Plans, Subscriptions} attr(:billable_user, Plausible.Auth.User, required: true) attr(:current_user, Plausible.Auth.User, required: true) @@ -40,7 +40,7 @@ defmodule PlausibleWeb.Components.Billing do not has_access? && display_upgrade_link? -> ~H""" <%= @feature_mod.display_name() %> is part of the Plausible Business plan. To get access to it, please - <.link class="underline" href={Routes.billing_path(PlausibleWeb.Endpoint, :upgrade)}> + <.link class="underline inline-block" href={Plausible.Billing.upgrade_route_for(@current_user)}> upgrade your subscription to the Business plan. """ @@ -61,6 +61,50 @@ defmodule PlausibleWeb.Components.Billing do """ end + attr(:billable_user, Plausible.Auth.User, required: true) + attr(:current_user, Plausible.Auth.User, required: true) + attr(:limit, :integer, required: true) + attr(:resource, :string, required: true) + attr(:rest, :global) + + # credo:disable-for-next-line Credo.Check.Refactor.CyclomaticComplexity + def limit_exceeded_notice(assigns) do + billable_user = Plausible.Users.with_subscription(assigns.billable_user) + + plan = + Plausible.Billing.Plans.get_regular_plan(billable_user.subscription, only_non_expired: true) + + trial? = Plausible.Billing.on_trial?(assigns.billable_user) + growth? = plan && plan.kind == :growth + + display_upgrade_link? = assigns.current_user.id == assigns.billable_user.id + + message = + cond do + !display_upgrade_link? -> + "This account is limited to #{assigns.limit} #{assigns.resource}. To increase this limit, please reach out to the site owner to upgrade their subscription" + + growth? || trial? -> + ~H""" + Your account is limited to <%= @limit %> <%= @resource %>. To increase this limit, please + <.link class="underline inline-block" href={Plausible.Billing.upgrade_route_for(@current_user)}> + upgrade your subscription + to the Business plan. + """ + + true -> + "Your account is limited to #{assigns.limit} #{assigns.resource}. To increase this limit, please contact support@plausible.io about the Enterprise plan" + end + + assigns = assign(assigns, :message, message) + + ~H""" + <.notice :if={@message} {@rest}> + <%= @message %> + + """ + end + defp private_preview_days_remaining do private_preview_ends_at = Timex.shift(Plausible.Billing.Plans.business_tier_launch(), days: 7) @@ -126,9 +170,11 @@ defmodule PlausibleWeb.Components.Billing do <%= PlausibleWeb.AuthView.subscription_quota(@subscription, format: :long) %> <.styled_link - :if={show_upgrade_or_change_plan_link?(@user, @subscription)} + :if={ + not (Plausible.Auth.enterprise_configured?(@user) && Subscriptions.halted?(@subscription)) + } id="#upgrade-or-change-plan-link" - href={upgrade_link_href(@user)} + href={Routes.billing_path(PlausibleWeb.Endpoint, :choose_plan)} class="text-sm font-medium" > <%= change_plan_or_upgrade_text(@subscription) %> @@ -285,16 +331,9 @@ defmodule PlausibleWeb.Components.Billing do |> PlausibleWeb.StatsView.large_number_format() end - @spec format_price(Money.t(), Keyword.t()) :: String.t() - def format_price(money, opts \\ []) do - opts = - opts - |> Keyword.put_new(:format, :short) - |> Keyword.put_new(:fractional_digits, 2) - - money - |> Money.to_string!(opts) - |> String.replace(".00", "") + @spec format_price(Money.t()) :: String.t() + def format_price(money) do + Money.to_string!(money, fractional_digits: 2, no_fraction_if_integer: true) end attr :id, :string, required: true @@ -340,7 +379,10 @@ defmodule PlausibleWeb.Components.Billing do def upgrade_link(%{business_tier: true} = assigns) do ~H""" - + Upgrade """ @@ -356,29 +398,10 @@ defmodule PlausibleWeb.Components.Billing do """ end - defp upgrade_link_href(user) do - action = - if Plausible.Auth.enterprise_configured?(user), - do: :upgrade_to_enterprise_plan, - else: :choose_plan - - Routes.billing_path(PlausibleWeb.Endpoint, action) - end - defp change_plan_or_upgrade_text(nil), do: "Upgrade" defp change_plan_or_upgrade_text(%Subscription{status: Subscription.Status.deleted()}), do: "Upgrade" defp change_plan_or_upgrade_text(_subscription), do: "Change plan" - - defp show_upgrade_or_change_plan_link?(user, subscription) do - is_enterprise? = Plausible.Auth.enterprise_configured?(user) - - subscription_halted? = - subscription && - subscription.status in [Subscription.Status.past_due(), Subscription.Status.paused()] - - !(is_enterprise? && subscription_halted?) - end end diff --git a/lib/plausible_web/controllers/billing_controller.ex b/lib/plausible_web/controllers/billing_controller.ex index 3015a25a6..cc7bf0b50 100644 --- a/lib/plausible_web/controllers/billing_controller.ex +++ b/lib/plausible_web/controllers/billing_controller.ex @@ -20,6 +20,9 @@ defmodule PlausibleWeb.BillingController do Plausible.Auth.enterprise_configured?(user) -> redirect(conn, to: Routes.billing_path(conn, :upgrade_to_enterprise_plan)) + FunWithFlags.enabled?(:business_tier, for: user) -> + redirect(conn, to: Routes.billing_path(conn, :choose_plan)) + user.subscription && user.subscription.status == Subscription.Status.active() -> redirect(conn, to: Routes.billing_path(conn, :change_plan_form)) @@ -37,14 +40,21 @@ defmodule PlausibleWeb.BillingController do user = conn.assigns.current_user if FunWithFlags.enabled?(:business_tier, for: user) do - render(conn, "choose_plan.html", - skip_plausible_tracking: true, - user: user, - layout: {PlausibleWeb.LayoutView, "focus.html"}, - connect_live_socket: true - ) + if Plausible.Auth.enterprise_configured?(user) do + redirect(conn, to: Routes.billing_path(conn, :upgrade_to_enterprise_plan)) + else + render(conn, "choose_plan.html", + skip_plausible_tracking: true, + user: user, + layout: {PlausibleWeb.LayoutView, "focus.html"}, + connect_live_socket: true + ) + end else - render_error(conn, 404) + # This will be needed in case we need to flip back the flag. + # With the :business_tier flag enabled we'll have sent emails + # linking to `/billing/choose-plan`. + redirect(conn, to: Routes.billing_path(conn, :upgrade)) end end @@ -102,6 +112,9 @@ defmodule PlausibleWeb.BillingController do subscription = Billing.active_subscription_for(user.id) cond do + FunWithFlags.enabled?(:business_tier, for: user) -> + render_error(conn, 404) + Plausible.Auth.enterprise_configured?(user) -> redirect(conn, to: Routes.billing_path(conn, :upgrade_to_enterprise_plan)) @@ -139,8 +152,6 @@ defmodule PlausibleWeb.BillingController do ) else _ -> - 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." @@ -154,7 +165,7 @@ defmodule PlausibleWeb.BillingController do conn |> put_flash(:error, msg) - |> redirect(to: Routes.billing_path(conn, redirect_to)) + |> redirect(to: Plausible.Billing.upgrade_route_for(user)) end end diff --git a/lib/plausible_web/controllers/site/membership_controller.ex b/lib/plausible_web/controllers/site/membership_controller.ex index 2d0001a11..bdf34dba0 100644 --- a/lib/plausible_web/controllers/site/membership_controller.ex +++ b/lib/plausible_web/controllers/site/membership_controller.ex @@ -24,13 +24,22 @@ defmodule PlausibleWeb.Site.MembershipController do [:owner, :admin] when action not in @only_owner_is_allowed_to def invite_member_form(conn, _params) do - site = Sites.get_for_user!(conn.assigns[:current_user].id, conn.assigns[:site].domain) + site = + conn.assigns.current_user.id + |> Sites.get_for_user!(conn.assigns.site.domain) + |> Plausible.Repo.preload(:owner) + + limit = Plausible.Billing.Quota.team_member_limit(site.owner) + usage = Plausible.Billing.Quota.team_member_limit(site.owner) + below_limit? = Plausible.Billing.Quota.below_limit?(usage, limit) render( conn, "invite_member_form.html", site: site, layout: {PlausibleWeb.LayoutView, "focus.html"}, + team_member_limit: limit, + is_at_limit: not below_limit?, skip_plausible_tracking: true ) end diff --git a/lib/plausible_web/live/choose_plan.ex b/lib/plausible_web/live/choose_plan.ex index 2dfaaacb6..08e22197d 100644 --- a/lib/plausible_web/live/choose_plan.ex +++ b/lib/plausible_web/live/choose_plan.ex @@ -708,6 +708,7 @@ defmodule PlausibleWeb.Live.ChoosePlan do "Everything in Business", team_members, "50+ sites", + "600+ Stats API requests per hour", &sites_api_benefit/1, "Technical onboarding" ] @@ -727,6 +728,7 @@ defmodule PlausibleWeb.Live.ChoosePlan do Enum.map(plan.features, fn feature_mod -> case feature_mod.name() do :goals -> "Goals and custom events" + :stats_api -> "Stats API (600 requests per hour)" :revenue_goals -> "Ecommerce revenue attribution" _ -> feature_mod.display_name() end diff --git a/lib/plausible_web/live/goal_settings/form.ex b/lib/plausible_web/live/goal_settings/form.ex index 4229505c5..93e5e7e0a 100644 --- a/lib/plausible_web/live/goal_settings/form.ex +++ b/lib/plausible_web/live/goal_settings/form.ex @@ -152,6 +152,13 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do }) } > + - - - -
<.live_component id="currency_input" diff --git a/lib/plausible_web/templates/billing/upgrade_to_enterprise_plan.html.heex b/lib/plausible_web/templates/billing/upgrade_to_enterprise_plan.html.heex index 4388a9127..9649328ec 100644 --- a/lib/plausible_web/templates/billing/upgrade_to_enterprise_plan.html.heex +++ b/lib/plausible_web/templates/billing/upgrade_to_enterprise_plan.html.heex @@ -22,11 +22,8 @@ The plan is priced at <%= case @price do - %Money{} = money -> - PlausibleWeb.Components.Billing.format_price(money, format: :standard) - - nil -> - "N/A" + %Money{} = money -> PlausibleWeb.Components.Billing.format_price(money) + nil -> "N/A" end %> diff --git a/lib/plausible_web/templates/email/trial_one_week_reminder.html.eex b/lib/plausible_web/templates/email/trial_one_week_reminder.html.eex index 1b16bcfaf..cd1112910 100644 --- a/lib/plausible_web/templates/email/trial_one_week_reminder.html.eex +++ b/lib/plausible_web/templates/email/trial_one_week_reminder.html.eex @@ -2,6 +2,6 @@ Time flies! Your 30-day free trial of Plausible will end next week.

Over the last three weeks, We hope you got to experience the potential benefits of having website stats in an easy to use dashboard while respecting the privacy of your visitors, not annoying them with the cookie and privacy notices and still having a fast loading site.

-In order to continue receiving valuable website traffic insights at a glance, you’ll need to <%= link("upgrade your account", to: "#{plausible_url()}/billing/upgrade") %>. +In order to continue receiving valuable website traffic insights at a glance, you’ll need to <%= link("upgrade your account", to: plausible_url() <> Plausible.Billing.upgrade_route_for(@user)) %>.

If you have any questions or feedback for us, feel free to reply to this email. diff --git a/lib/plausible_web/templates/email/trial_over_email.html.eex b/lib/plausible_web/templates/email/trial_over_email.html.eex index 7c8981b8f..41766f8bf 100644 --- a/lib/plausible_web/templates/email/trial_over_email.html.eex +++ b/lib/plausible_web/templates/email/trial_over_email.html.eex @@ -1,6 +1,6 @@ Your free Plausible trial has now expired. Upgrade your account to continue receiving valuable website traffic insights at a glance while respecting the privacy of your visitors and still having a fast loading site.

-<%= link("Upgrade now", to: "#{plausible_url()}/billing/upgrade") %> +<%= link("Upgrade now", to: plausible_url() <> Plausible.Billing.upgrade_route_for(@user)) %>

We will keep recording stats for another month to give you time to upgrade. diff --git a/lib/plausible_web/templates/email/trial_upgrade_email.html.eex b/lib/plausible_web/templates/email/trial_upgrade_email.html.eex index 3fc23add7..7d92bd872 100644 --- a/lib/plausible_web/templates/email/trial_upgrade_email.html.eex +++ b/lib/plausible_web/templates/email/trial_upgrade_email.html.eex @@ -6,7 +6,7 @@ This is more than our standard plans, so please reply back to this email to get <% else %> Based on that we recommend you select a <%= @suggested_plan.volume %>/mo plan.

-<%= link("Upgrade now", to: "#{plausible_url()}/billing/upgrade") %> +<%= link("Upgrade now", to: plausible_url() <> Plausible.Billing.upgrade_route_for(@user)) %>

Have a question, feedback or need some guidance? Just reply to this email to get in touch! <% end %> diff --git a/lib/plausible_web/templates/email/yearly_expiration_notification.html.eex b/lib/plausible_web/templates/email/yearly_expiration_notification.html.eex index 6a4009c10..2a85813f2 100644 --- a/lib/plausible_web/templates/email/yearly_expiration_notification.html.eex +++ b/lib/plausible_web/templates/email/yearly_expiration_notification.html.eex @@ -1,6 +1,6 @@ Time flies! This is a reminder that your annual subscription for Plausible Analytics will expire on <%= @date %>.

-You need to renew your subscription on <%= link("account settings page", to: "#{plausible_url()}/billing/upgrade") %> if you want to continue using Plausible to count your website stats in a privacy-friendly way. +You need to <%= link("renew your subscription", to: plausible_url() <> Plausible.Billing.upgrade_route_for(@user)) %> if you want to continue using Plausible to count your website stats in a privacy-friendly way.

If you don't want to continue your subscription, there's no action required. You will lose access to the stats on <%= @date %>.

diff --git a/lib/plausible_web/templates/site/membership/invite_member_form.html.eex b/lib/plausible_web/templates/site/membership/invite_member_form.html.eex deleted file mode 100644 index 81467a65c..000000000 --- a/lib/plausible_web/templates/site/membership/invite_member_form.html.eex +++ /dev/null @@ -1,63 +0,0 @@ -<%= form_for @conn, Routes.membership_path(@conn, :invite_member, @site.domain), [class: "max-w-lg w-full mx-auto bg-white dark:bg-gray-800 shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-8"], fn f -> %> -

Invite member to <%= @site.domain %>

- -

- Enter the email address and role of the person you want to invite. We will contact them over email to offer them access to - <%= @site.domain %> analytics. -

- -

- The invitation will expire in 48 hours -

- - <%= if @conn.assigns[:error] do %> -
<%= @conn.assigns[:error] %>
- <% end %> - -
- <%= label f, :email, "Email address", class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %> -
-
- -
- <%= email_input(f, :email, class: "focus:ring-indigo-500 focus:border-indigo-500 dark:bg-gray-900 dark:text-gray-300 block w-full rounded-md pl-10 sm:text-sm border-gray-300 dark:border-gray-500", placeholder: "john.doe@example.com", required: "true") %> -
- <%= error_tag f, :email %> -
- -
- <%= label f, :role, class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %> -
- - - -
-
- -
- <%= submit(class: "button w-full") do %> - - Invite - <% end %> -
-<% end %> diff --git a/lib/plausible_web/templates/site/membership/invite_member_form.html.heex b/lib/plausible_web/templates/site/membership/invite_member_form.html.heex new file mode 100644 index 000000000..96bb1568e --- /dev/null +++ b/lib/plausible_web/templates/site/membership/invite_member_form.html.heex @@ -0,0 +1,121 @@ +<%= form_for @conn, Routes.membership_path(@conn, :invite_member, @site.domain), [class: "max-w-lg w-full mx-auto bg-white dark:bg-gray-800 shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-8"], fn f -> %> +

Invite member to <%= @site.domain %>

+ + + +

+ Enter the email address and role of the person you want to invite. We will contact them over email to offer them access to <%= @site.domain %> analytics. +

+ +

+ The invitation will expire in 48 hours +

+ + <%= if @conn.assigns[:error] do %> +
<%= @conn.assigns[:error] %>
+ <% end %> + +
+ <%= label(f, :email, "Email address", + class: "block text-sm font-medium text-gray-700 dark:text-gray-300" + ) %> +
+
+ +
+ <%= email_input(f, :email, + class: + "focus:ring-indigo-500 focus:border-indigo-500 dark:bg-gray-900 dark:text-gray-300 block w-full rounded-md pl-10 sm:text-sm border-gray-300 dark:border-gray-500", + placeholder: "john.doe@example.com", + required: "true" + ) %> +
+ <%= error_tag(f, :email) %> +
+ +
+ <%= label(f, :role, class: "block text-sm font-medium text-gray-700 dark:text-gray-300") %> +
+ + + +
+
+ +
+ <%= submit(class: "button w-full disabled:cursor-not-allowed", disabled: Map.get(assigns, :is_at_limit, false)) do %> + + + + + Invite + <% end %> +
+<% end %> diff --git a/lib/plausible_web/templates/site/new.html.eex b/lib/plausible_web/templates/site/new.html.eex deleted file mode 100644 index 14cf241f9..000000000 --- a/lib/plausible_web/templates/site/new.html.eex +++ /dev/null @@ -1,89 +0,0 @@ -
- <%= form_for @changeset, "/sites", [class: "max-w-lg w-full mx-auto bg-white dark:bg-gray-800 shadow-lg rounded px-8 pt-6 pb-8 mb-4 mt-8"], fn f -> %> -

Your website details

- - <%= if @is_at_limit do %> -
-
-
- -
-
-

- Upgrade required -

-
-

- Your account is limited to <%= @site_limit %> sites. Please contact support@plausible.io to add more sites. -

-
-
-
-
- <% end %> - - <%= if is_nil(@current_user.trial_expiry_date) do %> -
-
-
- -
-
-
-

- When you create your first site, your account will enter a 30 day free trial. -

-
-
-
-
- <% end %> - -
- <%= label f, :domain, class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %> -

Just the naked domain or subdomain without 'www'

-
- - https:// - - <%= text_input f, :domain, class: "focus:ring-indigo-500 focus:border-indigo-500 dark:bg-gray-800 flex-1 block w-full px-3 py-2 rounded-none rounded-r-md sm:text-sm border-gray-300 dark:border-gray-500 dark:bg-gray-900 dark:text-gray-300", placeholder: "example.com", disabled: @is_at_limit %> -
- <%= error_tag f, :domain %> -
-
- <%= label f, :timezone, "Reporting Timezone", class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %> -

To make sure we agree on what 'today' means

- -
- <%= select f, :timezone, Plausible.Timezones.options(), id: "tz-select", selected: "Etc/Greenwich", class: "mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 dark:border-gray-500 dark:bg-gray-900 dark:text-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md", disabled: @is_at_limit %> -
-
- - - <%= submit "Add snippet →", class: "button mt-4 w-full", disabled: @is_at_limit %> - <% end %> - - <%= if @is_first_site do %> - - <% end %> -
diff --git a/lib/plausible_web/templates/site/new.html.heex b/lib/plausible_web/templates/site/new.html.heex new file mode 100644 index 000000000..a24059ff0 --- /dev/null +++ b/lib/plausible_web/templates/site/new.html.heex @@ -0,0 +1,104 @@ +
+ <%= form_for @changeset, "/sites", [class: "max-w-lg w-full mx-auto bg-white dark:bg-gray-800 shadow-lg rounded px-8 pt-6 pb-8 mb-4 mt-8"], fn f -> %> +

Your website details

+ + + + <%= if is_nil(@current_user.trial_expiry_date) do %> +
+
+
+ +
+
+
+

+ When you create your first site, your account will enter a 30 day free trial. +

+
+
+
+
+ <% end %> + +
+ <%= label(f, :domain, class: "block text-sm font-medium text-gray-700 dark:text-gray-300") %> +

+ Just the naked domain or subdomain without 'www' +

+
+ + https:// + + <%= text_input(f, :domain, + class: + "focus:ring-indigo-500 focus:border-indigo-500 dark:bg-gray-800 flex-1 block w-full px-3 py-2 rounded-none rounded-r-md sm:text-sm border-gray-300 dark:border-gray-500 dark:bg-gray-900 dark:text-gray-300", + placeholder: "example.com", + disabled: Map.get(assigns, :is_at_limit, false) + ) %> +
+ <%= error_tag(f, :domain) %> +
+
+ <%= label(f, :timezone, "Reporting Timezone", + class: "block text-sm font-medium text-gray-700 dark:text-gray-300" + ) %> +

+ To make sure we agree on what 'today' means +

+ +
+ <%= select(f, :timezone, Plausible.Timezones.options(), + id: "tz-select", + selected: "Etc/Greenwich", + class: + "mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 dark:border-gray-500 dark:bg-gray-900 dark:text-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md", + disabled: Map.get(assigns, :is_at_limit, false) + ) %> +
+
+ + + <%= submit("Add snippet →", + class: "button mt-4 w-full disabled:cursor-not-allowed", + disabled: Map.get(assigns, :is_at_limit, false) + ) %> + <% end %> + + <%= if @is_first_site do %> + + <% end %> +
diff --git a/test/plausible/billing/feature_test.exs b/test/plausible/billing/feature_test.exs index 2167867d1..8466e0fa3 100644 --- a/test/plausible/billing/feature_test.exs +++ b/test/plausible/billing/feature_test.exs @@ -63,6 +63,18 @@ defmodule Plausible.Billing.FeatureTest do Plausible.Billing.Feature.StatsAPI.check_availability(user) end + test "Plausible.Billing.Feature.StatsAPI.check_availability/2 returns :ok when user trial hasn't started and was created before the business tier launch" do + user = insert(:user, inserted_at: ~N[2020-01-01T00:00:00], trial_expiry_date: nil) + assert :ok == Plausible.Billing.Feature.StatsAPI.check_availability(user) + end + + test "Plausible.Billing.Feature.StatsAPI.check_availability/2 returns error when user trial hasn't started and was created after the business tier launch" do + user = insert(:user, inserted_at: ~N[2024-01-01T00:00:00], trial_expiry_date: nil) + + assert {:error, :upgrade_required} == + Plausible.Billing.Feature.StatsAPI.check_availability(user) + end + test "Plausible.Billing.Feature.Props.check_availability/1 applies grandfathering to old plans" do user = insert(:user, subscription: build(:subscription, paddle_plan_id: @v1_plan_id)) assert :ok == Plausible.Billing.Feature.Props.check_availability(user) diff --git a/test/plausible_web/components/billing_test.exs b/test/plausible_web/components/billing_test.exs index d8bc28bc1..aff369bce 100644 --- a/test/plausible_web/components/billing_test.exs +++ b/test/plausible_web/components/billing_test.exs @@ -26,7 +26,7 @@ defmodule PlausibleWeb.Components.BillingTest do assert rendered =~ "Custom Properties is part of the Plausible Business plan." assert rendered =~ "upgrade your subscription" - assert rendered =~ "/billing/upgrade" + assert rendered =~ "/billing/choose-plan" end test "premium_feature_notice/1 does not render an upgrade link when user is not the site owner" do @@ -43,7 +43,7 @@ defmodule PlausibleWeb.Components.BillingTest do assert rendered =~ "Funnels is part of the Plausible Business plan. To get access to it, please reach out to the site owner to upgrade your subscription to the Business plan." - refute rendered =~ "/billing/upgrade" + refute rendered =~ "/billing/choose-plan" end test "premium_feature_notice/1 does not render a notice when the user has access to the feature" do @@ -58,4 +58,85 @@ defmodule PlausibleWeb.Components.BillingTest do assert rendered == "" end + + test "limit_exceeded_notice/1 when billable user is on growth displays upgrade link" do + me = insert(:user, subscription: build(:growth_subscription)) + + rendered = + render_component(&Billing.limit_exceeded_notice/1, + billable_user: me, + current_user: me, + limit: 10, + resource: "users" + ) + + assert rendered =~ "Your account is limited to 10 users. To increase this limit" + assert rendered =~ "upgrade your subscription" + assert rendered =~ "/billing/choose-plan" + end + + test "limit_exceeded_notice/1 when billable user is on growth but is not current user does not display upgrade link" do + me = insert(:user, subscription: build(:growth_subscription)) + + rendered = + render_component(&Billing.limit_exceeded_notice/1, + billable_user: me, + current_user: insert(:user), + limit: 10, + resource: "users" + ) + + assert rendered =~ + "This account is limited to 10 users. To increase this limit, please reach out to the site owner to upgrade their subscription" + end + + test "limit_exceeded_notice/1 when billable user is on trial displays upgrade link" do + me = insert(:user) + + rendered = + render_component(&Billing.limit_exceeded_notice/1, + billable_user: me, + current_user: me, + limit: 10, + resource: "users" + ) + + assert rendered =~ "Your account is limited to 10 users. To increase this limit" + assert rendered =~ "upgrade your subscription" + assert rendered =~ "/billing/choose-plan" + end + + test "limit_exceeded_notice/1 when billable user is on an enterprise plan displays support email" do + me = + insert(:user, + enterprise_plan: build(:enterprise_plan, paddle_plan_id: "123321"), + subscription: build(:subscription, paddle_plan_id: "123321") + ) + + rendered = + render_component(&Billing.limit_exceeded_notice/1, + billable_user: me, + current_user: me, + limit: 10, + resource: "users" + ) + + assert rendered =~ + "Your account is limited to 10 users. To increase this limit, please contact support@plausible.io about the Enterprise plan" + end + + test "limit_exceeded_notice/1 when billable user is on a business plan displays support email" do + me = insert(:user, subscription: build(:business_subscription)) + + rendered = + render_component(&Billing.limit_exceeded_notice/1, + billable_user: me, + current_user: me, + limit: 10, + resource: "users" + ) + + assert rendered =~ + "Your account is limited to 10 users. To increase this limit, please contact support@plausible.io about the Enterprise plan" + end end diff --git a/test/plausible_web/controllers/auth_controller_test.exs b/test/plausible_web/controllers/auth_controller_test.exs index 9bac44cf5..a2622e953 100644 --- a/test/plausible_web/controllers/auth_controller_test.exs +++ b/test/plausible_web/controllers/auth_controller_test.exs @@ -510,7 +510,7 @@ defmodule PlausibleWeb.AuthControllerTest do assert html_response(conn, 200) =~ "yearly billing" end - test "links to upgrade to a plan", %{conn: conn} do + test "renders two links to '/billing/choose-plan` with the text 'Upgrade'", %{conn: conn} do doc = get(conn, "/settings") |> html_response(200) @@ -525,7 +525,7 @@ defmodule PlausibleWeb.AuthControllerTest do assert text_of_attr(upgrade_link_2, "href") == Routes.billing_path(conn, :choose_plan) end - test "links to change existing plan", %{ + test "renders a link to '/billing/choose-plan' with the text 'Change plan' + cancel link", %{ conn: conn, user: user } do @@ -544,7 +544,7 @@ defmodule PlausibleWeb.AuthControllerTest do assert text_of_attr(change_plan_link, "href") == Routes.billing_path(conn, :choose_plan) end - test "upgrade_to_enterprise_plan link does not show up when subscription is past_due", %{ + test "/billing/choose-plan link does not show up when enterprise subscription is past_due", %{ conn: conn, user: user } do @@ -564,7 +564,7 @@ defmodule PlausibleWeb.AuthControllerTest do refute element_exists?(doc, "#upgrade-or-change-plan-link") end - test "upgrade_to_enterprise_plan link does not show up when subscription is paused", %{ + test "/billing/choose-plan link does not show up when enterprise subscription is paused", %{ conn: conn, user: user } do @@ -584,7 +584,7 @@ defmodule PlausibleWeb.AuthControllerTest do refute element_exists?(doc, "#upgrade-or-change-plan-link") end - test "links to upgrade to enterprise plan", + test "renders two links to '/billing/choose-plan' with the text 'Upgrade' for a configured enterprise plan", %{conn: conn, user: user} do configure_enterprise_plan(user) @@ -598,15 +598,15 @@ defmodule PlausibleWeb.AuthControllerTest do assert text(upgrade_link_1) == "Upgrade" assert text_of_attr(upgrade_link_1, "href") == - Routes.billing_path(conn, :upgrade_to_enterprise_plan) + Routes.billing_path(conn, :choose_plan) assert text(upgrade_link_2) == "Upgrade" assert text_of_attr(upgrade_link_2, "href") == - Routes.billing_path(conn, :upgrade_to_enterprise_plan) + Routes.billing_path(conn, :choose_plan) end - test "links to change enterprise plan and cancel subscription", + test "links to '/billing/choose-plan' with the text 'Change plan' for a configured enterprise plan with an existing subscription + renders cancel button", %{conn: conn, user: user} do insert(:subscription, paddle_plan_id: @v3_plan_id, user: user) @@ -624,7 +624,7 @@ defmodule PlausibleWeb.AuthControllerTest do assert text(change_plan_link) == "Change plan" assert text_of_attr(change_plan_link, "href") == - Routes.billing_path(conn, :upgrade_to_enterprise_plan) + Routes.billing_path(conn, :choose_plan) end test "shows invoices for subscribed user", %{conn: conn, user: user} do diff --git a/test/plausible_web/controllers/billing_controller_test.exs b/test/plausible_web/controllers/billing_controller_test.exs index 88ca8110b..41171be2d 100644 --- a/test/plausible_web/controllers/billing_controller_test.exs +++ b/test/plausible_web/controllers/billing_controller_test.exs @@ -10,19 +10,6 @@ defmodule PlausibleWeb.BillingControllerTest do describe "GET /upgrade" do setup [:create_user, :log_in] - test "shows upgrade page when user does not have a subcription already", %{conn: conn} do - conn = get(conn, Routes.billing_path(conn, :upgrade)) - - assert html_response(conn, 200) =~ "Upgrade your free trial" - end - - test "redirects user to change plan if they already have a plan", %{conn: conn, user: user} do - insert(:subscription, user: user) - conn = get(conn, Routes.billing_path(conn, :upgrade)) - - assert redirected_to(conn) == Routes.billing_path(conn, :change_plan_form) - end - test "redirects user to enteprise plan page if they are configured with one", %{ conn: conn, user: user @@ -33,35 +20,22 @@ defmodule PlausibleWeb.BillingControllerTest do end end - describe "GET /upgrade/enterprise/:plan_id (deprecated)" do + describe "GET /choose-plan" do setup [:create_user, :log_in] - test "redirects to the new :upgrade_to_enterprise_plan action", %{conn: conn} do - conn = get(conn, Routes.billing_path(conn, :upgrade_enterprise_plan, "123")) + test "redirects to enterprise upgrade page if user has an enterprise plan configured", + %{conn: conn, user: user} do + insert(:enterprise_plan, user: user, paddle_plan_id: "123") + conn = get(conn, Routes.billing_path(conn, :choose_plan)) assert redirected_to(conn) == Routes.billing_path(conn, :upgrade_to_enterprise_plan) end end - describe "GET /change-plan" do + describe "GET /upgrade/enterprise/:plan_id (deprecated)" do setup [:create_user, :log_in] - test "shows change plan page if user has subsription", %{conn: conn, user: user} do - insert(:subscription, user: user) - conn = get(conn, Routes.billing_path(conn, :change_plan_form)) - - assert html_response(conn, 200) =~ "Change subscription plan" - end - - test "redirects to /upgrade if user does not have a subscription", %{conn: conn} do - conn = get(conn, Routes.billing_path(conn, :change_plan_form)) - - assert redirected_to(conn) == Routes.billing_path(conn, :upgrade) - end - - test "redirects to enterprise upgrade page if user has an enterprise plan configured", - %{conn: conn, user: user} do - insert(:enterprise_plan, user: user, paddle_plan_id: "123") - conn = get(conn, Routes.billing_path(conn, :change_plan_form)) + test "redirects to the new :upgrade_to_enterprise_plan action", %{conn: conn} do + conn = get(conn, Routes.billing_path(conn, :upgrade_enterprise_plan, "123")) assert redirected_to(conn) == Routes.billing_path(conn, :upgrade_to_enterprise_plan) end end diff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exs index ada9c0fdd..09dbf0425 100644 --- a/test/plausible_web/controllers/site_controller_test.exs +++ b/test/plausible_web/controllers/site_controller_test.exs @@ -271,9 +271,7 @@ defmodule PlausibleWeb.SiteControllerTest do }) assert html = html_response(conn, 200) - assert html =~ "Upgrade required" assert html =~ "Your account is limited to 10 sites" - assert html =~ "Please contact support" refute Repo.get_by(Plausible.Site, domain: "over-limit.example.com") end diff --git a/test/plausible_web/live/choose_plan_test.exs b/test/plausible_web/live/choose_plan_test.exs index a95315ab4..5583aa5d7 100644 --- a/test/plausible_web/live/choose_plan_test.exs +++ b/test/plausible_web/live/choose_plan_test.exs @@ -49,7 +49,7 @@ defmodule PlausibleWeb.Live.ChoosePlanTest do assert growth_box =~ "Email/Slack reports" assert growth_box =~ "Google Analytics import" assert growth_box =~ "Goals and custom events" - assert growth_box =~ "Stats API" + assert growth_box =~ "Stats API (600 requests per hour)" assert growth_box =~ "Custom Properties" assert business_box =~ "Everything in Growth" @@ -94,7 +94,7 @@ defmodule PlausibleWeb.Live.ChoosePlanTest do assert business_box =~ "Everything in Growth" assert business_box =~ "Up to 10 team members" assert business_box =~ "Up to 50 sites" - assert business_box =~ "Stats API" + assert business_box =~ "Stats API (600 requests per hour)" assert business_box =~ "Custom Properties" assert business_box =~ "Funnels" assert business_box =~ "Ecommerce revenue attribution" @@ -105,6 +105,7 @@ defmodule PlausibleWeb.Live.ChoosePlanTest do assert enterprise_box =~ "Everything in Business" assert enterprise_box =~ "10+ team members" assert enterprise_box =~ "50+ sites" + assert enterprise_box =~ "600+ Stats API requests per hour" assert enterprise_box =~ "Sites API access for" assert enterprise_box =~ "Technical onboarding" @@ -293,7 +294,7 @@ defmodule PlausibleWeb.Live.ChoosePlanTest do assert business_box =~ "Everything in Growth" assert business_box =~ "Up to 10 team members" assert business_box =~ "Up to 50 sites" - assert business_box =~ "Stats API" + assert business_box =~ "Stats API (600 requests per hour)" assert business_box =~ "Custom Properties" assert business_box =~ "Funnels" assert business_box =~ "Ecommerce revenue attribution" @@ -304,6 +305,7 @@ defmodule PlausibleWeb.Live.ChoosePlanTest do assert enterprise_box =~ "Everything in Business" assert enterprise_box =~ "10+ team members" assert enterprise_box =~ "50+ sites" + assert enterprise_box =~ "600+ Stats API requests per hour" assert enterprise_box =~ "Sites API access for" assert enterprise_box =~ "Technical onboarding" @@ -510,7 +512,7 @@ defmodule PlausibleWeb.Live.ChoosePlanTest do assert business_box =~ "Everything in Growth" assert business_box =~ "Unlimited team members" assert business_box =~ "Up to 50 sites" - assert business_box =~ "Stats API" + assert business_box =~ "Stats API (600 requests per hour)" assert business_box =~ "Custom Properties" assert business_box =~ "Funnels" assert business_box =~ "Ecommerce revenue attribution" @@ -520,6 +522,7 @@ defmodule PlausibleWeb.Live.ChoosePlanTest do assert enterprise_box =~ "Everything in Business" assert enterprise_box =~ "50+ sites" + assert enterprise_box =~ "600+ Stats API requests per hour" assert enterprise_box =~ "Sites API access for" assert enterprise_box =~ "Technical onboarding" @@ -638,7 +641,7 @@ defmodule PlausibleWeb.Live.ChoosePlanTest do doc = lv |> element(@slider_input) |> render_change(%{slider: 9}) assert text_of_element(doc, @slider_value) == "50M" assert text_of_element(doc, @business_plan_box) =~ "Contact us" - assert text_of_element(doc, @growth_price_tag_amount) == "€1K" + assert text_of_element(doc, @growth_price_tag_amount) == "€1,000" assert text_of_element(doc, @growth_price_tag_interval) == "/year" doc = lv |> element(@slider_input) |> render_change(%{slider: 10}) @@ -673,11 +676,12 @@ defmodule PlausibleWeb.Live.ChoosePlanTest do refute business_box =~ "Goals and custom events" refute business_box =~ "Unlimited team members" refute business_box =~ "Up to 50 sites" - refute business_box =~ "Stats API" + refute business_box =~ "Stats API (600 requests per hour)" refute business_box =~ "Custom Properties" assert enterprise_box =~ "Everything in Business" assert enterprise_box =~ "50+ sites" + assert enterprise_box =~ "600+ Stats API requests per hour" assert enterprise_box =~ "Sites API access for" assert enterprise_box =~ "Technical onboarding"