Skip to content

Commit

Permalink
Fix passthrough processing billing callback (plausible#5006)
Browse files Browse the repository at this point in the history
* Revert "Remove support for legacy Paddle webhook passthrough formats (plausible#4939)"

This reverts commit 48bd2fb.

* Drop support for legacy passthrough formats _but_ leave new user only one
  • Loading branch information
zoldar authored Jan 22, 2025
1 parent 48bd2fb commit 4f98259
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
31 changes: 22 additions & 9 deletions lib/plausible/billing/billing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -132,28 +133,40 @@ 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
raise "Missing passthrough"
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)}"
Expand Down
18 changes: 17 additions & 1 deletion test/plausible/billing/billing_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/plausible_web/live/sites_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 4f98259

Please sign in to comment.