defmodule PlausibleWeb.Components.Billing do @moduledoc false use Phoenix.Component import PlausibleWeb.Components.Generic require Plausible.Billing.Subscription.Status alias PlausibleWeb.Router.Helpers, as: Routes alias Plausible.Billing.Subscription 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 private_preview? = not FunWithFlags.enabled?(:business_tier, for: assigns.current_user) display_upgrade_link? = assigns.current_user.id == assigns.billable_user.id has_access? = assigns.feature_mod.check_availability(assigns.billable_user) == :ok message = cond do private_preview? && assigns.grandfathered? -> "#{assigns.feature_mod.display_name()} is an upcoming premium functionality that's free-to-use during the private preview. Existing subscribers will be grandfathered and will keep having access to this feature without any change to their plan." private_preview? -> "#{assigns.feature_mod.display_name()} is an upcoming premium functionality that's free-to-use during the private preview. Pricing will be announced soon." Plausible.Billing.on_trial?(assigns.billable_user) -> "#{assigns.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." 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)}> upgrade your subscription to the Business plan. """ not has_access? && not display_upgrade_link? -> "#{assigns.feature_mod.display_name()} 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." true -> nil end assigns = assign(assigns, :message, message) ~H""" <.notice :if={@message} class="rounded-t-md rounded-b-none" size={@size} {@rest}> <%= @message %> """ 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={show_upgrade_or_change_plan_link?(@user, @subscription)} id="#upgrade-or-change-plan-link" href={upgrade_link_href(@user)} 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(), 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", "") end def paddle_button(assigns) do ~H""" """ end def paddle_script(assigns) do ~H""" """ end def upgrade_link(%{business_tier: true} = assigns) do ~H""" <.link id="upgrade-link-2" href={upgrade_link_href(@user)} class="inline-block px-3 py-2 border border-transparent text-sm leading-4 font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-500 focus:outline-none focus:border-indigo-700 focus:ring active:bg-indigo-700 transition ease-in-out duration-150" > Upgrade """ end def upgrade_link(assigns) do ~H""" <.link href={Routes.billing_path(PlausibleWeb.Endpoint, :upgrade)} class="inline-block px-3 py-2 border border-transparent text-sm leading-4 font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-500 focus:outline-none focus:border-indigo-700 focus:ring active:bg-indigo-700 transition ease-in-out duration-150" > Upgrade """ 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