defmodule PlausibleWeb.Components.Billing do @moduledoc false use Phoenix.Component import PlausibleWeb.Components.Generic require Plausible.Billing.Subscription.Status alias Plausible.Billing.Feature.{Props, StatsAPI} alias PlausibleWeb.Router.Helpers, as: Routes alias Plausible.Billing.{Subscription, Plans, Subscriptions} attr(:billable_user, Plausible.Auth.User, required: true) attr(:current_user, Plausible.Auth.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) # credo:disable-for-next-line Credo.Check.Refactor.CyclomaticComplexity def premium_feature_notice(assigns) do billable_user = Plausible.Users.with_subscription(assigns.billable_user) plan = Plans.get_regular_plan(billable_user.subscription, only_non_expired: true) business? = plan && plan.kind == :business legacy_feature_access? = Timex.before?(assigns.billable_user.inserted_at, Plans.business_tier_launch()) && assigns.feature_mod in [StatsAPI, Props] private_preview? = FunWithFlags.enabled?(:premium_features_private_preview) has_access? = assigns.feature_mod.check_availability(assigns.billable_user) == :ok cond do legacy_feature_access? -> ~H"" Plausible.Billing.on_trial?(assigns.billable_user) -> ~H""" <.notice class="rounded-t-md rounded-b-none" size={@size} {@rest}> <%= @feature_mod.display_name() %> is part of the Plausible Business plan. You can access it during your trial, but you'll need to subscribe to the Business plan to retain access after the trial ends." """ private_preview? && !business? -> ~H""" <.notice class="rounded-t-md rounded-b-none" size={@size} {@rest}> Business plans are now live! The private preview of <%= @feature_mod.display_name() %> ends <%= private_preview_days_remaining() %>. If you wish to continue using this feature, <.upgrade_call_to_action current_user={@current_user} billable_user={@billable_user} />. """ not has_access? -> ~H""" <.notice class="rounded-t-md rounded-b-none" size={@size} {@rest}> <%= 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} />. """ true -> ~H"" end end defp private_preview_days_remaining do private_preview_ends_at = Timex.shift(Plausible.Billing.Plans.business_tier_launch(), days: 8) days_remaining = Timex.diff(private_preview_ends_at, NaiveDateTime.utc_now(), :day) cond do days_remaining <= 0 -> "today" days_remaining == 1 -> "tomorrow" true -> "in #{days_remaining} days" end 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 ~H""" <.notice {@rest}> <%= 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={Plausible.Billing.upgrade_route_for(@current_user)}> upgrade your subscription """ true -> ~H"please contact support@plausible.io about the Enterprise plan" 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 slot(:inner_block, required: true) attr(:rest, :global) def usage_and_limits_table(assigns) do ~H""" <%= render_slot(@inner_block) %>
""" end attr(:title, :string, required: true) attr(:usage, :any, required: true) attr(:limit, :integer, default: nil) attr(:pad, :boolean, default: false) attr(:rest, :global) def usage_and_limits_row(assigns) do ~H""" <%= @title %> <%= render_quota(@usage) %> <%= if @limit, do: "/ #{render_quota(@limit)}" %> """ end defp render_quota(quota) do case quota do quota when is_number(quota) -> Cldr.Number.to_string!(quota) :unlimited -> "∞" nil -> "" end end def monthly_quota_box(%{business_tier: true} = assigns) do ~H"""

Monthly quota

<%= PlausibleWeb.AuthView.subscription_quota(@subscription, format: :long) %>
<.styled_link :if={ not (Plausible.Auth.enterprise_configured?(@user) && Subscriptions.halted?(@subscription)) } id="#upgrade-or-change-plan-link" href={Routes.billing_path(PlausibleWeb.Endpoint, :choose_plan)} class="text-sm font-medium" > <%= change_plan_or_upgrade_text(@subscription) %>
""" end def monthly_quota_box(%{business_tier: false} = assigns) do ~H"""

Monthly quota

<%= if @subscription do %>
<%= PlausibleWeb.AuthView.subscription_quota(@subscription) %> pageviews
<.styled_link :if={@subscription.status == Subscription.Status.active()} href={Routes.billing_path(PlausibleWeb.Endpoint, :change_plan_form)} class="text-sm font-medium" > Change plan Change plan <% else %>
Free trial
<.styled_link href={Routes.billing_path(PlausibleWeb.Endpoint, :upgrade)} class="text-sm font-medium" > Upgrade <% end %>
""" end def subscription_past_due_notice( %{subscription: %Subscription{status: Subscription.Status.past_due()}} = assigns ) do ~H""" """ end def subscription_past_due_notice(assigns), do: ~H"" def subscription_paused_notice( %{subscription: %Subscription{status: Subscription.Status.paused()}} = assigns ) do ~H""" """ end def subscription_paused_notice(assigns), do: ~H"" def present_enterprise_plan(assigns) do ~H""" """ end defp present_limit(enterprise_plan, key) do enterprise_plan |> Map.fetch!(key) |> PlausibleWeb.StatsView.large_number_format() 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 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""" """ end def paddle_script(assigns) do ~H""" """ end def upgrade_link(%{business_tier: true} = assigns) do ~H""" Upgrade """ end def upgrade_link(assigns) do ~H""" Upgrade """ 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" end