Skip to content

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

Fix passthrough processing billing callback (#5006)

Adrian Gruntkowski4f9825987455
diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex
index 8a5df0b336..245d78f53f 100644
--- a/lib/plausible/billing/billing.ex
+++ b/lib/plausible/billing/billing.ex
@@ -2,6 +2,7 @@ defmodule Plausible.Billing do
use Plausible
use Plausible.Repo
require Plausible.Billing.Subscription.Status
+ alias Plausible.Auth
alias Plausible.Billing.Subscription
alias Plausible.Teams
@@ -132,9 +133,15 @@ defmodule Plausible.Billing do
end
defp get_team!(%{"passthrough" => passthrough}) do
- passthrough
- |> parse_passthrough!()
- |> Teams.get!()
+ case parse_passthrough!(passthrough) do
+ {:team_id, team_id} ->
+ Teams.get!(team_id)
+
+ {:user_id, user_id} ->
+ user = Repo.get!(Auth.User, user_id)
+ {:ok, team} = Teams.get_or_create(user)
+ team
+ end
end
defp get_team!(_params) do
@@ -142,18 +149,24 @@ defmodule Plausible.Billing do
end
defp parse_passthrough!(passthrough) do
- team_id =
+ {user_id, team_id} =
case String.split(to_string(passthrough), ";") do
- ["ee:true", "user:" <> _user_id, "team:" <> team_id] ->
- team_id
+ ["ee:true", "user:" <> user_id, "team:" <> team_id] ->
+ {user_id, team_id}
+
+ ["ee:true", "user:" <> user_id] ->
+ {user_id, "0"}
_ ->
raise "Invalid passthrough sent via Paddle: #{inspect(passthrough)}"
end
- case Integer.parse(team_id) do
- {team_id, ""} when team_id > 0 ->
- team_id
+ case {Integer.parse(user_id), Integer.parse(team_id)} do
+ {{user_id, ""}, {0, ""}} when user_id > 0 ->
+ {:user_id, user_id}
+
+ {{_user_id, ""}, {team_id, ""}} when team_id > 0 ->
+ {:team_id, team_id}
_ ->
raise "Invalid passthrough sent via Paddle: #{inspect(passthrough)}"
diff --git a/test/plausible/billing/billing_test.exs b/test/plausible/billing/billing_test.exs
index 74e6f5c59d..5cc1da0162 100644
--- a/test/plausible/billing/billing_test.exs
+++ b/test/plausible/billing/billing_test.exs
@@ -172,7 +172,7 @@ defmodule Plausible.BillingTest do
user = new_user()
Repo.delete!(user)
- assert_raise RuntimeError, ~r/Invalid passthrough sent via Paddle/, fn ->
+ assert_raise Ecto.NoResultsError, fn ->
%{@subscription_created_params | "passthrough" => "ee:true;user:#{user.id}"}
|> Billing.subscription_created()
end
@@ -209,6 +209,22 @@ defmodule Plausible.BillingTest do
assert subscription.currency_code == "EUR"
end
+ test "supports user without a team case" do
+ user = new_user()
+
+ %{@subscription_created_params | "passthrough" => "ee:true;user:#{user.id}"}
+ |> Billing.subscription_created()
+
+ subscription =
+ user |> team_of() |> Plausible.Teams.with_subscription() |> Map.fetch!(:subscription)
+
+ assert subscription.paddle_subscription_id == @subscription_id
+ assert subscription.next_bill_date == ~D[2019-06-01]
+ assert subscription.last_bill_date == ~D[2019-05-01]
+ assert subscription.next_bill_amount == "6.00"
+ assert subscription.currency_code == "EUR"
+ end
+
test "unlocks sites if user has any locked sites" do
user = new_user()
site = new_site(owner: user, locked: true)
diff --git a/test/plausible_web/live/sites_test.exs b/test/plausible_web/live/sites_test.exs
index a472a0183d..2aacfd45f2 100644
--- a/test/plausible_web/live/sites_test.exs
+++ b/test/plausible_web/live/sites_test.exs
@@ -151,7 +151,7 @@ defmodule PlausibleWeb.Live.SitesTest do
{:ok, lv, _html} = live(conn, "/sites")
- type_into_input(lv, "filter_text", "first")
+ type_into_input(lv, "filter_text", "firs")
html = render(lv)
assert html =~ "first.example.com"