Skip to content

Commit 4f98259

Browse files
authored
Fix passthrough processing billing callback (#5006)
* Revert "Remove support for legacy Paddle webhook passthrough formats (#4939)" This reverts commit 48bd2fb. * Drop support for legacy passthrough formats _but_ leave new user only one
1 parent 48bd2fb commit 4f98259

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

lib/plausible/billing/billing.ex

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule Plausible.Billing do
22
use Plausible
33
use Plausible.Repo
44
require Plausible.Billing.Subscription.Status
5+
alias Plausible.Auth
56
alias Plausible.Billing.Subscription
67
alias Plausible.Teams
78

@@ -132,28 +133,40 @@ defmodule Plausible.Billing do
132133
end
133134

134135
defp get_team!(%{"passthrough" => passthrough}) do
135-
passthrough
136-
|> parse_passthrough!()
137-
|> Teams.get!()
136+
case parse_passthrough!(passthrough) do
137+
{:team_id, team_id} ->
138+
Teams.get!(team_id)
139+
140+
{:user_id, user_id} ->
141+
user = Repo.get!(Auth.User, user_id)
142+
{:ok, team} = Teams.get_or_create(user)
143+
team
144+
end
138145
end
139146

140147
defp get_team!(_params) do
141148
raise "Missing passthrough"
142149
end
143150

144151
defp parse_passthrough!(passthrough) do
145-
team_id =
152+
{user_id, team_id} =
146153
case String.split(to_string(passthrough), ";") do
147-
["ee:true", "user:" <> _user_id, "team:" <> team_id] ->
148-
team_id
154+
["ee:true", "user:" <> user_id, "team:" <> team_id] ->
155+
{user_id, team_id}
156+
157+
["ee:true", "user:" <> user_id] ->
158+
{user_id, "0"}
149159

150160
_ ->
151161
raise "Invalid passthrough sent via Paddle: #{inspect(passthrough)}"
152162
end
153163

154-
case Integer.parse(team_id) do
155-
{team_id, ""} when team_id > 0 ->
156-
team_id
164+
case {Integer.parse(user_id), Integer.parse(team_id)} do
165+
{{user_id, ""}, {0, ""}} when user_id > 0 ->
166+
{:user_id, user_id}
167+
168+
{{_user_id, ""}, {team_id, ""}} when team_id > 0 ->
169+
{:team_id, team_id}
157170

158171
_ ->
159172
raise "Invalid passthrough sent via Paddle: #{inspect(passthrough)}"

test/plausible/billing/billing_test.exs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ defmodule Plausible.BillingTest do
172172
user = new_user()
173173
Repo.delete!(user)
174174

175-
assert_raise RuntimeError, ~r/Invalid passthrough sent via Paddle/, fn ->
175+
assert_raise Ecto.NoResultsError, fn ->
176176
%{@subscription_created_params | "passthrough" => "ee:true;user:#{user.id}"}
177177
|> Billing.subscription_created()
178178
end
@@ -209,6 +209,22 @@ defmodule Plausible.BillingTest do
209209
assert subscription.currency_code == "EUR"
210210
end
211211

212+
test "supports user without a team case" do
213+
user = new_user()
214+
215+
%{@subscription_created_params | "passthrough" => "ee:true;user:#{user.id}"}
216+
|> Billing.subscription_created()
217+
218+
subscription =
219+
user |> team_of() |> Plausible.Teams.with_subscription() |> Map.fetch!(:subscription)
220+
221+
assert subscription.paddle_subscription_id == @subscription_id
222+
assert subscription.next_bill_date == ~D[2019-06-01]
223+
assert subscription.last_bill_date == ~D[2019-05-01]
224+
assert subscription.next_bill_amount == "6.00"
225+
assert subscription.currency_code == "EUR"
226+
end
227+
212228
test "unlocks sites if user has any locked sites" do
213229
user = new_user()
214230
site = new_site(owner: user, locked: true)

test/plausible_web/live/sites_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ defmodule PlausibleWeb.Live.SitesTest do
151151

152152
{:ok, lv, _html} = live(conn, "/sites")
153153

154-
type_into_input(lv, "filter_text", "first")
154+
type_into_input(lv, "filter_text", "firs")
155155
html = render(lv)
156156

157157
assert html =~ "first.example.com"

0 commit comments

Comments
 (0)