Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Redirect user after upgrading only when subscription is created (#2285)
Vinicius Brasilbeabef7269db
diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.exindex ef28e5e877..ad0762ee97 100644--- a/lib/plausible/billing/billing.ex+++ b/lib/plausible/billing/billing.ex@@ -2,8 +2,23 @@ defmodule Plausible.Billing douse Plausible.Repoalias Plausible.Billing.Subscription+ @spec active_subscription_for(integer()) :: Subscription.t() | nildef active_subscription_for(user_id) do- Repo.get_by(Subscription, user_id: user_id, status: "active")+ user_id |> active_subscription_query() |> Repo.one()+ end++ @spec has_active_subscription?(integer()) :: boolean()+ def has_active_subscription?(user_id) do+ user_id |> active_subscription_query() |> Repo.exists?()+ end++ defp active_subscription_query(user_id) do+ from(+ s in Subscription,+ where: s.user_id == ^user_id and s.status == "active",+ order_by: [desc: s.inserted_at],+ limit: 1+ )enddef subscription_created(params) dodiff --git a/lib/plausible_web/controllers/billing_controller.ex b/lib/plausible_web/controllers/billing_controller.exindex 22dacba709..ccd8cd2f93 100644--- a/lib/plausible_web/controllers/billing_controller.ex+++ b/lib/plausible_web/controllers/billing_controller.ex@@ -6,6 +6,11 @@ defmodule PlausibleWeb.BillingController doplug PlausibleWeb.RequireAccountPlug+ def ping_subscription(%Plug.Conn{} = conn, _params) do+ subscribed? = Billing.has_active_subscription?(conn.assigns.current_user.id)+ json(conn, %{is_subscribed: subscribed?})+ end+def upgrade(conn, _params) douser =conn.assigns[:current_user]diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.exindex 8328d16f0c..d86049d508 100644--- a/lib/plausible_web/router.ex+++ b/lib/plausible_web/router.ex@@ -172,6 +172,7 @@ defmodule PlausibleWeb.Router doget "/billing/upgrade/enterprise/:plan_id", BillingController, :upgrade_enterprise_planget "/billing/change-plan/enterprise/:plan_id", BillingController, :change_enterprise_planget "/billing/upgrade-success", BillingController, :upgrade_success+ get "/billing/subscription/ping", BillingController, :ping_subscriptionget "/sites", SiteController, :indexget "/sites/new", SiteController, :newdiff --git a/lib/plausible_web/templates/billing/upgrade_success.html.eex b/lib/plausible_web/templates/billing/upgrade_success.html.eexindex 4990bbacfa..524226e3e4 100644--- a/lib/plausible_web/templates/billing/upgrade_success.html.eex+++ b/lib/plausible_web/templates/billing/upgrade_success.html.eex@@ -1,18 +1,51 @@<div class="w-full max-w-lg px-4 mt-4 mx-auto">- <div x-data="{volume: '10k', billingCycle: 'monthly'}" class="flex-1 bg-white dark:bg-gray-800 shadow-md rounded px-8 py-4 mb-4 mt-8">+ <div class="flex-1 bg-white dark:bg-gray-800 shadow-md rounded px-8 py-4 mb-4 mt-8"><div class="w-full pt-2 text-xl font-bold dark:text-gray-100">- Subscription created successfully+ Your account is being upgraded...</div>- <div class="w-full text-gray-500 dark:text-gray-200 text-sm py-4">- Thank you for upgrading your subscription. You will be redirected to your- account within 5 seconds.- </div>+ <p class="text-gray-500 dark:text-gray-200 text-sm py-4">+ Thank you for upgrading your subscription! We're still working on+ upgrading your account, and you'll be automatically redirected in a few+ seconds.+ </p>++ <p hidden id="timeout-notice" class="text-gray-500 dark:text-gray-200 text-sm">+ Your subscription is taking longer than usual to upgrade. If you're not+ redirected soon, please contact <a class="text-indigo-500" href="mailto:[email protected]">[email protected]</a>.+ </p><div class="loading my-12 mx-auto"><div></div></div><script>- setTimeout(function() {window.location='/settings'}, 3500);+ const PING_SUBSCRIPTION_API = "<%= Routes.billing_path(@conn, :ping_subscription) %>"+ const REDIRECT_TO = "<%= Routes.auth_path(@conn, :user_settings) %>"+ const PING_EVERY_MS = 2000+ const TIMEOUT_AFTER_MS = 15000++ const ping = async function(fun) {+ let result = {}++ while (!result.is_subscribed) {+ await wait();+ const response = await fetch(PING_SUBSCRIPTION_API)+ result = await response.json()+ }++ window.location = REDIRECT_TO+ }++ const wait = function() {+ return new Promise(resolve => { setTimeout(resolve, PING_EVERY_MS) })+ }++ setTimeout(() => {+ document.getElementById("timeout-notice").removeAttribute("hidden")+ }, TIMEOUT_AFTER_MS)++ // Pings pingSubscriptionUrl every 2 seconds until an active subscription+ // is created from Paddle webhooks.+ ping()</script></div></div>diff --git a/test/plausible/billing/billing_test.exs b/test/plausible/billing/billing_test.exsindex afe5f7dbaf..61984dc668 100644--- a/test/plausible/billing/billing_test.exs+++ b/test/plausible/billing/billing_test.exs@@ -594,4 +594,24 @@ defmodule Plausible.BillingTest doassert subscription.next_bill_amount == "6.00"endend++ test "active_subscription_for/1 returns active subscription" do+ active = insert(:subscription, user: insert(:user), status: "active")+ paused = insert(:subscription, user: insert(:user), status: "paused")+ user_without_subscription = insert(:user)++ assert Billing.active_subscription_for(active.user_id).id == active.id+ assert Billing.active_subscription_for(paused.user_id) == nil+ assert Billing.active_subscription_for(user_without_subscription.id) == nil+ end++ test "has_active_subscription?/1 returns whether the user has an active subscription" do+ active = insert(:subscription, user: insert(:user), status: "active")+ paused = insert(:subscription, user: insert(:user), status: "paused")+ user_without_subscription = insert(:user)++ assert Billing.has_active_subscription?(active.user_id)+ refute Billing.has_active_subscription?(paused.user_id)+ refute Billing.has_active_subscription?(user_without_subscription.id)+ endenddiff --git a/test/plausible_web/controllers/billing_controller_test.exs b/test/plausible_web/controllers/billing_controller_test.exsindex d193b0954d..06b456a5f9 100644--- a/test/plausible_web/controllers/billing_controller_test.exs+++ b/test/plausible_web/controllers/billing_controller_test.exs@@ -143,7 +143,7 @@ defmodule PlausibleWeb.BillingControllerTest dotest "shows success page after user subscribes", %{conn: conn} doconn = get(conn, "/billing/upgrade-success")- assert html_response(conn, 200) =~ "Subscription created successfully"+ assert html_response(conn, 200) =~ "Your account is being upgraded"endendend