diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex index 0be926cb6..5f5b9e9ef 100644 --- a/lib/plausible/billing/billing.ex +++ b/lib/plausible/billing/billing.ex @@ -244,6 +244,11 @@ defmodule Plausible.Billing do end end + @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 + def paddle_api(), do: Application.fetch_env!(:plausible, :paddle_api) def cancelled_subscription_notice_dismiss_id(%Plausible.Auth.User{} = user) do diff --git a/lib/plausible_web/components/billing.ex b/lib/plausible_web/components/billing/billing.ex similarity index 50% rename from lib/plausible_web/components/billing.ex rename to lib/plausible_web/components/billing/billing.ex index 043932b45..9cc57470c 100644 --- a/lib/plausible_web/components/billing.ex +++ b/lib/plausible_web/components/billing/billing.ex @@ -4,86 +4,8 @@ defmodule PlausibleWeb.Components.Billing do use Phoenix.Component import PlausibleWeb.Components.Generic require Plausible.Billing.Subscription.Status - alias Plausible.Auth.User alias PlausibleWeb.Router.Helpers, as: Routes - alias Plausible.Billing.{Subscription, Plans, Subscriptions} - - 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) - attr(:rest, :global) - - def premium_feature_notice(assigns) do - ~H""" - <.notice - :if={@feature_mod.check_availability(@billable_user) !== :ok} - class="rounded-t-md rounded-b-none" - size={@size} - title="Notice" - {@rest} - > - <%= account_label(@current_user, @billable_user) %> does not have access to <%= @feature_mod.display_name() %>. To get access to this feature, - <.upgrade_call_to_action current_user={@current_user} billable_user={@billable_user} />. - - """ - end - - 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) - - def limit_exceeded_notice(assigns) do - ~H""" - <.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} />. - - """ - end - - attr(:current_user, :map) - attr(:billable_user, :map) - - defp upgrade_call_to_action(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 - - cond do - assigns.billable_user.id !== assigns.current_user.id -> - ~H"please reach out to the site owner to upgrade their subscription" - - growth? || trial? -> - ~H""" - please - <.link - class="underline inline-block" - href={Routes.billing_path(PlausibleWeb.Endpoint, :choose_plan)} - > - upgrade your subscription - - """ - - true -> - ~H"please contact hello@plausible.io to upgrade your subscription" - end - end - - defp account_label(current_user, billable_user) do - if current_user.id == billable_user.id do - "Your account" - else - "The owner of this site" - end - end + alias Plausible.Billing.{Subscription, Subscriptions} def render_monthly_pageview_usage(%{usage: usage} = assigns) when is_map_key(usage, :last_30_days) do @@ -283,142 +205,6 @@ 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 - - def subscription_paused_notice(assigns), do: ~H"" - - def upgrade_ineligible_notice(assigns) do - ~H""" - - """ - end - def present_enterprise_plan(assigns) do ~H"""
- You have access to your stats until <%= Timex.format!(@user.subscription.next_bill_date, "{Mshort} {D}, {YYYY}") %>. - <.link - class="underline inline-block" - href={Routes.billing_path(PlausibleWeb.Endpoint, :choose_plan)} - > - 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}} = assigns) do - plan = Plans.get_regular_plan(subscription, only_non_expired: true) - loses_grandfathering = 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/billing/notice.ex b/lib/plausible_web/components/billing/notice.ex new file mode 100644 index 000000000..d60b1322f --- /dev/null +++ b/lib/plausible_web/components/billing/notice.ex @@ -0,0 +1,262 @@ +defmodule PlausibleWeb.Components.Billing.Notice do + @moduledoc false + + use Phoenix.Component + require Plausible.Billing.Subscription.Status + import PlausibleWeb.Components.Generic + alias PlausibleWeb.Router.Helpers, as: Routes + alias Plausible.Auth.User + alias Plausible.Billing.{Subscription, Plans, Subscriptions, Feature} + + attr(:billable_user, User, required: true) + attr(:current_user, User, required: true) + attr(:feature_mod, :atom, required: true, values: Feature.list()) + attr(:grandfathered?, :boolean, default: false) + attr(:size, :atom, default: :sm) + attr(:rest, :global) + + def premium_feature(assigns) do + ~H""" + <.notice + :if={@feature_mod.check_availability(@billable_user) !== :ok} + class="rounded-t-md rounded-b-none" + size={@size} + title="Notice" + {@rest} + > + <%= account_label(@current_user, @billable_user) %> does not have access to <%= @feature_mod.display_name() %>. To get access to this feature, + <.upgrade_call_to_action current_user={@current_user} billable_user={@billable_user} />. + + """ + end + + 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) + + def limit_exceeded(assigns) do + ~H""" + <.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} />. + + """ + 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(assigns) + + def subscription_cancelled( + %{ + dismissable: true, + user: %User{subscription: %Subscription{status: Subscription.Status.deleted()}} + } = assigns + ) do + ~H""" + + """ + end + + def subscription_cancelled( + %{ + 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(assigns), do: ~H"" + + attr(:class, :string, default: "") + attr(:subscription, :any, default: nil) + + def subscription_past_due( + %{subscription: %Subscription{status: Subscription.Status.past_due()}} = assigns + ) do + ~H""" + + """ + end + + def subscription_past_due(assigns), do: ~H"" + + attr(:class, :string, default: "") + attr(:subscription, :any, default: nil) + + def subscription_paused( + %{subscription: %Subscription{status: Subscription.Status.paused()}} = assigns + ) do + ~H""" + + """ + end + + def subscription_paused(assigns), do: ~H"" + + def upgrade_ineligible(assigns) do + ~H""" + + """ + end + + def growth_grandfathered(assigns) do + ~H""" ++ You have access to your stats until <%= Timex.format!(@user.subscription.next_bill_date, "{Mshort} {D}, {YYYY}") %>. + <.link + class="underline inline-block" + href={Routes.billing_path(PlausibleWeb.Endpoint, :choose_plan)} + > + 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}} = assigns) do + plan = Plans.get_regular_plan(subscription, only_non_expired: true) + loses_grandfathering = 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 + + attr(:current_user, :map) + attr(:billable_user, :map) + + defp upgrade_call_to_action(assigns) do + billable_user = Plausible.Users.with_subscription(assigns.billable_user) + + plan = + Plans.get_regular_plan(billable_user.subscription, only_non_expired: true) + + trial? = Plausible.Billing.on_trial?(assigns.billable_user) + growth? = plan && plan.kind == :growth + + cond do + assigns.billable_user.id !== assigns.current_user.id -> + ~H"please reach out to the site owner to upgrade their subscription" + + growth? || trial? -> + ~H""" + please + <.link + class="underline inline-block" + href={Routes.billing_path(PlausibleWeb.Endpoint, :choose_plan)} + > + upgrade your subscription + + """ + + true -> + ~H"please contact hello@plausible.io to upgrade your subscription" + end + end + + defp account_label(current_user, billable_user) do + if current_user.id == billable_user.id do + "Your account" + else + "The owner of this site" + end + end +end diff --git a/lib/plausible_web/components/billing/pageview_slider.ex b/lib/plausible_web/components/billing/pageview_slider.ex new file mode 100644 index 000000000..f997cc0e8 --- /dev/null +++ b/lib/plausible_web/components/billing/pageview_slider.ex @@ -0,0 +1,160 @@ +defmodule PlausibleWeb.Components.Billing.PageviewSlider do + @moduledoc false + + use Phoenix.Component + use Phoenix.HTML + + def render(assigns) do + ~H""" + <.slider_output volume={@selected_volume} available_volumes={@available_volumes} /> + <.slider_input selected_volume={@selected_volume} available_volumes={@available_volumes} /> + <.slider_styles /> + """ + end + + attr :volume, :any + attr :available_volumes, :list + + defp slider_output(assigns) do + ~H""" + + """ + end + + defp slider_input(assigns) do + slider_labels = + Enum.map( + assigns.available_volumes ++ [:enterprise], + &format_volume(&1, assigns.available_volumes) + ) + + assigns = assign(assigns, :slider_labels, slider_labels) + + ~H""" ++ Sites API access for + <.link + class="text-indigo-500 hover:text-indigo-400" + href="https://plausible.io/white-label-web-analytics" + > + reselling + +
+ """ + end +end diff --git a/lib/plausible_web/components/billing/plan_box.ex b/lib/plausible_web/components/billing/plan_box.ex new file mode 100644 index 000000000..a7f2fbdf5 --- /dev/null +++ b/lib/plausible_web/components/billing/plan_box.ex @@ -0,0 +1,293 @@ +defmodule PlausibleWeb.Components.Billing.PlanBox do + @moduledoc false + + use Phoenix.Component + require Plausible.Billing.Subscription.Status + alias PlausibleWeb.Components.Billing.{PlanBenefits, Notice} + alias Plausible.Billing.{Plan, Quota, Subscription} + alias PlausibleWeb.Router.Helpers, as: Routes + + def standard(assigns) do + highlight = + cond do + assigns.owned -> "Current" + assigns.recommended -> "Recommended" + true -> nil + end + + assigns = assign(assigns, :highlight, highlight) + + ~H""" ++ + Custom + +
+ + <.contact_button class="" /> ++ <%= @text %> +
++ + Custom + +
+ + """ + end + + defp render_price_info(assigns) do + ~H""" ++ <.price_tag + kind={@kind} + selected_interval={@selected_interval} + plan_to_render={@plan_to_render} + /> +
++ VAT if applicable
+ """ + end + + defp price_tag(%{plan_to_render: %Plan{monthly_cost: nil}} = assigns) do + ~H""" + + N/A + + """ + end + + defp price_tag(%{selected_interval: :monthly} = assigns) do + ~H""" + + <%= @plan_to_render.monthly_cost |> Plausible.Billing.format_price() %> + + + /month + + """ + end + + defp price_tag(%{selected_interval: :yearly} = assigns) do + ~H""" + + <%= @plan_to_render.monthly_cost |> Money.mult!(12) |> Plausible.Billing.format_price() %> + + + <%= @plan_to_render.yearly_cost |> Plausible.Billing.format_price() %> + + + /year + + """ + end + + defp checkout(assigns) do + paddle_product_id = get_paddle_product_id(assigns.plan_to_render, assigns.selected_interval) + change_plan_link_text = change_plan_link_text(assigns) + + usage_within_limits = + Quota.ensure_can_subscribe_to_plan(assigns.user, assigns.plan_to_render, assigns.usage) == + :ok + + subscription = assigns.user.subscription + + billing_details_expired = + Subscription.Status.in?(subscription, [ + Subscription.Status.paused(), + Subscription.Status.past_due() + ]) + + subscription_deleted = Subscription.Status.deleted?(subscription) + + {checkout_disabled, disabled_message} = + cond do + assigns.usage.sites == 0 -> + {true, nil} + + change_plan_link_text == "Currently on this plan" && not subscription_deleted -> + {true, nil} + + assigns.available && !usage_within_limits -> + {true, "Your usage exceeds this plan"} + + billing_details_expired -> + {true, "Please update your billing details first"} + + true -> + {false, nil} + end + + features_to_lose = assigns.usage.features -- assigns.plan_to_render.features + + assigns = + assigns + |> assign(:paddle_product_id, paddle_product_id) + |> assign(:change_plan_link_text, change_plan_link_text) + |> assign(:checkout_disabled, checkout_disabled) + |> assign(:disabled_message, disabled_message) + |> assign(:confirm_message, losing_features_message(features_to_lose)) + + ~H""" + <%= if @owned_plan && Plausible.Billing.Subscriptions.resumable?(@user.subscription) do %> + <.change_plan_link {assigns} /> + <% else %> ++ <%= @disabled_message %> +
+ """ + end + + defp get_paddle_product_id(%Plan{monthly_product_id: plan_id}, :monthly), do: plan_id + defp get_paddle_product_id(%Plan{yearly_product_id: plan_id}, :yearly), do: plan_id + + defp change_plan_link_text( + %{ + owned_plan: %Plan{kind: from_kind, monthly_pageview_limit: from_volume}, + plan_to_render: %Plan{kind: to_kind, monthly_pageview_limit: to_volume}, + current_interval: from_interval, + selected_interval: to_interval + } = _assigns + ) do + cond do + from_kind == :business && to_kind == :growth -> + "Downgrade to Growth" + + from_kind == :growth && to_kind == :business -> + "Upgrade to Business" + + from_volume == to_volume && from_interval == to_interval -> + "Currently on this plan" + + from_volume == to_volume -> + "Change billing interval" + + from_volume > to_volume -> + "Downgrade" + + true -> + "Upgrade" + end + end + + defp change_plan_link_text(_), do: nil + + defp change_plan_link(assigns) do + confirmed = + if assigns.confirm_message, do: "confirm(\"#{assigns.confirm_message}\")", else: "true" + + assigns = assign(assigns, :confirmed, confirmed) + + ~H""" + + """ + 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 contact_button(assigns) do + ~H""" + <.link + href="https://plausible.io/contact" + class={[ + "mt-6 block rounded-md py-2 px-3 text-center text-sm font-semibold leading-6 bg-gray-800 hover:bg-gray-700 text-white dark:bg-indigo-600 dark:hover:bg-indigo-500", + @class + ]} + > + Contact us + + """ + end +end diff --git a/lib/plausible_web/live/choose_plan.ex b/lib/plausible_web/live/choose_plan.ex index 6f5c4d0cc..28ed8b900 100644 --- a/lib/plausible_web/live/choose_plan.ex +++ b/lib/plausible_web/live/choose_plan.ex @@ -5,13 +5,11 @@ defmodule PlausibleWeb.Live.ChoosePlan do use Phoenix.LiveView use Phoenix.HTML - import PlausibleWeb.Components.Billing - require Plausible.Billing.Subscription.Status + alias PlausibleWeb.Components.Billing.{PlanBox, PlanBenefits, Notice, PageviewSlider} alias Plausible.Users - alias Plausible.Billing.{Plans, Plan, Quota, Subscription} - alias PlausibleWeb.Router.Helpers, as: Routes + alias Plausible.Billing.{Plans, Plan, Quota} @contact_link "https://plausible.io/contact" @billing_faq_link "https://plausible.io/docs/billing" @@ -82,9 +80,9 @@ defmodule PlausibleWeb.Live.ChoosePlan do business_plan_to_render = assigns.selected_business_plan || List.last(assigns.available_plans.business) - growth_benefits = growth_benefits(growth_plan_to_render) - - business_benefits = business_benefits(business_plan_to_render, growth_benefits) + growth_benefits = PlanBenefits.for_growth(growth_plan_to_render) + business_benefits = PlanBenefits.for_business(business_plan_to_render, growth_benefits) + enterprise_benefits = PlanBenefits.for_enterprise(business_benefits) assigns = assigns @@ -92,14 +90,14 @@ defmodule PlausibleWeb.Live.ChoosePlan do |> assign(:business_plan_to_render, business_plan_to_render) |> assign(:growth_benefits, growth_benefits) |> assign(:business_benefits, business_benefits) - |> assign(:enterprise_benefits, enterprise_benefits(business_benefits)) + |> assign(:enterprise_benefits, enterprise_benefits) ~H"""<%= if @owned_plan, @@ -109,11 +107,13 @@ defmodule PlausibleWeb.Live.ChoosePlan do
You have used <%= PlausibleWeb.AuthView.delimit_integer(@last_30_days_usage) %> @@ -141,8 +141,7 @@ defmodule PlausibleWeb.Live.ChoosePlan do <.help_links />
- <%= @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""" -- - Custom - -
- - """ - end - - def render_price_info(assigns) do - ~H""" -- <.price_tag - kind={@kind} - selected_interval={@selected_interval} - plan_to_render={@plan_to_render} - /> -
-+ VAT if applicable
- """ - end - - defp change_plan_link(assigns) do - confirmed = - if assigns.confirm_message, do: "confirm(\"#{assigns.confirm_message}\")", else: "true" - - assigns = assign(assigns, :confirmed, confirmed) - - ~H""" - - """ - end - - slot :inner_block, required: true - attr :icon_color, :string, default: "indigo-600" - - defp plan_benefit(assigns) do - ~H""" -- - Custom - -
- - <.contact_button class="" /> -- <%= @text %> -
-- Sites API access for - <.link - class="text-indigo-500 hover:text-indigo-400" - href="https://plausible.io/white-label-web-analytics" - > - reselling - -
- """ - end - defp contact_link(), do: @contact_link defp billing_faq_link(), do: @billing_faq_link diff --git a/lib/plausible_web/live/goal_settings/form.ex b/lib/plausible_web/live/goal_settings/form.ex index 4da4681cf..285d1bd2d 100644 --- a/lib/plausible_web/live/goal_settings/form.ex +++ b/lib/plausible_web/live/goal_settings/form.ex @@ -154,7 +154,7 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do }) } > -