Skip to content

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

ignore irrelevant paddle webhook (#4240)

RobertJoonasb5c5ab8e61f9
diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex
index 10488656ad..0bb29e5bcc 100644
--- a/lib/plausible/billing/billing.ex
+++ b/lib/plausible/billing/billing.ex
@@ -137,7 +137,22 @@ defmodule Plausible.Billing do
defp handle_subscription_updated(params) do
subscription = Repo.get_by(Subscription, paddle_subscription_id: params["subscription_id"])
- if subscription do
+ # In a situation where the subscription is paused and a payment succeeds, we
+ # get notified of two "subscription_updated" webhook alerts from Paddle at the
+ # same time.
+ #
+ # * one with an `old_status` of "paused", and a `status` of "past_due"
+ # * the other with an `old_status` of "past_due", and a `status` of "active"
+ #
+ # https://developer.paddle.com/classic/guides/zg9joji1mzu0mduy-payment-failures
+ #
+ # Relying on the time when the webhooks are sent has caused issues where
+ # subscriptions have ended up `past_due` after a successful payment. Therefore,
+ # we're now explicitly ignoring the first webhook (with the update that's not
+ # relevant to us).
+ irrelevant? = params["old_status"] == "paused" && params["status"] == "past_due"
+
+ if subscription && not irrelevant? do
subscription
|> Subscription.changeset(format_subscription(params))
|> Repo.update!()
diff --git a/test/plausible/billing/billing_test.exs b/test/plausible/billing/billing_test.exs
index e4b19cd85f..082a1e4489 100644
--- a/test/plausible/billing/billing_test.exs
+++ b/test/plausible/billing/billing_test.exs
@@ -212,6 +212,21 @@ defmodule Plausible.BillingTest do
assert subscription.next_bill_amount == "12.00"
end
+ test "status update from 'paused' to 'past_due' is ignored" do
+ user = insert(:user)
+ subscription = insert(:subscription, user: user, status: Subscription.Status.paused())
+
+ %{@subscription_updated_params | "old_status" => "paused", "status" => "past_due"}
+ |> Map.merge(%{
+ "subscription_id" => subscription.paddle_subscription_id,
+ "passthrough" => user.id
+ })
+ |> Billing.subscription_updated()
+
+ subscription = Repo.get_by(Subscription, user_id: user.id)
+ assert subscription.status == Subscription.Status.paused()
+ end
+
test "unlocks sites if subscription is changed from past_due to active" do
user = insert(:user)
subscription = insert(:subscription, user: user, status: Subscription.Status.past_due())