defmodule PlausibleWeb.Components.Billing do @moduledoc false 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={Plausible.Billing.upgrade_route_for(@current_user)}> 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 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 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 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 subscription_cancelled_notice_body(assigns) do if Plausible.Billing.Subscriptions.expired?(assigns.user.subscription) do ~H""" <.link class="underline inline-block" href={Plausible.Billing.upgrade_route_for(@user)}> Upgrade your subscription

to get access to your stats again.

""" else ~H"""

You have access to your stats until <%= Timex.format!(@user.subscription.next_bill_date, "{Mshort} {D}, {YYYY}") %>. <.link class="underline inline-block" href={Plausible.Billing.upgrade_route_for(@user)}> 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} = user} = assigns) do business_tiers_available? = FunWithFlags.enabled?(:business_tier, for: user) plan = Plans.get_regular_plan(subscription, only_non_expired: true) loses_grandfathering = business_tiers_available? && 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()}), do: "Upgrade" defp change_plan_or_upgrade_text(_subscription), do: "Change plan" end