diff --git a/lib/mix/tasks/cancel_subscription.ex b/lib/mix/tasks/cancel_subscription.ex index e6390e273..27299c5de 100644 --- a/lib/mix/tasks/cancel_subscription.ex +++ b/lib/mix/tasks/cancel_subscription.ex @@ -8,16 +8,17 @@ defmodule Mix.Tasks.CancelSubscription do use Mix.Task use Plausible.Repo - alias Plausible.{Repo, Billing.Subscription} + require Plausible.Billing.Subscription.Status require Logger + alias Plausible.{Repo, Billing.Subscription} def run([paddle_subscription_id]) do Mix.Task.run("app.start") Repo.get_by!(Subscription, paddle_subscription_id: paddle_subscription_id) - |> Subscription.changeset(%{status: "deleted"}) + |> Subscription.changeset(%{status: Subscription.Status.deleted()}) |> Repo.update!() - Logger.info("Successfully set the subscription status to 'deleted'.") + Logger.info("Successfully set the subscription status to #{Subscription.Status.deleted()}") end end diff --git a/lib/plausible/auth/auth.ex b/lib/plausible/auth/auth.ex index 1d78a0852..126e27553 100644 --- a/lib/plausible/auth/auth.ex +++ b/lib/plausible/auth/auth.ex @@ -118,10 +118,11 @@ defmodule Plausible.Auth do user_id in Application.get_env(:plausible, :super_admin_user_ids) end - def enterprise?(nil), do: false + def enterprise_configured?(nil), do: false - def enterprise?(%Plausible.Auth.User{} = user) do - user = Repo.preload(user, :enterprise_plan) - user.enterprise_plan != nil + def enterprise_configured?(%Plausible.Auth.User{} = user) do + user + |> Ecto.assoc(:enterprise_plan) + |> Repo.exists?() end end diff --git a/lib/plausible/auth/user_admin.ex b/lib/plausible/auth/user_admin.ex index 264ded630..b1a5016eb 100644 --- a/lib/plausible/auth/user_admin.ex +++ b/lib/plausible/auth/user_admin.ex @@ -1,5 +1,7 @@ defmodule Plausible.Auth.UserAdmin do use Plausible.Repo + require Plausible.Billing.Subscription.Status + alias Plausible.Billing.Subscription def custom_index_query(_conn, _schema, query) do subscripton_q = from(s in Plausible.Billing.Subscription, order_by: [desc: s.inserted_at]) @@ -83,7 +85,7 @@ defmodule Plausible.Auth.UserAdmin do end defp subscription_plan(user) do - if user.subscription && user.subscription.status == "active" && + if user.subscription && user.subscription.status == Subscription.Status.active() && user.subscription.paddle_subscription_id do quota = PlausibleWeb.AuthView.subscription_quota(user.subscription) interval = PlausibleWeb.AuthView.subscription_interval(user.subscription) diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex index 932f55adb..a7fcf4814 100644 --- a/lib/plausible/billing/billing.ex +++ b/lib/plausible/billing/billing.ex @@ -1,5 +1,6 @@ defmodule Plausible.Billing do use Plausible.Repo + require Plausible.Billing.Subscription.Status alias Plausible.Billing.Subscription @spec active_subscription_for(integer()) :: Subscription.t() | nil @@ -92,10 +93,12 @@ defmodule Plausible.Billing do end end - defp subscription_is_active?(%Subscription{status: "active"}), do: true - defp subscription_is_active?(%Subscription{status: "past_due"}), do: true + defp subscription_is_active?(%Subscription{status: Subscription.Status.active()}), do: true + defp subscription_is_active?(%Subscription{status: Subscription.Status.past_due()}), do: true - defp subscription_is_active?(%Subscription{status: "deleted"} = subscription) do + defp subscription_is_active?( + %Subscription{status: Subscription.Status.deleted()} = subscription + ) do subscription.next_bill_date && !Timex.before?(subscription.next_bill_date, Timex.today()) end @@ -269,7 +272,7 @@ defmodule Plausible.Billing do defp active_subscription_query(user_id) do from(s in Subscription, - where: s.user_id == ^user_id and s.status == "active", + where: s.user_id == ^user_id and s.status == ^Subscription.Status.active(), order_by: [desc: s.inserted_at], limit: 1 ) diff --git a/lib/plausible/billing/plans.ex b/lib/plausible/billing/plans.ex index d179d92e2..5520c1680 100644 --- a/lib/plausible/billing/plans.ex +++ b/lib/plausible/billing/plans.ex @@ -134,6 +134,19 @@ defmodule Plausible.Billing.Plans do end end + def latest_enterprise_plan_with_price(user) do + enterprise_plan = + Repo.one!( + from(e in EnterprisePlan, + where: e.user_id == ^user.id, + order_by: [desc: e.inserted_at], + limit: 1 + ) + ) + + {enterprise_plan, get_price_for(enterprise_plan)} + end + def subscription_interval(subscription) do case get_subscription_plan(subscription) do %EnterprisePlan{billing_interval: interval} -> @@ -185,6 +198,13 @@ defmodule Plausible.Billing.Plans do end end + def get_price_for(%EnterprisePlan{paddle_plan_id: product_id}) do + case Plausible.Billing.paddle_api().fetch_prices([product_id]) do + {:ok, prices} -> Map.fetch!(prices, product_id) + {:error, :api_error} -> nil + end + end + defp get_enterprise_plan(%Subscription{} = subscription) do Repo.get_by(EnterprisePlan, user_id: subscription.user_id, @@ -217,7 +237,7 @@ defmodule Plausible.Billing.Plans do def suggest(user, usage_during_cycle) do cond do usage_during_cycle > @enterprise_level_usage -> :enterprise - Plausible.Auth.enterprise?(user) -> :enterprise + Plausible.Auth.enterprise_configured?(user) -> :enterprise true -> suggest_by_usage(user, usage_during_cycle) end end diff --git a/lib/plausible/billing/subscription.ex b/lib/plausible/billing/subscription.ex index d5f4927ad..e5c72af0f 100644 --- a/lib/plausible/billing/subscription.ex +++ b/lib/plausible/billing/subscription.ex @@ -1,38 +1,10 @@ defmodule Plausible.Billing.Subscription do + @moduledoc false + use Ecto.Schema import Ecto.Changeset - - @moduledoc """ - The subscription statuses are stored in Paddle. They can only be changed - through Paddle webhooks, which always send the current subscription status - via the payload. - - * `active` - All good with the payments. Can access stats. - - * `past_due` - The payment has failed, but we're trying to charge the customer - again. Access to stats is still granted. There will be three retries - after - 3, 5, and 7 days have passed from the first failure. After a failure on the - final retry, the subscription status will change to `paused`. As soon as the - customer updates their billing details, Paddle will charge them again, and - after a successful payment, the subscription will become `active` again. - - * `paused` - we've tried to charge the customer but all the retries have failed. - Stats access restricted. As soon as the customer updates their billing details, - Paddle will charge them again, and after a successful payment, the subscription - will become `active` again. - - * `deleted` - The customer has triggered the cancel subscription action. Access - to stats should be granted for the time the customer has already paid for. If - they want to upgrade again, new billing details have to be provided. - - Paddle documentation links for reference: - - * Subscription statuses - - https://developer.paddle.com/classic/reference/zg9joji1mzu0mdi2-subscription-status-reference - - * Payment failures - - https://developer.paddle.com/classic/guides/zg9joji1mzu0mduy-payment-failures - """ + require Plausible.Billing.Subscription.Status + alias Plausible.Billing.Subscription @type t() :: %__MODULE__{} @@ -50,14 +22,12 @@ defmodule Plausible.Billing.Subscription do @optional_fields [:last_bill_date] - @valid_statuses ["active", "past_due", "deleted", "paused"] - schema "subscriptions" do field :paddle_subscription_id, :string field :paddle_plan_id, :string field :update_url, :string field :cancel_url, :string - field :status, :string + field :status, Ecto.Enum, values: Subscription.Status.valid_statuses() field :next_bill_amount, :string field :next_bill_date, :date field :last_bill_date, :date @@ -72,20 +42,18 @@ defmodule Plausible.Billing.Subscription do model |> cast(attrs, @required_fields ++ @optional_fields) |> validate_required(@required_fields) - |> validate_inclusion(:status, @valid_statuses) |> unique_constraint(:paddle_subscription_id) end def free(attrs \\ %{}) do %__MODULE__{ paddle_plan_id: "free_10k", - status: "active", + status: Subscription.Status.active(), next_bill_amount: "0", currency_code: "EUR" } |> cast(attrs, @required_fields) |> validate_required([:user_id]) - |> validate_inclusion(:status, @valid_statuses) |> unique_constraint(:paddle_subscription_id) end end diff --git a/lib/plausible/billing/subscription/status.ex b/lib/plausible/billing/subscription/status.ex new file mode 100644 index 000000000..8e63468ec --- /dev/null +++ b/lib/plausible/billing/subscription/status.ex @@ -0,0 +1,45 @@ +defmodule Plausible.Billing.Subscription.Status do + @moduledoc """ + The subscription statuses are stored in Paddle. They can only be changed + through Paddle webhooks, which always send the current subscription status + via the payload. + + * `active` - All good with the payments. Can access stats. + + * `past_due` - The payment has failed, but we're trying to charge the customer + again. Access to stats is still granted. There will be three retries - after + 3, 5, and 7 days have passed from the first failure. After a failure on the + final retry, the subscription status will change to `paused`. As soon as the + customer updates their billing details, Paddle will charge them again, and + after a successful payment, the subscription will become `active` again. + + * `paused` - we've tried to charge the customer but all the retries have failed. + Stats access restricted. As soon as the customer updates their billing details, + Paddle will charge them again, and after a successful payment, the subscription + will become `active` again. + + * `deleted` - The customer has triggered the cancel subscription action. Access + to stats should be granted for the time the customer has already paid for. If + they want to upgrade again, new billing details have to be provided. + + Paddle documentation links for reference: + + * Subscription statuses - + https://developer.paddle.com/classic/reference/zg9joji1mzu0mdi2-subscription-status-reference + + * Payment failures - + https://developer.paddle.com/classic/guides/zg9joji1mzu0mduy-payment-failures + """ + + @statuses [:active, :past_due, :paused, :deleted] + + @type status() :: unquote(Enum.reduce(@statuses, &{:|, [], [&1, &2]})) + + for status <- @statuses do + defmacro unquote(status)(), do: unquote(status) + end + + def valid_statuses() do + @statuses + end +end diff --git a/lib/plausible/billing/subscriptions.ex b/lib/plausible/billing/subscriptions.ex index f98b635b3..6c0bc9825 100644 --- a/lib/plausible/billing/subscriptions.ex +++ b/lib/plausible/billing/subscriptions.ex @@ -1,7 +1,8 @@ defmodule Plausible.Billing.Subscriptions do @moduledoc false - alias Plausible.Billing.{Subscription} + require Plausible.Billing.Subscription.Status + alias Plausible.Billing.Subscription @spec expired?(Subscription.t()) :: boolean() @doc """ @@ -14,9 +15,19 @@ defmodule Plausible.Billing.Subscriptions do def expired?(%Subscription{paddle_plan_id: "free_10k"}), do: false def expired?(%Subscription{status: status, next_bill_date: next_bill_date}) do - cancelled? = status == "deleted" + cancelled? = status == Subscription.Status.deleted() expired? = Timex.compare(next_bill_date, Timex.today()) < 0 cancelled? && expired? end + + def resumable?(nil), do: false + + def resumable?(%Subscription{status: status}) do + status in [ + Subscription.Status.active(), + 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 6e4f55bfe..ac22eee9b 100644 --- a/lib/plausible_web/components/billing.ex +++ b/lib/plausible_web/components/billing.ex @@ -3,6 +3,7 @@ defmodule PlausibleWeb.Components.Billing do use Phoenix.Component import PlausibleWeb.Components.Generic + require Plausible.Billing.Subscription.Status alias PlausibleWeb.Router.Helpers, as: Routes alias Plausible.Billing.Subscription @@ -50,6 +51,7 @@ defmodule PlausibleWeb.Components.Billing do def monthly_quota_box(%{business_tier: true} = assigns) do ~H"""