Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Paddle audit (#6819)

Adamc468bcf9626e
diff --git a/lib/plausible/billing/migration_audit.ex b/lib/plausible/billing/migration_audit.ex
new file mode 100644
index 0000000000..5980042940
--- /dev/null
+++ b/lib/plausible/billing/migration_audit.ex
@@ -0,0 +1,671 @@
+defmodule Plausible.Billing.MigrationAudit do
+ @moduledoc """
+ Read-only audit report for the Paddle Classic->Billing migration.
+ Paddle is only called when explicitly asked for with `catalog: true`,
+ and then with read-only requests.
+
+ ## Answering the audit asked for in the Paddle Billing project
+
+ | Item | Where |
+ | ----------------------------------------------- | ----------------------------------------------------------------------------------------------- |
+ | Active subscriptions by plan id | `plan_resolution/1`, `:by_plan_id` |
+ | Plan generation | `plan_resolution/1`, `:by_generation` |
+ | Enterprise plans attached to live subscriptions | `plan_resolution/1`, `:enterprise_plans` |
+ | Active, paused, trialing, past-due | `subscribers/1`, `:statuses` |
+ | Plans with multiple currency prices | `classic_catalog/1`, `:plans` |
+ | Customer-specific or negotiated plans | `subscribers/1`, `:enterprise` and `:vs_list_price`; `classic_catalog/1`, `:plans` `:by_origin` |
+ | Recurring or one-time modifiers | `classic_catalog/1`, `:modifiers` |
+ | Effective currency and amount paid | `subscribers/1`, `:billed` and `:vs_list_price` |
+ """
+
+ use Plausible.Repo
+
+ import Ecto.Query, only: [from: 2, dynamic: 2]
+
+ require Plausible.Billing.Subscription.Status
+
+ alias Plausible.Billing.{EnterprisePlan, Plans, Subscription}
+ alias Plausible.Billing.MigrationAudit.{ClassicCatalog, Subscribers}
+ alias Plausible.Billing.Subscription.Status
+
+ @plan_files [
+ legacy_plans: 1,
+ plans_v1: 1,
+ plans_v2: 2,
+ plans_v3: 3,
+ plans_v4: 4,
+ plans_v5: 5
+ ]
+
+ @classic_id_format ~r/^\d+$/
+
+ @type category ::
+ :free
+ | :manual
+ | :enterprise
+ | :unresolved
+ | {:regular, generation :: integer(), kind :: atom()}
+
+ @type problem :: %{check: atom(), file: String.t() | nil, message: String.t()}
+
+ @doc """
+ bin/plausible rpc "IO.puts(Plausible.Billing.MigrationAudit.json_report())"
+
+ or, from a remote shell:
+
+ iex> IO.puts(Plausible.Billing.MigrationAudit.json_report())
+ """
+ @spec json_report(Keyword.t()) :: String.t()
+ def json_report(opts \\ []) do
+ opts
+ |> Keyword.put_new(:catalog, true)
+ |> collect()
+ |> update_in([:plan_resolution, :unresolved], fn rows ->
+ %{count: length(rows), rows: "left out, see plan_resolution/1"}
+ end)
+ |> to_json()
+ end
+
+ @spec collect(Keyword.t()) :: map()
+ def collect(opts \\ []) do
+ result = %{
+ plan_resolution: plan_resolution(opts),
+ plan_files: plan_files_integrity(opts),
+ subscribers: subscribers(opts)
+ }
+
+ if Keyword.get(opts, :catalog, false) do
+ Map.put(result, :classic_catalog, classic_catalog_result(opts))
+ else
+ result
+ end
+ end
+
+ defp classic_catalog_result(opts) do
+ case classic_catalog(opts) do
+ {:ok, catalog} ->
+ catalog
+
+ {:error, {part, reason}} ->
+ %{error: %{failed_call: part, reason: inspect(reason)}}
+ end
+ end
+
+ @doc """
+ Like `collect/1`, but prints a readable report, and returns the same data.
+ """
+ def run(opts \\ []) do
+ result = collect(opts)
+
+ print_plan_resolution(result.plan_resolution)
+ print_plan_files(result.plan_files)
+ print_subscribers(result.subscribers)
+
+ case Map.fetch(result, :classic_catalog) do
+ {:ok, catalog} ->
+ print_classic_catalog(catalog)
+
+ :error ->
+ log("Classic catalog and modifiers skipped. Run with `catalog: true` to include them.")
+ end
+
+ result
+ end
+
+ @doc """
+ Subscribers by status, effective amounts and enterprise plans. Database only,
+ see `Plausible.Billing.MigrationAudit.Subscribers.audit/1` for the result and options.
+ """
+ defdelegate subscribers(opts \\ []), to: Subscribers, as: :audit
+
+ @doc """
+ Plan catalog and modifiers from Paddle Classic. Makes read-only API calls, see
+ `Plausible.Billing.MigrationAudit.ClassicCatalog.audit/1` for the result and options.
+ """
+ defdelegate classic_catalog(opts \\ []), to: ClassicCatalog, as: :audit
+
+ @doc """
+ Encodes a result of this module as pretty JSON.
+ """
+ @spec to_json(term()) :: String.t()
+ def to_json(result), do: Jason.encode!(result, pretty: true)
+
+ # --- plan resolution ----------------------------------------------------------
+
+ @doc """
+ Resolves subscriptions to plans exactly like the app does
+ (`Plausible.Billing.Plans.get_subscription_plan/1`) and summarises the result.
+
+ Options:
+
+ * `:scope` - `:live` (default) audits every subscription that could matter
+ for a port: active, past due or paused, plus cancelled ones that are still
+ inside the paid period. `:current` audits the last subscription of every
+ team, which is the one the app uses to determine a team's plan.
+ * `:today` - the date used for "still inside the paid period" (default:
+ today, UTC).
+ """
+ @spec plan_resolution(Keyword.t()) :: map()
+ def plan_resolution(opts \\ []) do
+ scope = Keyword.get(opts, :scope, :live)
+ today = Keyword.get(opts, :today, Date.utc_today())
+
+ subscriptions = Repo.all(subscriptions_query(scope, today))
+ classified = Enum.map(subscriptions, &{&1, classify(&1)})
+
+ %{
+ scope: scope,
+ today: today,
+ total: length(subscriptions),
+ by_category: count_by(classified, fn {_s, category} -> category_label(category) end),
+ by_plan_id: by_plan_id(classified),
+ by_generation: by_generation(classified),
+ unresolved:
+ for {subscription, :unresolved} <- classified do
+ %{
+ subscription_id: subscription.id,
+ team_id: subscription.team_id,
+ paddle_plan_id: subscription.paddle_plan_id,
+ status: subscription.status,
+ next_bill_date: subscription.next_bill_date
+ }
+ end,
+ teams_with_multiple_subscriptions: teams_with_multiple_subscriptions(today),
+ enterprise_plans: enterprise_plans_summary(today)
+ }
+ end
+
+ @doc """
+ Classifies the plan of a single subscription.
+ """
+ @spec classify(Subscription.t()) :: category()
+ def classify(%Subscription{} = subscription) do
+ if Subscription.manual_subscription?(subscription) do
+ :manual
+ else
+ case Plans.get_subscription_plan(subscription) do
+ nil -> :unresolved
+ :free_10k -> :free
+ %EnterprisePlan{} -> :enterprise
+ %{generation: generation, kind: kind} -> {:regular, generation, kind}
+ end
+ end
+ end
+
+ defp subscriptions_query(:live, today) do
+ from(s in Subscription, where: ^live_condition(today), order_by: [asc: s.id])
+ end
+
+ defp subscriptions_query(:current, _today) do
+ # one row per team: the last subscription, as in Teams.last_subscription_query/0
+ from(s in Subscription,
+ distinct: s.team_id,
+ order_by: [asc: s.team_id, desc: s.inserted_at, desc: s.id]
+ )
+ end
+
+ defp live_condition(today) do
+ live = [Status.active(), Status.past_due(), Status.paused()]
+
+ dynamic(
+ [s],
+ s.status in ^live or (s.status == ^Status.deleted() and s.next_bill_date >= ^today)
+ )
+ end
+
+ defp count_by(items, fun) do
+ items
+ |> Enum.frequencies_by(fun)
+ |> Enum.map(fn {category, n} -> %{category: category, subscriptions: n} end)
+ |> Enum.sort_by(&{-&1.subscriptions, &1.category})
+ end
+
+ defp category_label({:regular, generation, kind}), do: "v#{generation} #{kind}"
+ defp category_label(other), do: Atom.to_string(other)
+
+ defp by_plan_id(classified) do
+ classified
+ |> Enum.group_by(fn {s, category} -> {s.paddle_plan_id, category_label(category)} end)
+ |> Enum.map(fn {{plan_id, label}, items} ->
+ %{paddle_plan_id: plan_id, category: label, subscriptions: length(items)}
+ end)
+ |> Enum.sort_by(&{-&1.subscriptions, &1.paddle_plan_id})
+ end
+
+ # For every generation of the static plans: how many live subscriptions and
+ # distinct plan ids are in use, against how many plan ids the files contain.
+ # This is the list of Billing prices that are actually needed per generation.
+ defp by_generation(classified) do
+ in_use =
+ for {s, {:regular, generation, _kind}} <- classified, do: {generation, s.paddle_plan_id}
+
+ ids_in_files =
+ Plans.all()
+ |> Enum.flat_map(fn plan ->
+ for id <- [plan.monthly_product_id, plan.yearly_product_id],
+ is_binary(id),
+ do: {plan.generation, id}
+ end)
+
+ for generation <- ids_in_files |> Enum.map(&elem(&1, 0)) |> Enum.uniq() |> Enum.sort() do
+ used = for {^generation, id} <- in_use, do: id
+
+ %{
+ generation: generation,
+ subscriptions: length(used),
+ distinct_plan_ids_in_use: used |> Enum.uniq() |> length(),
+ plan_ids_in_files: for({^generation, _} <- ids_in_files, do: 1) |> length()
+ }
+ end
+ end
+
+ defp teams_with_multiple_subscriptions(today) do
+ all =
+ from(s in Subscription, group_by: s.team_id, having: count(s.id) > 1, select: s.team_id)
+
+ live =
+ from(s in Subscription,
+ where: ^live_condition(today),
+ group_by: s.team_id,
+ having: count(s.id) > 1,
+ select: s.team_id
+ )
+
+ %{any_status: length(Repo.all(all)), live: length(Repo.all(live))}
+ end
+
+ defp enterprise_plans_summary(today) do
+ attached = fn condition ->
+ from(ep in EnterprisePlan,
+ as: :ep,
+ where:
+ exists(
+ from(s in Subscription,
+ where: s.team_id == parent_as(:ep).team_id,
+ where: s.paddle_plan_id == parent_as(:ep).paddle_plan_id,
+ where: ^condition
+ )
+ ),
+ select: count(ep.id)
+ )
+ |> Repo.one()
+ end
+
+ %{
+ total: Repo.aggregate(EnterprisePlan, :count),
+ attached_to_live_subscription: attached.(live_condition(today)),
+ attached_to_any_subscription: attached.(dynamic([s], true))
+ }
+ end
+
+ @doc """
+ Checks the static plan files and returns a list of problems (empty if all is
+ well). Reads the files from the release's `priv` directory.
+
+ Options:
+
+ * `:priv_dir` - directory to read from (default: the app's `priv`).
+ """
+ @spec plan_files_integrity(Keyword.t()) :: [problem()]
+ def plan_files_integrity(opts \\ []) do
+ priv_dir = Keyword.get(opts, :priv_dir, Application.app_dir(:plausible, "priv"))
+
+ files =
+ for {name, _generation} <- @plan_files, into: %{} do
+ file = Atom.to_string(name)
+ {file, priv_dir |> Path.join("#{file}.json") |> read_json()}
+ end
+
+ prices = priv_dir |> Path.join("plan_prices.json") |> read_json()
+
+ check_plan_files(files, prices)
+ end
+
+ defp read_json(path) do
+ with {:ok, content} <- File.read(path),
+ {:ok, decoded} <- Jason.decode(content) do
+ decoded
+ else
+ {:error, %Jason.DecodeError{} = error} ->
+ {:error, "#{Path.basename(path)}: invalid JSON (#{Exception.message(error)})"}
+
+ {:error, reason} ->
+ {:error, "#{Path.basename(path)}: #{:file.format_error(reason)}"}
+ end
+ end
+
+ @doc """
+ Pure part of `plan_files_integrity/1`, for testing and for use on data that
+ isn't on disk.
+
+ `files` maps a file name without extension (for example `"plans_v1"`) to its
+ decoded JSON (a list of plan maps), `prices` is
+ the decoded `plan_prices.json` (plan id => price). A value of
+ `{:error, message}` stands for a file that couldn't be read.
+
+ Checks:
+
+ * `:unreadable` - a file is missing or isn't valid
+ * `:generation_mismatch` - a plan's `generation` doesn't match its file
+ * `:bad_id_format` - an id isn't a numeric (Paddle Classic) id
+ * `:same_monthly_and_yearly_id` - a plan uses one id for both intervals
+ * `:duplicate_id` - an id is used more than once
+ * `:duplicate_plan` - two plans in a file share kind and pageview limit
+ * `:missing_price` / `:orphan_price` / `:bad_price` - `plan_prices.json` has
+ to hold a positive price for every plan id, and nothing else
+ """
+ @spec check_plan_files(map(), map() | {:error, String.t()}) :: [problem()]
+ def check_plan_files(files, prices) do
+ {readable, unreadable} =
+ Enum.split_with(files, fn {_name, content} -> is_list(content) end)
+
+ readable = Map.new(readable)
+
+ unreadable_problems =
+ for {name, {:error, message}} <- unreadable do
+ problem(:unreadable, name, message)
+ end
+
+ prices_problems =
+ case prices do
+ {:error, message} -> [problem(:unreadable, "plan_prices.json", message)]
+ prices when is_map(prices) -> check_prices(readable, prices)
+ end
+
+ unreadable_problems ++
+ Enum.flat_map(readable, fn {name, plans} -> check_file(name, plans) end) ++
+ check_duplicate_ids(readable) ++
+ prices_problems
+ end
+
+ defp problem(check, file, message), do: %{check: check, file: file, message: message}
+
+ defp file_generation(name) do
+ Keyword.fetch!(@plan_files, String.to_existing_atom(name))
+ end
+
+ defp plan_ids(plan) do
+ for key <- ["monthly_product_id", "yearly_product_id"],
+ id = plan[key],
+ not is_nil(id),
+ do: {key, id}
+ end
+
+ defp check_file(name, plans) do
+ generation = file_generation(name)
+
+ per_plan =
+ plans
+ |> Enum.with_index(1)
+ |> Enum.flat_map(fn {plan, index} ->
+ label = "#{name} ##{index} (#{plan["kind"]}, #{plan["monthly_pageview_limit"]})"
+
+ generation_problems =
+ if plan["generation"] != generation do
+ [
+ problem(
+ :generation_mismatch,
+ name,
+ "#{label}: generation is #{inspect(plan["generation"])}, expected #{generation}"
+ )
+ ]
+ else
+ []
+ end
+
+ id_problems =
+ for {key, id} <- plan_ids(plan),
+ not (is_binary(id) and Regex.match?(@classic_id_format, id)) do
+ problem(:bad_id_format, name, "#{label}: #{key} #{inspect(id)} isn't a numeric id")
+ end
+
+ same_id_problems =
+ case plan_ids(plan) do
+ [{_, id}, {_, id}] ->
+ [
+ problem(
+ :same_monthly_and_yearly_id,
+ name,
+ "#{label}: monthly and yearly both use #{inspect(id)}"
+ )
+ ]
+
+ _ ->
+ []
+ end
+
+ generation_problems ++ id_problems ++ same_id_problems
+ end)
+
+ duplicate_plans =
+ plans
+ |> Enum.frequencies_by(&{&1["kind"], &1["monthly_pageview_limit"]})
+ |> Enum.filter(fn {_key, count} -> count > 1 end)
+ |> Enum.map(fn {{kind, limit}, count} ->
+ problem(
+ :duplicate_plan,
+ name,
+ "#{count} plans with kind #{inspect(kind)} and limit #{inspect(limit)}"
+ )
+ end)
+
+ per_plan ++ duplicate_plans
+ end
+
+ defp check_duplicate_ids(files) do
+ for {id, owners} <- ids_with_owners(files), length(owners) > 1 do
+ problem(
+ :duplicate_id,
+ nil,
+ "#{inspect(id)} is used #{length(owners)} times (#{Enum.join(owners, ", ")})"
+ )
+ end
+ end
+
+ defp ids_with_owners(files) do
+ files
+ |> Enum.flat_map(fn {name, plans} ->
+ for plan <- plans, {key, id} <- plan_ids(plan), do: {id, "#{name} #{key}"}
+ end)
+ |> Enum.group_by(&elem(&1, 0), &elem(&1, 1))
+ end
+
+ defp check_prices(files, prices) do
+ known_ids =
+ for {_name, plans} <- files,
+ plan <- plans,
+ {_key, id} <- plan_ids(plan),
+ into: MapSet.new(),
+ do: id
+
+ missing =
+ for id <- known_ids, not Map.has_key?(prices, id) do
+ problem(:missing_price, "plan_prices.json", "no price for plan id #{inspect(id)}")
+ end
+
+ orphans =
+ for {id, _price} <- prices, not MapSet.member?(known_ids, id) do
+ problem(:orphan_price, "plan_prices.json", "price for unknown plan id #{inspect(id)}")
+ end
+
+ bad =
+ for {id, price} <- prices, not (is_number(price) and price > 0) do
+ problem(
+ :bad_price,
+ "plan_prices.json",
+ "price for #{inspect(id)} is #{inspect(price)}, expected a positive number"
+ )
+ end
+
+ Enum.sort_by(missing ++ orphans ++ bad, & &1.message)
+ end
+
+ # --- printing ------------------------------------------------------------------------
+
+ defp print_plan_resolution(result) do
+ log("Subscription plan resolution (scope: #{result.scope}, as of #{result.today})")
+ log(" subscriptions audited: #{result.total}")
+
+ log(" by category:")
+ for row <- result.by_category, do: log(" #{pad(row.category, 22)} #{row.subscriptions}")
+
+ log(" by generation (regular plans only):")
+
+ for g <- result.by_generation do
+ log(
+ " v#{g.generation}: #{g.subscriptions} subscriptions on " <>
+ "#{g.distinct_plan_ids_in_use} of #{g.plan_ids_in_files} plan ids"
+ )
+ end
+
+ log(" by plan id (top 15):")
+
+ for row <- Enum.take(result.by_plan_id, 15) do
+ log(" #{pad(row.paddle_plan_id, 22)} #{pad(row.category, 16)} #{row.subscriptions}")
+ end
+
+ multiple = result.teams_with_multiple_subscriptions
+
+ log(
+ " teams with more than one subscription row: #{multiple.any_status} " <>
+ "(#{multiple.live} with more than one live row)"
+ )
+
+ enterprise = result.enterprise_plans
+
+ log(
+ " enterprise plans: #{enterprise.total} total, " <>
+ "#{enterprise.attached_to_live_subscription} attached to a live subscription, " <>
+ "#{enterprise.attached_to_any_subscription} attached to any subscription"
+ )
+
+ case result.unresolved do
+ [] ->
+ log(" unresolved subscriptions: none")
+
+ unresolved ->
+ log(" UNRESOLVED subscriptions (#{length(unresolved)}), these lose their plan limits:")
+
+ for row <- unresolved do
+ log(
+ " subscription ##{row.subscription_id} team ##{row.team_id} " <>
+ "plan #{inspect(row.paddle_plan_id)} #{row.status} next_bill_date=#{row.next_bill_date}"
+ )
+ end
+ end
+ end
+
+ defp print_plan_files([]), do: log("Plan files: no problems found")
+
+ defp print_plan_files(problems) do
+ log("Plan files: #{length(problems)} problem(s)")
+
+ for p <- problems do
+ log(" [#{p.check}] #{p.file || "-"}: #{p.message}")
+ end
+ end
+
+ defp print_subscribers(result) do
+ log("Subscribers (as of #{result.as_of})")
+ statuses = result.statuses
+ log(" current subscription of #{statuses.teams_with_subscription} teams, by status:")
+ for row <- statuses.by_status, do: log(" #{pad(row.status, 22)} #{row.teams}")
+ log(" teams in the app's own trial without a subscription: #{statuses.teams_on_app_trial}")
+
+ log(" paid by live subscribers (kind, currency, interval):")
+
+ for row <- result.billed do
+ log(
+ " #{pad(row.kind, 11)} #{pad(row.currency, 4)} #{pad(row.interval, 8)} " <>
+ "#{pad(row.subscriptions, 7)} total #{row.total} (min #{row.min}, max #{row.max})" <>
+ if(row.unparsable_amounts > 0,
+ do: ", #{row.unparsable_amounts} unparsable",
+ else: ""
+ )
+ )
+ end
+
+ log(" regular plans, amount paid vs the USD list price:")
+
+ for row <- result.vs_list_price.by_currency do
+ log(
+ " #{pad(row.currency, 4)} #{row.subscriptions} subscriptions: #{row.equal} equal, " <>
+ "#{row.lower} lower, #{row.higher} higher, #{row.no_list_price} without list price, " <>
+ "#{row.unparsable} unparsable"
+ )
+ end
+
+ log(" most common off-list price points (top 10):")
+
+ for row <- Enum.take(result.vs_list_price.off_list_price_points, 10) do
+ log(
+ " plan #{pad(row.plan_id, 8)} #{row.currency} #{pad(row.interval, 8)} paid #{row.paid} " <>
+ "vs list #{row.list_price}: #{row.subscriptions}"
+ )
+ end
+
+ enterprise = result.enterprise
+
+ log(
+ " enterprise: #{enterprise.live_subscriptions} live subscriptions on " <>
+ "#{enterprise.distinct_plan_ids} plan ids, #{enterprise.distinct_price_points} distinct price points"
+ )
+
+ log(
+ " plan ids shared by several teams: #{enterprise.plan_ids_shared_by_several_teams}, " <>
+ "teams with several enterprise plans: #{enterprise.teams_with_several_enterprise_plans}"
+ )
+
+ log(" top price points:")
+
+ for row <- Enum.take(enterprise.top_price_points, 10) do
+ log(
+ " #{row.currency} #{pad(row.interval, 8)} #{pad(row.amount, 10)} #{row.subscriptions}"
+ )
+ end
+ end
+
+ defp print_classic_catalog(%{error: %{failed_call: part, reason: reason}}) do
+ log("Classic catalog: failed to fetch #{part}: #{reason}")
+ end
+
+ defp print_classic_catalog(%{plans: plans, modifiers: modifiers}) do
+ log("Classic catalog: #{plans.total} plans")
+ log(" by origin: #{inspect(plans.by_origin)}")
+
+ log(
+ " with a live subscription, by origin: #{inspect(plans.with_live_subscription_by_origin)}"
+ )
+
+ log(
+ " single currency: #{plans.single_currency}, multiple currencies: #{plans.multi_currency} " <>
+ "(#{plans.multi_currency_parity} at parity, #{plans.multi_currency_differing} differing)"
+ )
+
+ log(" multiple currencies by origin: #{inspect(plans.multi_currency_by_origin)}")
+
+ log(" without a recurring price: #{plans.without_recurring_price}")
+
+ for row <- plans.currency_sets do
+ log(" currencies #{Enum.join(row.currencies, ",")}: #{row.plans} plans")
+ end
+
+ case plans.live_plan_ids_missing_from_catalog do
+ [] -> log(" every live plan id is in the catalog")
+ ids -> log(" live plan ids missing from the catalog: #{Enum.join(ids, ", ")}")
+ end
+
+ log(
+ "Modifiers: #{modifiers.total} (#{modifiers.recurring} recurring, #{modifiers.one_time} one-time, " <>
+ "#{modifiers.recurrence_unknown} unknown) on #{modifiers.subscriptions_with_modifiers} subscriptions, " <>
+ "#{modifiers.of_which_live} live"
+ )
+
+ log(" discounts: #{modifiers.discounts}, surcharges: #{modifiers.surcharges}")
+ log(" #{modifiers.not_covered}")
+ end
+
+ defp pad(value, width), do: value |> to_string() |> String.pad_trailing(width)
+
+ defp log(message), do: IO.puts("[#{inspect(__MODULE__)}] #{message}")
+end
diff --git a/lib/plausible/billing/migration_audit/classic_catalog.ex b/lib/plausible/billing/migration_audit/classic_catalog.ex
new file mode 100644
index 0000000000..2a8c43c8ac
--- /dev/null
+++ b/lib/plausible/billing/migration_audit/classic_catalog.ex
@@ -0,0 +1,240 @@
+defmodule Plausible.Billing.MigrationAudit.ClassicCatalog do
+ @moduledoc """
+ Part of the Paddle Billing migration audit that needs Paddle Classic: the
+ plan catalog (which plans have prices in several currencies, and which kind of
+ plan each one is) and subscription modifiers. It only makes read-only calls
+ (`list_plans` and `list_modifiers`) and the output is aggregated: plan names
+ and descriptions (customer names, mostly) are never included.
+
+ See `Plausible.Billing.MigrationAudit`
+ """
+
+ use Plausible.Repo
+
+ import Ecto.Query, only: [from: 2]
+
+ require Plausible.Billing.Subscription.Status
+
+ alias Plausible.Billing.{EnterprisePlan, PaddleClassic, Plans, Subscription}
+ alias Plausible.Billing.Subscription.Status
+
+ @doc """
+ Fetches the catalog and the modifiers from Paddle Classic and summarises them.
+
+ Returns `{:ok, %{plans: ..., modifiers: ...}}`, or `{:error, {:plans | :modifiers, reason}}`.
+
+ Options:
+
+ * `:api` - the Classic API client (default: `PaddleClassic.api()`). Needs
+ `list_plans/0` and `list_modifiers/0`.
+ * `:plans`, `:modifiers` - already fetched lists, to skip the calls.
+ * `:today` - date for "inside the paid period" (default: today, UTC).
+ """
+ @spec audit(Keyword.t()) :: {:ok, map()} | {:error, {atom(), term()}}
+ def audit(opts \\ []) do
+ api = Keyword.get_lazy(opts, :api, &PaddleClassic.api/0)
+ today = Keyword.get(opts, :today, Date.utc_today())
+
+ with {:ok, plans} <- fetch(opts, :plans, &api.list_plans/0),
+ {:ok, modifiers} <- fetch(opts, :modifiers, &api.list_modifiers/0) do
+ live = live_subscriptions(today)
+
+ {:ok,
+ %{plans: summarise_plans(plans, live), modifiers: summarise_modifiers(modifiers, live)}}
+ end
+ end
+
+ defp fetch(opts, key, fun) do
+ case Keyword.fetch(opts, key) do
+ {:ok, value} -> {:ok, value}
+ :error -> with {:error, reason} <- fun.(), do: {:error, {key, reason}}
+ end
+ end
+
+ # plan id and subscription id of live subscriptions (see MigrationAudit.plan_resolution/1)
+ defp live_subscriptions(today) do
+ live = [Status.active(), Status.past_due(), Status.paused()]
+
+ from(s in Subscription,
+ where: s.status in ^live or (s.status == ^Status.deleted() and s.next_bill_date >= ^today),
+ select: {s.paddle_plan_id, s.paddle_subscription_id}
+ )
+ |> Repo.all()
+ end
+
+ defp summarise_plans(plans, live) do
+ static_ids = MapSet.new(Plans.all(), & &1.monthly_product_id) |> add_yearly_ids()
+
+ enterprise_ids =
+ from(ep in EnterprisePlan, distinct: true, select: ep.paddle_plan_id)
+ |> Repo.all()
+ |> MapSet.new()
+
+ live_plan_ids = live |> Enum.map(&elem(&1, 0)) |> MapSet.new()
+
+ described =
+ Enum.map(plans, fn plan ->
+ id = to_string(plan["id"])
+ prices = recurring_prices(plan)
+
+ %{
+ id: id,
+ origin: origin(id, static_ids, enterprise_ids),
+ prices: prices,
+ currencies: prices |> Map.keys() |> Enum.sort(),
+ parity?: parity?(prices),
+ billing: {plan["billing_type"], plan["billing_period"]},
+ live?: MapSet.member?(live_plan_ids, id)
+ }
+ end)
+
+ catalog_ids = MapSet.new(described, & &1.id)
+
+ multi = Enum.filter(described, &(length(&1.currencies) > 1))
+
+ %{
+ total: length(described),
+ by_origin: Enum.frequencies_by(described, & &1.origin),
+ with_live_subscription_by_origin:
+ described |> Enum.filter(& &1.live?) |> Enum.frequencies_by(& &1.origin),
+ without_recurring_price: Enum.count(described, &(&1.prices == %{})),
+ by_billing_period:
+ described
+ |> counts(& &1.billing)
+ |> Enum.map(fn {{type, period}, n} ->
+ %{billing_type: type, billing_period: period, plans: n}
+ end),
+ single_currency: length(described) - length(multi),
+ multi_currency: length(multi),
+ multi_currency_by_origin: Enum.frequencies_by(multi, & &1.origin),
+ multi_currency_parity: Enum.count(multi, & &1.parity?),
+ multi_currency_differing: Enum.count(multi, &(not &1.parity?)),
+ currency_sets:
+ described
+ |> counts(& &1.currencies)
+ |> Enum.map(fn {currencies, n} -> %{currencies: currencies, plans: n} end),
+ differing_examples:
+ multi
+ |> Enum.reject(& &1.parity?)
+ |> Enum.take(5)
+ |> Enum.map(&%{plan_id: &1.id, origin: &1.origin, prices: &1.prices}),
+ live_plan_ids_missing_from_catalog:
+ live_plan_ids
+ |> Enum.reject(
+ &(&1 in [Subscription.manual_plan_id(), "free_10k"] or MapSet.member?(catalog_ids, &1))
+ )
+ |> Enum.sort()
+ }
+ end
+
+ defp add_yearly_ids(set) do
+ Enum.reduce(Plans.all(), set, fn plan, acc ->
+ MapSet.put(acc, plan.yearly_product_id)
+ end)
+ end
+
+ defp origin(id, static_ids, enterprise_ids) do
+ cond do
+ MapSet.member?(static_ids, id) -> :static
+ MapSet.member?(enterprise_ids, id) -> :enterprise
+ true -> :other
+ end
+ end
+
+ defp recurring_prices(%{"recurring_price" => %{} = prices}) do
+ for {currency, amount} <- prices, {:ok, decimal} <- [to_decimal(amount)], into: %{} do
+ {currency, Decimal.to_string(Decimal.round(decimal, 2), :normal)}
+ end
+ end
+
+ defp recurring_prices(_plan), do: %{}
+
+ defp parity?(prices) do
+ prices
+ |> Map.values()
+ |> Enum.map(&Decimal.new/1)
+ |> Enum.uniq_by(&Decimal.to_string/1)
+ |> length() <= 1
+ end
+
+ defp summarise_modifiers(modifiers, live) do
+ live_subscription_ids = live |> Enum.map(&to_string(elem(&1, 1))) |> MapSet.new()
+
+ described =
+ Enum.map(modifiers, fn modifier ->
+ %{
+ subscription_id: to_string(modifier["subscription_id"]),
+ currency: modifier["currency"],
+ amount: to_decimal(modifier["amount"]),
+ recurring?: recurring?(modifier)
+ }
+ end)
+
+ subscription_ids = described |> Enum.map(& &1.subscription_id) |> Enum.uniq()
+
+ per_subscription = Enum.frequencies_by(described, & &1.subscription_id)
+
+ %{
+ total: length(described),
+ recurring: Enum.count(described, &(&1.recurring? == true)),
+ one_time: Enum.count(described, &(&1.recurring? == false)),
+ recurrence_unknown: Enum.count(described, &(&1.recurring? == :unknown)),
+ subscriptions_with_modifiers: length(subscription_ids),
+ of_which_live: Enum.count(subscription_ids, &MapSet.member?(live_subscription_ids, &1)),
+ max_modifiers_on_one_subscription:
+ per_subscription |> Map.values() |> Enum.max(fn -> 0 end),
+ discounts: Enum.count(described, &sign?(&1.amount, :lt)),
+ surcharges: Enum.count(described, &sign?(&1.amount, :gt)),
+ zero_or_unparsable:
+ Enum.count(described, &(not sign?(&1.amount, :lt) and not sign?(&1.amount, :gt))),
+ by_currency:
+ described
+ |> Enum.group_by(& &1.currency)
+ |> Enum.map(fn {currency, group} ->
+ amounts = for %{amount: {:ok, a}} <- group, do: a
+
+ %{
+ currency: currency,
+ modifiers: length(group),
+ net_amount:
+ case amounts do
+ [] ->
+ nil
+
+ amounts ->
+ amounts
+ |> Enum.reduce(&Decimal.add/2)
+ |> Decimal.round(2)
+ |> Decimal.to_string(:normal)
+ end
+ }
+ end)
+ |> Enum.sort_by(&{-&1.modifiers, &1.currency || ""}),
+ not_covered:
+ "One-time charges made outside modifiers aren't returned by the modifiers list. " <>
+ "Coupons aren't stored on our side or returned here."
+ }
+ end
+
+ defp recurring?(%{"is_recurring" => value}) when is_boolean(value), do: value
+ defp recurring?(_), do: :unknown
+
+ defp sign?({:ok, amount}, sign), do: Decimal.compare(amount, 0) == sign
+ defp sign?(_, _sign), do: false
+
+ defp counts(items, fun) do
+ items |> Enum.frequencies_by(fun) |> Enum.sort_by(fn {key, n} -> {-n, inspect(key)} end)
+ end
+
+ defp to_decimal(value) when is_integer(value), do: {:ok, Decimal.new(value)}
+ defp to_decimal(value) when is_float(value), do: {:ok, Decimal.from_float(value)}
+
+ defp to_decimal(value) when is_binary(value) do
+ case Decimal.parse(String.trim(value)) do
+ {decimal, ""} -> {:ok, decimal}
+ _ -> :error
+ end
+ end
+
+ defp to_decimal(_), do: :error
+end
diff --git a/lib/plausible/billing/migration_audit/subscribers.ex b/lib/plausible/billing/migration_audit/subscribers.ex
new file mode 100644
index 0000000000..9b8356e93c
--- /dev/null
+++ b/lib/plausible/billing/migration_audit/subscribers.ex
@@ -0,0 +1,323 @@
+defmodule Plausible.Billing.MigrationAudit.Subscribers do
+ @moduledoc """
+ Part of the Paddle Billing migration audit fetched from local db.
+ How subscribers are distributed by status, what they effectively
+ pay, and how enterprise (customer-specific) plans look. Read-only, and all
+ output is aggregated, no customer identifiers.
+
+ See `Plausible.Billing.MigrationAudit`
+ """
+
+ use Plausible.Repo
+
+ import Ecto.Query, only: [from: 2]
+
+ require Plausible.Billing.Subscription.Status
+
+ alias Plausible.Billing.{EnterprisePlan, Plan, Plans, Subscription}
+ alias Plausible.Billing.Subscription.Status
+ alias Plausible.Teams.Team
+
+ @free_plan_id "free_10k"
+
+ @doc """
+ Returns a map with the following keys:
+
+ * `:statuses` - the current subscription of every team, by status. Cancelled
+ subscriptions are split into those still inside the paid period and those
+ that expired. `:teams_on_app_trial` are teams in our own trial without
+ any subscription
+ * `:billed` - live subscriptions (active, past due, paused, or cancelled but
+ paid up) by kind, currency and interval: the effective amount paid.
+ * `:vs_list_price` - for regular plans, how the amount paid compares to the
+ list price in `plan_prices.json`, by currency. The number is compared
+ as is, so for EUR and GBP "equal" means parity with the USD price. Anything
+ that isn't equal is a discount, a modifier, a negotiated price or a
+ grandfathered one. `:off_list_price_points` lists the most common ones.
+ * `:enterprise` - live enterprise plans: price points (distinct currency,
+ interval and amount combinations), by interval, and how many plans are
+ shared or repeated.
+
+ Options:
+
+ * `:today` - date for "inside the paid period" (default: today, UTC).
+ * `:list_prices` - map of plan id => USD list price (default:
+ `plan_prices.json` from `priv`).
+ """
+ @spec audit(Keyword.t()) :: map()
+ def audit(opts \\ []) do
+ today = Keyword.get(opts, :today, Date.utc_today())
+ list_prices = Keyword.get_lazy(opts, :list_prices, &read_list_prices/0)
+
+ rows = live_rows(today)
+
+ %{
+ as_of: today,
+ statuses: statuses(today),
+ billed: billed(rows),
+ vs_list_price: vs_list_price(rows, list_prices),
+ enterprise: enterprise(rows)
+ }
+ end
+
+ defp statuses(today) do
+ current =
+ from(s in Subscription,
+ distinct: s.team_id,
+ order_by: [asc: s.team_id, desc: s.inserted_at, desc: s.id],
+ select: {s.paddle_plan_id, s.status, s.next_bill_date}
+ )
+ |> Repo.all()
+
+ by_status =
+ current
+ |> Enum.frequencies_by(&status_label(&1, today))
+ |> Enum.map(fn {status, teams} -> %{status: status, teams: teams} end)
+ |> Enum.sort_by(&{-&1.teams, &1.status})
+
+ %{
+ teams_with_subscription: length(current),
+ by_status: by_status,
+ teams_on_app_trial: teams_on_app_trial(today)
+ }
+ end
+
+ defp status_label({@free_plan_id, _status, _date}, _today), do: "free_10k"
+
+ defp status_label({plan_id, status, date}, today) do
+ cond do
+ plan_id == Subscription.manual_plan_id() ->
+ "manual"
+
+ (status == Status.deleted() and date) && Date.compare(date, today) != :lt ->
+ "cancelled_paid_up"
+
+ status == Status.deleted() ->
+ "cancelled_expired"
+
+ true ->
+ Atom.to_string(status)
+ end
+ end
+
+ defp teams_on_app_trial(today) do
+ from(t in Team,
+ as: :team,
+ where: t.trial_expiry_date >= ^today,
+ where: not exists(from(s in Subscription, where: s.team_id == parent_as(:team).id)),
+ select: count(t.id)
+ )
+ |> Repo.one()
+ end
+
+ # Live, paid subscriptions with the plan resolved the way the app does it.
+ defp live_rows(today) do
+ live = [Status.active(), Status.past_due(), Status.paused()]
+
+ from(s in Subscription,
+ where: s.status in ^live or (s.status == ^Status.deleted() and s.next_bill_date >= ^today),
+ where: s.paddle_plan_id not in [^@free_plan_id, ^Subscription.manual_plan_id()],
+ order_by: [asc: s.id]
+ )
+ |> Repo.all()
+ |> Enum.map(fn subscription ->
+ plan = Plans.get_subscription_plan(subscription)
+
+ %{
+ plan_id: subscription.paddle_plan_id,
+ currency: subscription.currency_code,
+ amount: parse_amount(subscription.next_bill_amount),
+ kind: kind(plan),
+ interval: interval(plan, subscription)
+ }
+ end)
+ end
+
+ defp kind(%EnterprisePlan{}), do: :enterprise
+ defp kind(%Plan{}), do: :regular
+ defp kind(_), do: :unresolved
+
+ defp interval(%EnterprisePlan{billing_interval: interval}, _subscription), do: interval
+
+ defp interval(%Plan{monthly_product_id: id}, %{paddle_plan_id: id}), do: :monthly
+ defp interval(%Plan{}, _subscription), do: :yearly
+ defp interval(_plan, _subscription), do: :unknown
+
+ defp billed(rows) do
+ rows
+ |> Enum.group_by(&{&1.kind, &1.currency, &1.interval})
+ |> Enum.map(fn {{kind, currency, interval}, group} ->
+ amounts = for %{amount: {:ok, amount}} <- group, do: amount
+
+ %{
+ kind: kind,
+ currency: currency,
+ interval: interval,
+ subscriptions: length(group),
+ unparsable_amounts: length(group) - length(amounts)
+ }
+ |> Map.merge(amount_stats(amounts))
+ end)
+ |> Enum.sort_by(&{-&1.subscriptions, &1.kind, &1.currency, &1.interval})
+ end
+
+ defp amount_stats([]), do: %{total: nil, min: nil, max: nil}
+
+ defp amount_stats(amounts) do
+ %{
+ total: amounts |> Enum.reduce(&Decimal.add/2) |> format(),
+ min: amounts |> Enum.reduce(&Decimal.min/2) |> format(),
+ max: amounts |> Enum.reduce(&Decimal.max/2) |> format()
+ }
+ end
+
+ defp vs_list_price(rows, list_prices) do
+ regular = Enum.filter(rows, &(&1.kind == :regular))
+
+ compared =
+ Enum.map(regular, fn row ->
+ {row, compare(row.amount, Map.get(list_prices, row.plan_id))}
+ end)
+
+ by_currency =
+ compared
+ |> Enum.group_by(fn {row, _} -> row.currency end, fn {_, result} -> result end)
+ |> Enum.map(fn {currency, results} ->
+ frequencies = Enum.frequencies(results)
+
+ %{
+ currency: currency,
+ subscriptions: length(results),
+ equal: Map.get(frequencies, :equal, 0),
+ lower: Map.get(frequencies, :lower, 0),
+ higher: Map.get(frequencies, :higher, 0),
+ no_list_price: Map.get(frequencies, :no_list_price, 0),
+ unparsable: Map.get(frequencies, :unparsable, 0)
+ }
+ end)
+ |> Enum.sort_by(&{-&1.subscriptions, &1.currency})
+
+ off_list =
+ for {row, result} <- compared, result in [:lower, :higher], reduce: %{} do
+ acc ->
+ key = {row.plan_id, row.currency, row.interval, format(elem(row.amount, 1)), result}
+ Map.update(acc, key, 1, &(&1 + 1))
+ end
+
+ %{
+ by_currency: by_currency,
+ off_list_price_points:
+ off_list
+ |> Enum.map(fn {{plan_id, currency, interval, amount, result}, count} ->
+ %{
+ plan_id: plan_id,
+ currency: currency,
+ interval: interval,
+ paid: amount,
+ list_price: format(to_decimal(Map.fetch!(list_prices, plan_id))),
+ comparison: result,
+ subscriptions: count
+ }
+ end)
+ |> Enum.sort_by(&{-&1.subscriptions, &1.plan_id})
+ |> Enum.take(20)
+ }
+ end
+
+ defp compare({:ok, _amount}, nil), do: :no_list_price
+ defp compare(:error, _list_price), do: :unparsable
+
+ defp compare({:ok, amount}, list_price) do
+ case Decimal.compare(amount, to_decimal(list_price)) do
+ :eq -> :equal
+ :lt -> :lower
+ :gt -> :higher
+ end
+ end
+
+ defp enterprise(rows) do
+ live = Enum.filter(rows, &(&1.kind == :enterprise))
+
+ price_points =
+ live
+ |> Enum.group_by(fn row ->
+ {row.currency, row.interval, row.amount |> amount_or_nil() |> format()}
+ end)
+ |> Enum.map(fn {{currency, interval, amount}, group} ->
+ %{currency: currency, interval: interval, amount: amount, subscriptions: length(group)}
+ end)
+ |> Enum.sort_by(&{-&1.subscriptions, &1.currency, &1.amount})
+
+ %{
+ live_subscriptions: length(live),
+ distinct_plan_ids: live |> Enum.map(& &1.plan_id) |> Enum.uniq() |> length(),
+ by_interval:
+ live
+ |> Enum.frequencies_by(& &1.interval)
+ |> Enum.map(fn {interval, n} -> %{interval: interval, subscriptions: n} end)
+ |> Enum.sort_by(&{-&1.subscriptions, &1.interval}),
+ distinct_price_points: length(price_points),
+ top_price_points: Enum.take(price_points, 15),
+ plan_ids_shared_by_several_teams: plan_ids_shared_by_several_teams(),
+ teams_with_several_enterprise_plans: teams_with_several_enterprise_plans()
+ }
+ end
+
+ defp subscribed_enterprise_plans() do
+ from(ep in EnterprisePlan,
+ join: s in Subscription,
+ on: s.paddle_plan_id == ep.paddle_plan_id and s.team_id == ep.team_id,
+ distinct: ep.id,
+ select: ep
+ )
+ end
+
+ defp plan_ids_shared_by_several_teams() do
+ from(ep in Ecto.Query.subquery(subscribed_enterprise_plans()),
+ group_by: ep.paddle_plan_id,
+ having: count(ep.team_id, :distinct) > 1,
+ select: ep.paddle_plan_id
+ )
+ |> Repo.all()
+ |> length()
+ end
+
+ defp teams_with_several_enterprise_plans() do
+ from(ep in Ecto.Query.subquery(subscribed_enterprise_plans()),
+ group_by: ep.team_id,
+ having: count(ep.id) > 1,
+ select: ep.team_id
+ )
+ |> Repo.all()
+ |> length()
+ end
+
+ defp read_list_prices() do
+ path = Application.app_dir(:plausible, ["priv", "plan_prices.json"])
+
+ with {:ok, content} <- File.read(path),
+ {:ok, prices} when is_map(prices) <- Jason.decode(content) do
+ prices
+ else
+ _ -> %{}
+ end
+ end
+
+ defp parse_amount(nil), do: :error
+
+ defp parse_amount(string) when is_binary(string) do
+ case Decimal.parse(String.trim(string)) do
+ {amount, ""} -> {:ok, amount}
+ _ -> :error
+ end
+ end
+
+ defp amount_or_nil({:ok, amount}), do: amount
+ defp amount_or_nil(:error), do: nil
+
+ defp to_decimal(number) when is_float(number), do: Decimal.from_float(number)
+ defp to_decimal(number), do: Decimal.new(number)
+
+ defp format(nil), do: nil
+ defp format(decimal), do: decimal |> Decimal.round(2) |> Decimal.to_string(:normal)
+end
diff --git a/lib/plausible/billing/paddle_classic/api.ex b/lib/plausible/billing/paddle_classic/api.ex
index 1a4e1c9ba6..2e2b86bae0 100644
--- a/lib/plausible/billing/paddle_classic/api.ex
+++ b/lib/plausible/billing/paddle_classic/api.ex
@@ -158,6 +158,92 @@ defmodule Plausible.Billing.PaddleClassic.Api do
end
end
+ @plans_page_size 500
+ @modifiers_page_size 250
+
+ @doc """
+ Lists every plan of the Classic catalog (read-only), following pagination.
+
+ Each plan has `"id"` (integer), `"name"`, `"billing_type"` (`"day"`, `"week"`, `"month"`
+ or `"year"`), `"billing_period"` (integer), `"trial_days"` (integer) and
+ `"initial_price"` / `"recurring_price"` maps of currency code => amount string.
+
+ See `POST /2.0/subscription/plans` in Paddle's
+ [Subscription API](https://classic.paddle.com/api-reference/reference/subscription-api).
+ """
+ def list_plans() do
+ list_plans(0, [])
+ end
+
+ defp list_plans(offset, acc) do
+ params =
+ Map.merge(auth_params(), %{limit: @plans_page_size, offset: offset})
+
+ case HTTPClient.impl().post(plans_url(), @headers, params) do
+ {:ok, %{body: %{"success" => true, "response" => plans} = body}} when is_list(plans) ->
+ acc = acc ++ plans
+ total = body["total"] || length(acc)
+
+ if plans != [] and length(acc) < total do
+ list_plans(offset + length(plans), acc)
+ else
+ {:ok, acc}
+ end
+
+ {:ok, %{body: body}} ->
+ {:error, body["error"] || :unexpected_response}
+
+ {:error, %{reason: reason}} ->
+ {:error, reason}
+ end
+ end
+
+ @doc """
+ Lists the modifiers of all subscriptions (read-only), following pagination.
+
+ Each modifier has `"modifier_id"` (integer), `"subscription_id"` (integer), `"amount"`
+ (string, negative for discounts), `"currency"`, `"is_recurring"` (boolean) and `"description"`.
+
+ The endpoint pages with `after`: the id of the last modifier of the previous page.
+
+ See `POST /2.0/subscription/modifiers` in Paddle's
+ [Subscription API](https://classic.paddle.com/api-reference/reference/subscription-api).
+ """
+ def list_modifiers() do
+ list_modifiers(nil, [])
+ end
+
+ defp list_modifiers(after_id, acc) do
+ params =
+ Map.merge(auth_params(), %{results_per_page: @modifiers_page_size})
+
+ params = if after_id, do: Map.put(params, :after, after_id), else: params
+
+ case HTTPClient.impl().post(modifiers_url(), @headers, params) do
+ {:ok, %{body: %{"success" => true, "response" => page}}} when is_list(page) ->
+ # Paddle doesn't say if `after` is inclusive, so never count a modifier twice
+ seen = MapSet.new(acc, & &1["modifier_id"])
+ new = Enum.reject(page, &MapSet.member?(seen, &1["modifier_id"]))
+
+ if new == [] or length(page) < @modifiers_page_size do
+ {:ok, acc ++ new}
+ else
+ list_modifiers(List.last(page)["modifier_id"], acc ++ new)
+ end
+
+ {:ok, %{body: body}} ->
+ {:error, body["error"] || :unexpected_response}
+
+ {:error, %{reason: reason}} ->
+ {:error, reason}
+ end
+ end
+
+ defp auth_params() do
+ config = get_config()
+ %{vendor_id: config[:vendor_id], vendor_auth_code: config[:vendor_auth_code]}
+ end
+
def checkout_domain() do
if Application.get_env(:plausible, :environment) == "staging" do
"https://sandbox-checkout.paddle.com"
@@ -194,6 +280,14 @@ defmodule Plausible.Billing.PaddleClassic.Api do
Path.join(vendors_domain(), "/api/2.0/subscription/users")
end
+ defp plans_url() do
+ Path.join(vendors_domain(), "/api/2.0/subscription/plans")
+ end
+
+ defp modifiers_url() do
+ Path.join(vendors_domain(), "/api/2.0/subscription/modifiers")
+ end
+
defp prices_url() do
Path.join(checkout_domain(), "/api/2.0/prices")
end
diff --git a/test/plausible/billing/migration_audit/classic_catalog_test.exs b/test/plausible/billing/migration_audit/classic_catalog_test.exs
new file mode 100644
index 0000000000..e382039d98
--- /dev/null
+++ b/test/plausible/billing/migration_audit/classic_catalog_test.exs
@@ -0,0 +1,171 @@
+defmodule Plausible.Billing.MigrationAudit.ClassicCatalogTest do
+ use Plausible.DataCase, async: true
+
+ alias Plausible.Billing.MigrationAudit.ClassicCatalog
+ alias Plausible.Billing.Plans
+
+ require Plausible.Billing.Subscription.Status
+ alias Plausible.Billing.Subscription.Status
+
+ @today ~D[2026-10-20]
+
+ defmodule FailingApi do
+ def list_plans(), do: {:error, :boom}
+ def list_modifiers(), do: {:ok, []}
+ end
+
+ defmodule BrokenModifiersApi do
+ def list_plans(), do: {:ok, []}
+ def list_modifiers(), do: {:error, "nope"}
+ end
+
+ defmodule FakeApi do
+ def list_plans(), do: {:ok, [%{"id" => 1, "recurring_price" => %{"USD" => "5.00"}}]}
+ def list_modifiers(), do: {:ok, []}
+ end
+
+ defp static_id(),
+ do: Plans.all() |> Enum.find(& &1.monthly_product_id) |> Map.fetch!(:monthly_product_id)
+
+ defp catalog_plan(id, prices, extra \\ %{}) do
+ Map.merge(
+ %{
+ "id" => id,
+ "name" => "Secret Customer Ltd",
+ "billing_type" => "month",
+ "billing_period" => 1,
+ "initial_price" => %{},
+ "recurring_price" => prices
+ },
+ extra
+ )
+ end
+
+ describe "plans" do
+ test "classifies plans by origin and currencies, without exposing names" do
+ static_id = static_id()
+
+ new_user() |> subscribe_to_plan(static_id)
+ new_user() |> subscribe_to_enterprise_plan(paddle_plan_id: "777001")
+ new_user() |> subscribe_to_plan("no-longer-in-catalog")
+
+ plans = [
+ catalog_plan(String.to_integer(static_id), %{
+ "USD" => "9.00",
+ "EUR" => "9.00",
+ "GBP" => "9.00"
+ }),
+ catalog_plan(777_001, %{"USD" => "100.00"}),
+ catalog_plan(777_002, %{"USD" => "9.00", "EUR" => "8.00"}),
+ catalog_plan(777_003, "not a map", %{"billing_type" => "year"})
+ ]
+
+ assert {:ok, %{plans: result}} =
+ ClassicCatalog.audit(today: @today, plans: plans, modifiers: [])
+
+ assert result.total == 4
+ assert Map.new(result.by_origin) == %{static: 1, enterprise: 1, other: 2}
+ assert Map.new(result.with_live_subscription_by_origin) == %{static: 1, enterprise: 1}
+ assert result.single_currency == 2
+ assert result.multi_currency == 2
+ assert result.multi_currency_parity == 1
+ assert result.multi_currency_differing == 1
+ assert Map.new(result.multi_currency_by_origin) == %{static: 1, other: 1}
+ assert result.without_recurring_price == 1
+ assert result.live_plan_ids_missing_from_catalog == ["no-longer-in-catalog"]
+
+ assert [%{plan_id: "777002", origin: :other, prices: %{"USD" => "9.00", "EUR" => "8.00"}}] =
+ result.differing_examples
+
+ assert %{currencies: ["EUR", "GBP", "USD"], plans: 1} in result.currency_sets
+ assert %{billing_type: "month", billing_period: 1, plans: 3} in result.by_billing_period
+
+ refute inspect(result) =~ "Secret Customer"
+ end
+
+ test "a cancelled subscription still inside the paid period is live, an expired one is not" do
+ id = static_id()
+
+ new_user()
+ |> subscribe_to_plan(id, status: Status.deleted(), next_bill_date: @today)
+
+ plan = catalog_plan(String.to_integer(id), %{"USD" => "9.00"})
+
+ assert {:ok, %{plans: %{with_live_subscription_by_origin: %{static: 1}}}} =
+ ClassicCatalog.audit(today: @today, plans: [plan], modifiers: [])
+
+ assert {:ok, %{plans: %{with_live_subscription_by_origin: %{}}}} =
+ ClassicCatalog.audit(
+ today: Date.shift(@today, day: 1),
+ plans: [plan],
+ modifiers: []
+ )
+ end
+ end
+
+ describe "modifiers" do
+ test "summarises modifiers and the subscriptions they are on" do
+ new_user() |> subscribe_to_plan(static_id(), paddle_subscription_id: "5001")
+ new_user() |> subscribe_to_plan(static_id(), paddle_subscription_id: "5002")
+
+ modifiers = [
+ %{
+ "modifier_id" => 1,
+ "subscription_id" => 5001,
+ "amount" => "-2.00",
+ "currency" => "USD",
+ "is_recurring" => true,
+ "description" => "Customer secret"
+ },
+ %{
+ "modifier_id" => 2,
+ "subscription_id" => 5001,
+ "amount" => "5.00",
+ "currency" => "USD",
+ "is_recurring" => false
+ },
+ %{
+ "modifier_id" => 3,
+ "subscription_id" => 9999,
+ "amount" => "-1.50",
+ "currency" => "EUR",
+ "is_recurring" => true
+ },
+ %{"modifier_id" => 4, "subscription_id" => 5002, "amount" => "0.00", "currency" => "USD"}
+ ]
+
+ assert {:ok, %{modifiers: result}} =
+ ClassicCatalog.audit(today: @today, plans: [], modifiers: modifiers)
+
+ assert result.total == 4
+ assert result.recurring == 2
+ assert result.one_time == 1
+ assert result.recurrence_unknown == 1
+ assert result.subscriptions_with_modifiers == 3
+ assert result.of_which_live == 2
+ assert result.max_modifiers_on_one_subscription == 2
+ assert result.discounts == 2
+ assert result.surcharges == 1
+ assert result.zero_or_unparsable == 1
+
+ assert [
+ %{currency: "USD", modifiers: 3, net_amount: "3.00"},
+ %{currency: "EUR", modifiers: 1, net_amount: "-1.50"}
+ ] =
+ result.by_currency
+
+ refute inspect(result) =~ "secret"
+ end
+ end
+
+ describe "fetching" do
+ test "uses the given api module" do
+ assert {:ok, %{plans: %{total: 1}}} = ClassicCatalog.audit(api: FakeApi, today: @today)
+ end
+
+ test "reports which call failed" do
+ assert {:error, {:plans, :boom}} = ClassicCatalog.audit(api: FailingApi)
+ assert {:error, {:modifiers, "nope"}} = ClassicCatalog.audit(api: BrokenModifiersApi)
+ end
+ end
+end
diff --git a/test/plausible/billing/migration_audit/subscribers_test.exs b/test/plausible/billing/migration_audit/subscribers_test.exs
new file mode 100644
index 0000000000..b00cc80a39
--- /dev/null
+++ b/test/plausible/billing/migration_audit/subscribers_test.exs
@@ -0,0 +1,245 @@
+defmodule Plausible.Billing.MigrationAudit.SubscribersTest do
+ use Plausible.DataCase, async: true
+
+ alias Plausible.Billing.MigrationAudit.Subscribers
+ alias Plausible.Billing.{Plans, Subscription}
+
+ require Plausible.Billing.Subscription.Status
+ alias Plausible.Billing.Subscription.Status
+
+ @today ~D[2026-10-20]
+
+ # a plan with both a monthly and a yearly id
+ defp plan(), do: Enum.find(Plans.all(), &(&1.monthly_product_id && &1.yearly_product_id))
+
+ describe "statuses" do
+ test "counts the current subscription of every team by status" do
+ plan_id = plan().monthly_product_id
+ yesterday = Date.shift(@today, day: -1)
+
+ new_user() |> subscribe_to_plan(plan_id)
+ new_user() |> subscribe_to_plan(plan_id)
+ new_user() |> subscribe_to_plan(plan_id, status: Status.past_due())
+ new_user() |> subscribe_to_plan(plan_id, status: Status.paused())
+ new_user() |> subscribe_to_plan(plan_id, status: Status.deleted(), next_bill_date: @today)
+
+ new_user()
+ |> subscribe_to_plan(plan_id, status: Status.deleted(), next_bill_date: yesterday)
+
+ new_user() |> subscribe_to_plan("free_10k")
+ new_user() |> subscribe_to_plan(Subscription.manual_plan_id())
+
+ assert %{teams_with_subscription: 8, by_status: by_status} =
+ Subscribers.audit(today: @today, list_prices: %{}).statuses
+
+ assert Map.new(by_status, &{&1.status, &1.teams}) == %{
+ "active" => 2,
+ "past_due" => 1,
+ "paused" => 1,
+ "cancelled_paid_up" => 1,
+ "cancelled_expired" => 1,
+ "free_10k" => 1,
+ "manual" => 1
+ }
+ end
+
+ test "only the last subscription of a team counts" do
+ plan_id = plan().monthly_product_id
+ team = new_user() |> subscribe_to_plan(plan_id, status: Status.deleted()) |> team_of()
+ insert(:subscription, team: team, paddle_plan_id: plan_id)
+
+ assert %{by_status: [%{status: "active", teams: 1}]} =
+ Subscribers.audit(today: @today, list_prices: %{}).statuses
+ end
+
+ test "counts teams in the app's own trial that never subscribed" do
+ plan_id = plan().monthly_product_id
+
+ # the test database can hold teams of its own, so compare against a baseline
+ baseline = Subscribers.audit(today: @today, list_prices: %{}).statuses.teams_on_app_trial
+
+ new_user(trial_expiry_date: Date.shift(@today, day: 5))
+ new_user(trial_expiry_date: Date.shift(@today, day: -5))
+
+ new_user(trial_expiry_date: Date.shift(@today, day: 5))
+ |> subscribe_to_plan(plan_id)
+
+ assert Subscribers.audit(today: @today, list_prices: %{}).statuses.teams_on_app_trial ==
+ baseline + 1
+ end
+ end
+
+ describe "billed" do
+ test "groups live paid subscriptions by kind, currency and interval" do
+ plan = plan()
+
+ new_user() |> subscribe_to_plan(plan.monthly_product_id, next_bill_amount: "6.00")
+ new_user() |> subscribe_to_plan(plan.monthly_product_id, next_bill_amount: "10.50")
+
+ new_user()
+ |> subscribe_to_plan(plan.yearly_product_id,
+ next_bill_amount: "60.00",
+ currency_code: "EUR"
+ )
+
+ new_user() |> subscribe_to_enterprise_plan(billing_interval: :yearly)
+
+ new_user()
+ |> subscribe_to_plan(plan.monthly_product_id, next_bill_amount: "oops")
+
+ # not live, free and manual ones are left out
+ new_user()
+ |> subscribe_to_plan(plan.monthly_product_id,
+ status: Status.deleted(),
+ next_bill_date: Date.shift(@today, day: -1)
+ )
+
+ new_user() |> subscribe_to_plan("free_10k")
+ new_user() |> subscribe_to_plan(Subscription.manual_plan_id())
+
+ billed = Subscribers.audit(today: @today, list_prices: %{}).billed
+
+ assert [
+ %{
+ kind: :regular,
+ currency: "USD",
+ interval: :monthly,
+ subscriptions: 3,
+ unparsable_amounts: 1,
+ total: "16.50",
+ min: "6.00",
+ max: "10.50"
+ }
+ ] = Enum.filter(billed, &(&1.interval == :monthly and &1.kind == :regular))
+
+ assert Enum.any?(billed, &match?(%{kind: :regular, currency: "EUR", interval: :yearly}, &1))
+ assert Enum.any?(billed, &match?(%{kind: :enterprise, interval: :yearly}, &1))
+ assert Enum.sum(Enum.map(billed, & &1.subscriptions)) == 5
+ end
+
+ test "an unknown plan id is reported as unresolved" do
+ new_user() |> subscribe_to_plan("no-such-plan")
+
+ assert [%{kind: :unresolved, interval: :unknown, subscriptions: 1}] =
+ Subscribers.audit(today: @today, list_prices: %{}).billed
+ end
+ end
+
+ describe "vs_list_price" do
+ test "compares what is paid to the USD list price" do
+ id = plan().monthly_product_id
+
+ new_user() |> subscribe_to_plan(id, next_bill_amount: "10.00")
+ new_user() |> subscribe_to_plan(id, next_bill_amount: "10.00", currency_code: "EUR")
+ new_user() |> subscribe_to_plan(id, next_bill_amount: "8.00")
+ new_user() |> subscribe_to_plan(id, next_bill_amount: "8.00")
+ new_user() |> subscribe_to_plan(id, next_bill_amount: "12.00")
+ new_user() |> subscribe_to_plan(id, next_bill_amount: "x")
+ new_user() |> subscribe_to_enterprise_plan()
+
+ result = Subscribers.audit(today: @today, list_prices: %{id => 10}).vs_list_price
+
+ assert [usd, eur] = result.by_currency
+
+ assert %{currency: "USD", subscriptions: 5, equal: 1, lower: 2, higher: 1, unparsable: 1} =
+ usd
+
+ assert %{currency: "EUR", subscriptions: 1, equal: 1, lower: 0, higher: 0} = eur
+
+ assert [
+ %{paid: "8.00", list_price: "10.00", comparison: :lower, subscriptions: 2},
+ %{paid: "12.00", comparison: :higher, subscriptions: 1}
+ ] = result.off_list_price_points
+ end
+
+ test "plans without a list price are counted separately" do
+ new_user() |> subscribe_to_plan(plan().monthly_product_id)
+
+ assert [%{no_list_price: 1, equal: 0}] =
+ Subscribers.audit(today: @today, list_prices: %{}).vs_list_price.by_currency
+ end
+ end
+
+ describe "enterprise" do
+ test "summarises price points of live enterprise plans" do
+ new_user() |> subscribe_to_enterprise_plan(subscription: [next_bill_amount: "100.00"])
+ new_user() |> subscribe_to_enterprise_plan(subscription: [next_bill_amount: "100.00"])
+ new_user() |> subscribe_to_enterprise_plan(subscription: [next_bill_amount: "250.00"])
+
+ new_user()
+ |> subscribe_to_enterprise_plan(
+ billing_interval: :yearly,
+ subscription: [next_bill_amount: "900.00"]
+ )
+
+ # not live
+ new_user()
+ |> subscribe_to_enterprise_plan(
+ subscription: [status: Status.deleted(), next_bill_date: Date.shift(@today, day: -1)]
+ )
+
+ enterprise = Subscribers.audit(today: @today, list_prices: %{}).enterprise
+
+ assert %{live_subscriptions: 4, distinct_plan_ids: 4, distinct_price_points: 3} = enterprise
+
+ assert [%{interval: :monthly, subscriptions: 3}, %{interval: :yearly, subscriptions: 1}] =
+ enterprise.by_interval
+
+ assert [
+ %{amount: "100.00", interval: :monthly, subscriptions: 2},
+ %{subscriptions: 1},
+ %{subscriptions: 1}
+ ] = enterprise.top_price_points
+ end
+
+ test "reports plan ids shared by several teams and teams with several plans" do
+ shared = "shared-plan"
+ new_user() |> subscribe_to_enterprise_plan(paddle_plan_id: shared)
+ new_user() |> subscribe_to_enterprise_plan(paddle_plan_id: shared)
+
+ user = new_user() |> subscribe_to_enterprise_plan()
+ team = team_of(user)
+ insert(:enterprise_plan, team: team, paddle_plan_id: "another")
+ insert(:subscription, team: team, paddle_plan_id: "another")
+
+ enterprise = Subscribers.audit(today: @today, list_prices: %{}).enterprise
+
+ assert enterprise.plan_ids_shared_by_several_teams == 1
+ assert enterprise.teams_with_several_enterprise_plans == 1
+ end
+
+ test "enterprise plans without a matching subscription are not counted" do
+ shared = "shared-plan"
+ new_user() |> subscribe_to_enterprise_plan(paddle_plan_id: shared)
+
+ # same plan id, but this team has no subscription for it
+ other = new_user()
+ {:ok, other_team} = Plausible.Teams.get_or_create(other)
+ insert(:enterprise_plan, team: other_team, paddle_plan_id: shared)
+
+ user = new_user() |> subscribe_to_enterprise_plan()
+ insert(:enterprise_plan, team: team_of(user), paddle_plan_id: "not-subscribed-yet")
+
+ enterprise = Subscribers.audit(today: @today, list_prices: %{}).enterprise
+
+ assert enterprise.plan_ids_shared_by_several_teams == 0
+ assert enterprise.teams_with_several_enterprise_plans == 0
+ end
+ end
+
+ test "an empty database gives an empty audit" do
+ result = Subscribers.audit(today: @today, list_prices: %{})
+
+ assert result.billed == []
+ assert %{teams_with_subscription: 0, by_status: []} = result.statuses
+ assert result.enterprise.live_subscriptions == 0
+ assert result.vs_list_price == %{by_currency: [], off_list_price_points: []}
+ end
+
+ test "reads the list prices from the priv directory by default" do
+ id = plan().monthly_product_id
+ new_user() |> subscribe_to_plan(id)
+
+ assert [%{no_list_price: 0, subscriptions: 1}] = Subscribers.audit().vs_list_price.by_currency
+ end
+end
diff --git a/test/plausible/billing/migration_audit_test.exs b/test/plausible/billing/migration_audit_test.exs
new file mode 100644
index 0000000000..9aaffbf18b
--- /dev/null
+++ b/test/plausible/billing/migration_audit_test.exs
@@ -0,0 +1,346 @@
+defmodule Plausible.Billing.MigrationAuditTest do
+ use Plausible.DataCase, async: true
+
+ alias Plausible.Billing.MigrationAudit
+ alias Plausible.Billing.Subscription
+
+ require Plausible.Billing.Subscription.Status
+ alias Plausible.Billing.Subscription.Status
+
+ @v1_growth_plan_id "558018"
+ @v5_starter_plan_id "910413"
+
+ describe "plan_resolution/1" do
+ test "classifies every kind of live subscription" do
+ new_user() |> subscribe_to_plan(@v1_growth_plan_id)
+ new_user() |> subscribe_to_plan(@v5_starter_plan_id)
+ new_user() |> subscribe_to_plan("free_10k")
+ new_user() |> subscribe_to_plan(Subscription.manual_plan_id())
+ new_user() |> subscribe_to_enterprise_plan()
+ unresolved_user = new_user() |> subscribe_to_plan("some-unknown-plan")
+
+ result = MigrationAudit.plan_resolution()
+
+ assert result.total == 6
+
+ assert Map.new(result.by_category, &{&1.category, &1.subscriptions}) == %{
+ "v1 growth" => 1,
+ "v5 starter" => 1,
+ "free" => 1,
+ "manual" => 1,
+ "enterprise" => 1,
+ "unresolved" => 1
+ }
+
+ unresolved_team = team_of(unresolved_user)
+
+ assert [
+ %{
+ team_id: team_id,
+ paddle_plan_id: "some-unknown-plan",
+ status: :active
+ }
+ ] = result.unresolved
+
+ assert team_id == unresolved_team.id
+ end
+
+ test "counts a subscription whose paid period is over only when it isn't expired" do
+ today = ~D[2026-10-20]
+
+ expired = [status: Status.deleted(), next_bill_date: Date.shift(today, day: -1)]
+ paid_up = [status: Status.deleted(), next_bill_date: today]
+
+ new_user() |> subscribe_to_plan(@v1_growth_plan_id, expired)
+ new_user() |> subscribe_to_plan(@v1_growth_plan_id, paid_up)
+ new_user() |> subscribe_to_plan(@v1_growth_plan_id, status: Status.paused())
+ new_user() |> subscribe_to_plan(@v1_growth_plan_id, status: Status.past_due())
+
+ result = MigrationAudit.plan_resolution(today: today)
+
+ assert result.total == 3
+ assert Map.new(result.by_category, &{&1.category, &1.subscriptions}) == %{"v1 growth" => 3}
+ end
+
+ test "the :current scope audits only the last subscription of each team" do
+ user = new_user()
+ team = user |> subscribe_to_plan(@v1_growth_plan_id) |> team_of()
+ insert(:subscription, team: team, paddle_plan_id: @v5_starter_plan_id)
+
+ result = MigrationAudit.plan_resolution(scope: :current)
+
+ assert result.total == 1
+ assert Map.new(result.by_category, &{&1.category, &1.subscriptions}) == %{"v5 starter" => 1}
+ assert result.teams_with_multiple_subscriptions == %{any_status: 1, live: 1}
+ end
+
+ test "reports teams with more than one live subscription separately" do
+ team = new_user() |> subscribe_to_plan(@v1_growth_plan_id) |> team_of()
+
+ insert(:subscription,
+ team: team,
+ paddle_plan_id: @v5_starter_plan_id,
+ status: Status.deleted(),
+ next_bill_date: ~D[2020-01-01]
+ )
+
+ result = MigrationAudit.plan_resolution()
+
+ assert result.teams_with_multiple_subscriptions == %{any_status: 1, live: 0}
+ end
+
+ test "summarises generations and plan ids in use" do
+ new_user() |> subscribe_to_plan(@v1_growth_plan_id)
+ new_user() |> subscribe_to_plan(@v1_growth_plan_id)
+
+ result = MigrationAudit.plan_resolution()
+
+ assert [%{subscriptions: 2, distinct_plan_ids_in_use: 1, plan_ids_in_files: files}] =
+ Enum.filter(result.by_generation, &(&1.generation == 1))
+
+ assert files > 0
+
+ assert [%{paddle_plan_id: @v1_growth_plan_id, category: "v1 growth", subscriptions: 2}] =
+ result.by_plan_id
+ end
+
+ test "summarises enterprise plans by their subscriptions" do
+ new_user() |> subscribe_to_enterprise_plan()
+
+ new_user()
+ |> subscribe_to_enterprise_plan(
+ subscription: [status: Status.deleted(), next_bill_date: ~D[2020-01-01]]
+ )
+
+ new_user() |> subscribe_to_enterprise_plan(subscription?: false)
+
+ result = MigrationAudit.plan_resolution()
+
+ assert result.enterprise_plans == %{
+ total: 3,
+ attached_to_live_subscription: 1,
+ attached_to_any_subscription: 2
+ }
+ end
+
+ test "an empty database gives an empty report" do
+ result = MigrationAudit.plan_resolution()
+
+ assert result.total == 0
+ assert result.unresolved == []
+ assert result.by_category == []
+ end
+ end
+
+ describe "run/1" do
+ test "prints both reports and returns the data" do
+ new_user() |> subscribe_to_plan("some-unknown-plan")
+
+ output =
+ ExUnit.CaptureIO.capture_io(fn ->
+ assert %{plan_resolution: %{total: 1}, plan_files: plan_files} = MigrationAudit.run()
+ assert is_list(plan_files)
+ end)
+
+ assert output =~ "UNRESOLVED subscriptions (1)"
+ assert output =~ "Plan files:"
+ assert output =~ "Subscribers (as of"
+ assert output =~ "Classic catalog and modifiers skipped"
+ end
+
+ test "with catalog: true it also audits the Classic catalog" do
+ plans = [%{"id" => 1, "recurring_price" => %{"USD" => "9.00", "EUR" => "9.00"}}]
+
+ output =
+ ExUnit.CaptureIO.capture_io(fn ->
+ assert %{classic_catalog: %{plans: %{total: 1}, modifiers: %{total: 0}}} =
+ MigrationAudit.run(catalog: true, plans: plans, modifiers: [])
+ end)
+
+ assert output =~ "Classic catalog: 1 plans"
+ assert output =~ "Modifiers: 0"
+ end
+
+ test "reports a failing Classic call without raising" do
+ defmodule BrokenApi do
+ def list_plans(), do: {:error, :timeout}
+ def list_modifiers(), do: {:ok, []}
+ end
+
+ output =
+ ExUnit.CaptureIO.capture_io(fn ->
+ assert %{classic_catalog: %{error: %{failed_call: :plans, reason: ":timeout"}}} =
+ MigrationAudit.run(catalog: true, api: BrokenApi)
+ end)
+
+ assert output =~ "failed to fetch plans: :timeout"
+ end
+ end
+
+ describe "json_report/1" do
+ test "runs everything including the catalog and returns shareable JSON" do
+ new_user() |> subscribe_to_plan("some-unknown-plan")
+ plans = [%{"id" => 1, "recurring_price" => %{"USD" => "9.00", "EUR" => "9.00"}}]
+
+ output =
+ ExUnit.CaptureIO.capture_io(fn ->
+ json = MigrationAudit.json_report(plans: plans, modifiers: [])
+ send(self(), {:json, json})
+ end)
+
+ assert output == ""
+ assert_received {:json, json}
+
+ decoded = Jason.decode!(json)
+
+ assert decoded["classic_catalog"]["plans"]["total"] == 1
+ assert decoded["classic_catalog"]["plans"]["multi_currency"] == 1
+ assert decoded["plan_resolution"]["unresolved"]["count"] == 1
+ assert is_list(decoded["subscribers"]["billed"])
+
+ refute json =~ "team_id"
+ refute json =~ "subscription_id"
+ end
+
+ test "reports a failing Classic call in the JSON instead of raising" do
+ defmodule JsonBrokenApi do
+ def list_plans(), do: {:error, :timeout}
+ def list_modifiers(), do: {:ok, []}
+ end
+
+ decoded = MigrationAudit.json_report(api: JsonBrokenApi) |> Jason.decode!()
+
+ assert decoded["classic_catalog"] == %{
+ "error" => %{"failed_call" => "plans", "reason" => ":timeout"}
+ }
+
+ assert is_map(decoded["plan_resolution"])
+ end
+ end
+
+ describe "to_json/1" do
+ test "encodes results with dates, atoms, tuples and decimals" do
+ new_user() |> subscribe_to_plan(@v1_growth_plan_id)
+ new_user() |> subscribe_to_plan("some-unknown-plan")
+
+ {result, _output} =
+ ExUnit.CaptureIO.with_io(fn ->
+ MigrationAudit.run(catalog: true, plans: [], modifiers: [])
+ end)
+
+ decoded = result |> MigrationAudit.to_json() |> Jason.decode!()
+
+ assert decoded["plan_resolution"]["total"] == 2
+
+ assert Enum.sort(decoded["plan_resolution"]["by_category"]) == [
+ %{"category" => "unresolved", "subscriptions" => 1},
+ %{"category" => "v1 growth", "subscriptions" => 1}
+ ]
+
+ assert is_binary(decoded["subscribers"]["as_of"])
+ assert decoded["subscribers"]["billed"] |> is_list()
+ end
+ end
+
+ describe "plan_files_integrity/1" do
+ test "the plan files in priv are consistent" do
+ assert MigrationAudit.plan_files_integrity() == []
+ end
+
+ test "reports files that can't be read" do
+ dir = System.tmp_dir!() |> Path.join("audit_test_#{System.unique_integer([:positive])}")
+ File.mkdir_p!(dir)
+ on_exit(fn -> File.rm_rf!(dir) end)
+
+ File.write!(Path.join(dir, "plans_v1.json"), "not json")
+
+ problems = MigrationAudit.plan_files_integrity(priv_dir: dir)
+
+ assert Enum.any?(problems, &(&1.check == :unreadable and &1.file == "plans_v1"))
+ assert Enum.any?(problems, &(&1.check == :unreadable and &1.file == "plans_v2"))
+ assert Enum.any?(problems, &(&1.check == :unreadable and &1.file == "plan_prices.json"))
+ end
+ end
+
+ describe "check_plan_files/2" do
+ test "accepts consistent files" do
+ assert MigrationAudit.check_plan_files(valid_files(), valid_prices()) == []
+ end
+
+ test "flags a generation that doesn't match the file" do
+ files = put_in(valid_files(), ["plans_v1", Access.at(0), "generation"], 2)
+ assert :generation_mismatch in checks(files, valid_prices())
+ end
+
+ test "flags ids that aren't numeric" do
+ files = put_in(valid_files(), ["plans_v1", Access.at(0), "monthly_product_id"], "pri_123")
+ assert :bad_id_format in checks(files, valid_prices())
+ end
+
+ test "flags a plan that uses one id for both intervals" do
+ files = put_in(valid_files(), ["plans_v1", Access.at(0), "yearly_product_id"], "1")
+ assert :same_monthly_and_yearly_id in checks(files, valid_prices())
+ end
+
+ test "flags an id used by two plans" do
+ files = put_in(valid_files(), ["plans_v1", Access.at(1), "monthly_product_id"], "1")
+
+ problems = MigrationAudit.check_plan_files(files, valid_prices())
+
+ assert [%{check: :duplicate_id, message: message}] =
+ Enum.filter(problems, &(&1.check == :duplicate_id))
+
+ assert message =~ "plans_v1 monthly_product_id"
+ end
+
+ test "flags two plans with the same kind and pageview limit" do
+ files = put_in(valid_files(), ["plans_v1", Access.at(1), "monthly_pageview_limit"], 10_000)
+
+ assert :duplicate_plan in checks(files, valid_prices())
+ end
+
+ test "flags missing, orphaned and invalid prices" do
+ prices =
+ valid_prices()
+ |> Map.delete("1")
+ |> Map.put("999", 5)
+ |> Map.put("2", 0)
+
+ assert checks(valid_files(), prices) == [:bad_price, :missing_price, :orphan_price]
+ end
+
+ test "reports unreadable files and prices" do
+ files = Map.put(valid_files(), "plans_v2", {:error, "plans_v2.json: no such file"})
+
+ problems =
+ MigrationAudit.check_plan_files(files, {:error, "plan_prices.json: no such file"})
+
+ assert Enum.map(problems, & &1.check) == [:unreadable, :unreadable]
+ end
+ end
+
+ defp checks(files, prices) do
+ files |> MigrationAudit.check_plan_files(prices) |> Enum.map(& &1.check) |> Enum.sort()
+ end
+
+ defp plan(limit, monthly, yearly) do
+ %{
+ "kind" => "growth",
+ "generation" => 1,
+ "monthly_pageview_limit" => limit,
+ "monthly_product_id" => monthly,
+ "yearly_product_id" => yearly,
+ "site_limit" => 50,
+ "team_member_limit" => "unlimited",
+ "features" => ["goals"]
+ }
+ end
+
+ defp valid_files() do
+ %{
+ "plans_v1" => [plan(10_000, "1", "2"), plan(100_000, "3", "4")]
+ }
+ end
+
+ defp valid_prices(), do: %{"1" => 6, "2" => 60, "3" => 12, "4" => 120}
+end
diff --git a/test/plausible/billing/paddle_classic/api_test.exs b/test/plausible/billing/paddle_classic/api_test.exs
index 4532f1875b..071a3ede39 100644
--- a/test/plausible/billing/paddle_classic/api_test.exs
+++ b/test/plausible/billing/paddle_classic/api_test.exs
@@ -2,6 +2,7 @@ defmodule Plausible.Billing.PaddleClassic.ApiTest do
use Plausible.DataCase
import Mox
+ alias Plausible.Billing.PaddleClassic.Api
setup :verify_on_exit!
@success "fixture/paddle_prices_success_response.json" |> File.read!() |> Jason.decode!()
@@ -25,7 +26,7 @@ defmodule Plausible.Billing.PaddleClassic.ApiTest do
end
)
- assert Plausible.Billing.PaddleClassic.Api.fetch_prices(
+ assert Api.fetch_prices(
["19878", "20127", "20657", "20658"],
"127.0.0.1"
) ==
@@ -38,4 +39,121 @@ defmodule Plausible.Billing.PaddleClassic.ApiTest do
}}
end
end
+
+ describe "list_plans/0" do
+ test "follows pagination until all plans are fetched" do
+ expect(Plausible.HTTPClient.Mock, :post, 2, fn
+ "https://vendors.paddle.com/api/2.0/subscription/plans",
+ _headers,
+ %{offset: 0} = params ->
+ assert params.limit == 500
+
+ {:ok,
+ ok_response(%{
+ "success" => true,
+ "count" => 2,
+ "total" => 3,
+ "response" => [%{"id" => 1}, %{"id" => 2}]
+ })}
+
+ "https://vendors.paddle.com/api/2.0/subscription/plans", _headers, %{offset: 2} ->
+ {:ok,
+ ok_response(%{
+ "success" => true,
+ "count" => 1,
+ "total" => 3,
+ "response" => [%{"id" => 3}]
+ })}
+ end)
+
+ assert {:ok, [%{"id" => 1}, %{"id" => 2}, %{"id" => 3}]} =
+ Api.list_plans()
+ end
+
+ test "stops on an empty page instead of looping" do
+ expect(Plausible.HTTPClient.Mock, :post, fn _url, _headers, _params ->
+ {:ok, ok_response(%{"success" => true, "count" => 0, "total" => 5, "response" => []})}
+ end)
+
+ assert {:ok, []} = Api.list_plans()
+ end
+
+ test "returns Paddle's error" do
+ expect(Plausible.HTTPClient.Mock, :post, fn _url, _headers, _params ->
+ {:ok,
+ ok_response(%{"success" => false, "error" => %{"code" => 100, "message" => "bad auth"}})}
+ end)
+
+ assert {:error, %{"code" => 100}} = Api.list_plans()
+ end
+
+ test "returns transport errors" do
+ expect(Plausible.HTTPClient.Mock, :post, fn _url, _headers, _params ->
+ {:error, %Mint.TransportError{reason: :timeout}}
+ end)
+
+ assert {:error, :timeout} = Api.list_plans()
+ end
+ end
+
+ describe "list_modifiers/0 pagination" do
+ test "pages with `after` until a page is short" do
+ full_page = for id <- 1..250, do: %{"modifier_id" => id}
+
+ expect(Plausible.HTTPClient.Mock, :post, 2, fn
+ _url, _headers, %{results_per_page: 250} = params when not is_map_key(params, :after) ->
+ {:ok, ok_response(%{"success" => true, "response" => full_page})}
+
+ _url, _headers, %{results_per_page: 250, after: 250} ->
+ {:ok, ok_response(%{"success" => true, "response" => [%{"modifier_id" => 251}]})}
+ end)
+
+ assert {:ok, modifiers} = Api.list_modifiers()
+ assert length(modifiers) == 251
+ assert List.last(modifiers) == %{"modifier_id" => 251}
+ end
+
+ test "never counts a modifier twice if `after` turns out to be inclusive" do
+ full_page = for id <- 1..250, do: %{"modifier_id" => id}
+
+ expect(Plausible.HTTPClient.Mock, :post, 2, fn
+ _url, _headers, %{results_per_page: 250} = params when not is_map_key(params, :after) ->
+ {:ok, ok_response(%{"success" => true, "response" => full_page})}
+
+ _url, _headers, %{after: 250} ->
+ # the last modifier of the previous page comes back first
+ {:ok, ok_response(%{"success" => true, "response" => [%{"modifier_id" => 250}]})}
+ end)
+
+ assert {:ok, modifiers} = Api.list_modifiers()
+ assert length(modifiers) == 250
+ end
+ end
+
+ describe "list_modifiers/0" do
+ test "lists the modifiers" do
+ expect(
+ Plausible.HTTPClient.Mock,
+ :post,
+ fn "https://vendors.paddle.com/api/2.0/subscription/modifiers", _headers, params ->
+ refute Map.has_key?(params, :subscription_id)
+ {:ok, ok_response(%{"success" => true, "response" => [%{"modifier_id" => 1}]})}
+ end
+ )
+
+ assert {:ok, [%{"modifier_id" => 1}]} = Api.list_modifiers()
+ end
+
+ test "returns Paddle's error" do
+ expect(Plausible.HTTPClient.Mock, :post, fn _url, _headers, _params ->
+ {:ok, ok_response(%{"success" => false, "error" => "nope"})}
+ end)
+
+ assert {:error, "nope"} = Api.list_modifiers()
+ end
+ end
+
+ defp ok_response(body) do
+ %Finch.Response{status: 200, headers: [{"content-type", "application/json"}], body: body}
+ end
end