Redirect user after upgrading only when subscription is created (#2285)

* Create API to check for an active subscription

* Redirect after upgrading only when subscription is created

* Change upgrade page copy

* Add timeout notice to upgrade success page
pull/2305/head
Vinicius Brasil 4 years ago committed by GitHub
parent bf2b6880c5
commit beabef7269
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,8 +2,23 @@ defmodule Plausible.Billing do
use Plausible.Repo
alias Plausible.Billing.Subscription
@spec active_subscription_for(integer()) :: Subscription.t() | nil
def 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
)
end
def subscription_created(params) do

@ -6,6 +6,11 @@ defmodule PlausibleWeb.BillingController do
plug 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) do
user =
conn.assigns[:current_user]

@ -172,6 +172,7 @@ defmodule PlausibleWeb.Router do
get "/billing/upgrade/enterprise/:plan_id", BillingController, :upgrade_enterprise_plan
get "/billing/change-plan/enterprise/:plan_id", BillingController, :change_enterprise_plan
get "/billing/upgrade-success", BillingController, :upgrade_success
get "/billing/subscription/ping", BillingController, :ping_subscription
get "/sites", SiteController, :index
get "/sites/new", SiteController, :new

@ -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:hello@plausible.io">hello@plausible.io</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>

@ -594,4 +594,24 @@ defmodule Plausible.BillingTest do
assert subscription.next_bill_amount == "6.00"
end
end
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)
end
end

@ -143,7 +143,7 @@ defmodule PlausibleWeb.BillingControllerTest do
test "shows success page after user subscribes", %{conn: conn} do
conn = get(conn, "/billing/upgrade-success")
assert html_response(conn, 200) =~ "Subscription created successfully"
assert html_response(conn, 200) =~ "Your account is being upgraded"
end
end
end

Loading…
Cancel
Save