diff --git a/assets/css/tooltip.css b/assets/css/tooltip.css index d2dd2f474..727e78b0a 100644 --- a/assets/css/tooltip.css +++ b/assets/css/tooltip.css @@ -30,7 +30,7 @@ color: #fff; font-size: .875rem; min-width: 80px; - max-width: 320px; + max-width: 420px; border-radius: 3px; pointer-events: none; padding: 4px 8px; diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex index 028917ef4..22f2c82f9 100644 --- a/lib/plausible/billing/billing.ex +++ b/lib/plausible/billing/billing.ex @@ -94,12 +94,19 @@ defmodule Plausible.Billing do def needs_to_upgrade?(user) do if Timex.before?(user.trial_expiry_date, Timex.today()) do - !active_subscription_for(user.id) + !subscription_is_active?(user.subscription) else false end end + defp subscription_is_active?(%Subscription{status: "active"}), do: true + defp subscription_is_active?(%Subscription{status: "past_due"}), do: true + defp subscription_is_active?(%Subscription{status: "deleted"} = subscription) do + subscription.next_bill_date && !Timex.before?(subscription.next_bill_date, Timex.today()) + end + defp subscription_is_active?(_), do: false + def on_trial?(user), do: trial_days_left(user) >= 0 def trial_days_left(user) do diff --git a/lib/plausible_web/controllers/auth_controller.ex b/lib/plausible_web/controllers/auth_controller.ex index 6ad16ca56..4444a4204 100644 --- a/lib/plausible_web/controllers/auth_controller.ex +++ b/lib/plausible_web/controllers/auth_controller.ex @@ -236,8 +236,7 @@ defmodule PlausibleWeb.AuthController do def user_settings(conn, _params) do changeset = Auth.User.changeset(conn.assigns[:current_user]) - subscription = Plausible.Billing.active_subscription_for(conn.assigns[:current_user].id) - render(conn, "user_settings.html", changeset: changeset, subscription: subscription) + render(conn, "user_settings.html", changeset: changeset, subscription: conn.assigns[:current_user].subscription) end def save_settings(conn, %{"user" => user_params}) do diff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.ex index 87ec20944..cd2e5ccb2 100644 --- a/lib/plausible_web/controllers/stats_controller.ex +++ b/lib/plausible_web/controllers/stats_controller.ex @@ -15,7 +15,7 @@ defmodule PlausibleWeb.StatsController do user = conn.assigns[:current_user] if user && Plausible.Billing.needs_to_upgrade?(conn.assigns[:current_user]) do - redirect(conn, to: "/billing/upgrade") + redirect(conn, to: "/settings") else if Stats.has_pageviews?(site) do demo = site.domain == base_domain() diff --git a/lib/plausible_web/plugs/auth_plug.ex b/lib/plausible_web/plugs/auth_plug.ex index bf4ba1178..8a81b8c0c 100644 --- a/lib/plausible_web/plugs/auth_plug.ex +++ b/lib/plausible_web/plugs/auth_plug.ex @@ -14,7 +14,7 @@ defmodule PlausibleWeb.AuthPlug do id -> user = Repo.get_by(Plausible.Auth.User, id: id) - |> Repo.preload(:subscription) + |> Repo.preload(subscription: from(s in Plausible.Billing.Subscription, order_by: [desc: s.inserted_at])) if user do Sentry.Context.set_user_context(%{id: user.id, name: user.name}) diff --git a/lib/plausible_web/templates/auth/user_settings.html.eex b/lib/plausible_web/templates/auth/user_settings.html.eex index f627749f4..f293700e2 100644 --- a/lib/plausible_web/templates/auth/user_settings.html.eex +++ b/lib/plausible_web/templates/auth/user_settings.html.eex @@ -1,14 +1,46 @@
-

Subscription Plan

+
+

Subscription Plan

+ <%= if @subscription do %> + + <%= present_subscription_status(@subscription.status) %> + + <% end %> +
+ +
-
+ <%= if @subscription && @subscription.status == "deleted" do %> +
+
+
+ + + +

+ <%= if @subscription.next_bill_date && Timex.compare(@subscription.next_bill_date, Timex.today()) >= 0 do %> + Your subscription is cancelled but you have access to your stats until <%= Timex.format!(@subscription.next_bill_date, "{Mshort} {D}, {YYYY}") %>. Upgrade below to make sure you don't lose access. + <% else %> + Your subscription is cancelled. Upgrade below to get access to your stats again. + <% end %> +

+
+
+
+ <% end %>

Current plan

<%= if @subscription do %>
<%= subscription_name(@subscription) %>
- <%= link("Change plan", to: "/billing/change-plan", class: "text-sm text-indigo-500 font-medium") %> + <%= case @subscription.status do %> + <% "active" -> %> + <%= link("Change plan", to: "/billing/change-plan", class: "text-sm text-indigo-500 font-medium") %> + <% "past_due" -> %> + Change plan + <% _ -> %> + <% end %> <% else %>
Free trial
<%= link("Upgrade", to: "/billing/upgrade", class: "text-sm text-indigo-500 font-medium") %> @@ -16,22 +48,22 @@

Next bill amount

- <%= if @subscription do %> + <%= if @subscription && @subscription.status in ["active", "past_due"] do %>
$<%= @subscription.next_bill_amount %>
<%= if @subscription.update_url do %> <%= link("Update billing info", to: @subscription.update_url, class: "text-sm text-indigo-500 font-medium") %> <% end %> <% else %> -
$0
+
---
<% end %>

Next bill date

- <%= if @subscription && @subscription.next_bill_date do %> + <%= if @subscription && @subscription.next_bill_date && @subscription.status in ["active", "past_due"] do %>
<%= Timex.format!(@subscription.next_bill_date, "{Mshort} {D}, {YYYY}") %>
<% else %> -
–
+
---
<% end %>
@@ -43,10 +75,15 @@ pageviews in the last 30 days
- <%= if @subscription && @subscription.cancel_url do %> -
- <%= link("Cancel my subscription", to: @subscription.cancel_url, class: "text-indigo-500 text-sm") %> -
+ <%= cond do %> + <% @subscription && @subscription.status in ["active", "past_due", "paused"] && @subscription.cancel_url -> %> +
+ <%= link("Cancel my subscription", to: @subscription.cancel_url, class: "inline-block mt-4 px-4 py-2 border border-gray-300 text-sm leading-5 font-medium rounded-md text-red-700 bg-white hover:text-red-500 focus:outline-none focus:border-blue-300 focus:shadow-outline-blue active:text-red-800 active:bg-gray-50 transition ease-in-out duration-150", method: "delete") %> +
+ <% true -> %> +
+ <%= link("Upgrade", to: "/billing/upgrade", class: "inline-block px-3 py-2 border border-transparent text-sm leading-4 font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-500 focus:outline-none focus:border-indigo-700 focus:shadow-outline-indigo active:bg-indigo-700 transition ease-in-out duration-150") %> +
<% end %> @@ -83,6 +120,6 @@ Delete my account

Your account cannot be deleted because you have an active subscription. If you want to delete your account, please cancel your subscription first.

<% else %> - <%= link "Delete my account ", to: "/me", method: :delete, class: "button bg-red-600 mt-6", data: [confirm: "Deleting your account cannot be reversed. Are you sure?"] %> + <%= link("Delete my account", to: "/me", class: "inline-block mt-4 px-4 py-2 border border-gray-300 text-sm leading-5 font-medium rounded-md text-red-700 bg-white hover:text-red-500 focus:outline-none focus:border-blue-300 focus:shadow-outline-blue active:text-red-800 active:bg-gray-50 transition ease-in-out duration-150", method: "delete", data: [confirm: "Deleting your account cannot be reversed. Are you sure?"]) %> <% end %> diff --git a/lib/plausible_web/templates/layout/app.html.eex b/lib/plausible_web/templates/layout/app.html.eex index c92e392a1..964a0fa62 100644 --- a/lib/plausible_web/templates/layout/app.html.eex +++ b/lib/plausible_web/templates/layout/app.html.eex @@ -95,6 +95,50 @@ <% end %> + <%= if @conn.assigns[:current_user] && @conn.assigns[:current_user].subscription && @conn.assigns[:current_user].subscription.status == "past_due" do %> +
+
+
+
+ + + +

+ Your latest payment failed. Please provide valid payment details to keep using Plausible. +

+
+
+
+ <%= link("Update billing info", to: @conn.assigns[:current_user].subscription.update_url, class: "flex items-center justify-center px-4 py-2 border border-transparent text-sm leading-5 font-medium rounded-md text-gray-600 bg-white hover:text-gray-500 focus:outline-none focus:shadow-outline transition ease-in-out duration-150") %> +
+
+
+
+
+ <% end %> + + <%= if @conn.assigns[:current_user] && @conn.assigns[:current_user].subscription && @conn.assigns[:current_user].subscription.status == "paused" do %> +
+
+
+
+ + + +

+ Your subscription is paused due to failed payments. Please provide valid payment details to keep using Plausible. +

+
+
+
+ <%= link("Update billing info", to: @conn.assigns[:current_user].subscription.update_url, class: "flex items-center justify-center px-4 py-2 border border-transparent text-sm leading-5 font-medium rounded-md text-gray-600 bg-white hover:text-gray-500 focus:outline-none focus:shadow-outline transition ease-in-out duration-150") %> +
+
+
+
+
+ <% end %> +
<%= render @view_module, @view_template, assigns %>
diff --git a/lib/plausible_web/views/auth_view.ex b/lib/plausible_web/views/auth_view.ex index f63fe24b2..f009fa462 100644 --- a/lib/plausible_web/views/auth_view.ex +++ b/lib/plausible_web/views/auth_view.ex @@ -51,4 +51,16 @@ defmodule PlausibleWeb.AuthView do defp delimit_integer(list, acc) do :lists.reverse(list) ++ acc end + + def present_subscription_status("active"), do: "Active" + def present_subscription_status("past_due"), do: "Past due" + def present_subscription_status("deleted"), do: "Cancelled" + def present_subscription_status("paused"), do: "Paused" + def present_subscription_status(status), do: status + + def subscription_colors("active"), do: "bg-green-100 text-green-800" + def subscription_colors("past_due"), do: "bg-yellow-100 text-yellow-800" + def subscription_colors("paused"), do: "bg-red-100 text-red-800" + def subscription_colors("deleted"), do: "bg-red-100 text-red-800" + def subscription_colors(_), do: "" end diff --git a/test/plausible/billing/billing_test.exs b/test/plausible/billing/billing_test.exs index cbaa7dd6d..e17bcc023 100644 --- a/test/plausible/billing/billing_test.exs +++ b/test/plausible/billing/billing_test.exs @@ -48,12 +48,14 @@ defmodule Plausible.BillingTest do describe "needs_to_upgrade?" do test "is false for a trial user" do user = insert(:user) + user = Repo.preload(user, :subscription) refute Billing.needs_to_upgrade?(user) end test "is true for a user with an expired trial" do user = insert(:user, trial_expiry_date: Timex.shift(Timex.today(), days: -1)) + user = Repo.preload(user, :subscription) assert Billing.needs_to_upgrade?(user) end @@ -61,13 +63,31 @@ defmodule Plausible.BillingTest do test "is false for a user with an expired trial but an active subscription" do user = insert(:user, trial_expiry_date: Timex.shift(Timex.today(), days: -1)) insert(:subscription, user: user) + user = Repo.preload(user, :subscription) refute Billing.needs_to_upgrade?(user) end - test "is true for a user with an expired trial and a non-active subscription" do + test "is false for a user with a cancelled subscription IF the billing cycle isn't completed yet" do user = insert(:user, trial_expiry_date: Timex.shift(Timex.today(), days: -1)) - insert(:subscription, user: user, status: "past_due") + insert(:subscription, user: user, status: "deleted", next_bill_date: Timex.today()) + user = Repo.preload(user, :subscription) + + refute Billing.needs_to_upgrade?(user) + end + + test "is true for a user with a cancelled subscription IF the billing cycle is complete" do + user = insert(:user, trial_expiry_date: Timex.shift(Timex.today(), days: -1)) + insert(:subscription, user: user, status: "deleted", next_bill_date: Timex.shift(Timex.today(), days: -1)) + user = Repo.preload(user, :subscription) + + assert Billing.needs_to_upgrade?(user) + end + + test "is false for a deleted subscription if not next_bill_date specified" do + user = insert(:user, trial_expiry_date: Timex.shift(Timex.today(), days: -1)) + insert(:subscription, user: user, status: "deleted", next_bill_date: nil) + user = Repo.preload(user, :subscription) assert Billing.needs_to_upgrade?(user) end diff --git a/test/plausible_web/plugs/auth_plug_test.exs b/test/plausible_web/plugs/auth_plug_test.exs new file mode 100644 index 000000000..a0c59aff5 --- /dev/null +++ b/test/plausible_web/plugs/auth_plug_test.exs @@ -0,0 +1,40 @@ +defmodule PlausibleWeb.AuthPlugTest do + use Plausible.DataCase + use Plug.Test + alias PlausibleWeb.AuthPlug + + test "does nothing if user is not logged in" do + conn = + conn(:get, "/") + |> init_test_session(%{}) + |> AuthPlug.call(%{}) + + assert is_nil(conn.assigns[:current_user]) + end + + test "looks up current user if they are logged in" do + user = insert(:user) + subscription = insert(:subscription, user: user) + conn = + conn(:get, "/") + |> init_test_session(%{current_user_id: user.id}) + |> AuthPlug.call(%{}) + + assert conn.assigns[:current_user].id == user.id + assert conn.assigns[:current_user].subscription.id == subscription.id + end + + test "looks up the latest subscription" do + user = insert(:user) + _old_subscription = insert(:subscription, user: user, inserted_at: Timex.now() |> Timex.shift(days: -1)) + subscription = insert(:subscription, user: user, inserted_at: Timex.now()) + + conn = + conn(:get, "/") + |> init_test_session(%{current_user_id: user.id}) + |> AuthPlug.call(%{}) + + assert conn.assigns[:current_user].id == user.id + assert conn.assigns[:current_user].subscription.id == subscription.id + end +end