Fix billing bugs (#208)

* Show past due subscription in user settings

* Users who are past due keep their access

* Add more info in the past due banner

* Show cancelled subscription plan

* Add init admin back into test suite

* Make sure users have access to their stats until the end of billing
cycle

* Fix free subscription bug

* Ensure the latest subscription is loaded
pull/215/head
Uku Taht 6 years ago committed by GitHub
parent 66906bdb6e
commit 7683839de1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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;

@ -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

@ -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

@ -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()

@ -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})

@ -1,14 +1,46 @@
<div class="max-w-2xl mx-auto bg-white shadow-md rounded rounded-t-none border-t-2 border-orange-200 px-8 pt-6 pb-8 mt-24">
<h2 class="text-xl font-black">Subscription Plan</h2>
<div class="flex justify-between">
<h2 class="text-xl font-black">Subscription Plan</h2>
<%= if @subscription do %>
<span class="inline-flex items-center px-2.5 py-0.5 rounded-md text-sm font-bold leading-5 <%= subscription_colors(@subscription.status) %>">
<%= present_subscription_status(@subscription.status) %>
</span>
<% end %>
</div>
<div class="my-4 border-b border-gray-400"></div>
<div class="mt-4 border-b border-gray-400"></div>
<%= if @subscription && @subscription.status == "deleted" do %>
<div class="p-2 rounded-lg bg-red-100 sm:p-3">
<div class="flex items-center justify-between flex-wrap">
<div class="w-0 flex-1 flex items-center">
<svg class="h-6 w-6 text-red-800" viewBox="0 0 24 24" stroke="currentColor" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M12 9V11M12 15H12.01M5.07183 19H18.9282C20.4678 19 21.4301 17.3333 20.6603 16L13.7321 4C12.9623 2.66667 11.0378 2.66667 10.268 4L3.33978 16C2.56998 17.3333 3.53223 19 5.07183 19Z" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
<p class="ml-3 font-medium text-red-800">
<%= 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 %>
</p>
</div>
</div>
</div>
<% end %>
<div class="flex flex-col items-center sm:flex-row sm:items-start justify-between mt-8">
<div class="text-center bg-gray-100 py-4 px-2 rounded h-32 my-4" style="width: 11.75rem;">
<h4 class="font-black">Current plan</h4>
<%= if @subscription do %>
<div class="text-xl py-2 font-medium"><%= subscription_name(@subscription) %></div>
<%= 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" -> %>
<span class="text-sm text-gray-600 font-medium" tooltip="Please update your billing details before changing plans">Change plan</span>
<% _ -> %>
<% end %>
<% else %>
<div class="text-xl py-2 font-medium">Free trial</div>
<%= link("Upgrade", to: "/billing/upgrade", class: "text-sm text-indigo-500 font-medium") %>
@ -16,22 +48,22 @@
</div>
<div class="text-center bg-gray-100 py-4 px-2 rounded h-32 my-4" style="width: 11.75rem;">
<h4 class="font-black">Next bill amount</h4>
<%= if @subscription do %>
<%= if @subscription && @subscription.status in ["active", "past_due"] do %>
<div class="text-xl py-2 font-medium">$<%= @subscription.next_bill_amount %></div>
<%= if @subscription.update_url do %>
<%= link("Update billing info", to: @subscription.update_url, class: "text-sm text-indigo-500 font-medium") %>
<% end %>
<% else %>
<div class="text-xl py-2 font-medium">$0</div>
<div class="text-xl py-2 font-medium">---</div>
<% end %>
</div>
<div class="text-center bg-gray-100 py-4 px-2 rounded h-32 my-4" style="width: 11.75rem;">
<h4 class="font-black">Next bill date</h4>
<%= if @subscription && @subscription.next_bill_date do %>
<%= if @subscription && @subscription.next_bill_date && @subscription.status in ["active", "past_due"] do %>
<div class="text-xl py-2 font-medium"><%= Timex.format!(@subscription.next_bill_date, "{Mshort} {D}, {YYYY}") %></div>
<% else %>
<div class="text-xl py-2 font-medium">–</div>
<div class="text-xl py-2 font-medium">---</div>
<% end %>
</div>
</div>
@ -43,10 +75,15 @@
pageviews in the last 30 days
</div>
<%= if @subscription && @subscription.cancel_url do %>
<div class="mt-8">
<%= link("Cancel my subscription", to: @subscription.cancel_url, class: "text-indigo-500 text-sm") %>
</div>
<%= cond do %>
<% @subscription && @subscription.status in ["active", "past_due", "paused"] && @subscription.cancel_url -> %>
<div class="mt-8">
<%= 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") %>
</div>
<% true -> %>
<div class="mt-8">
<%= 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") %>
</div>
<% end %>
</div>
@ -83,6 +120,6 @@
<span class="button bg-gray-300 mt-6 hover:shadow-none">Delete my account</span>
<p class="text-gray-600 text-sm mt-2">Your account cannot be deleted because you have an active subscription. If you want to delete your account, please cancel your subscription first.</p>
<% 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 %>
</div>

@ -95,6 +95,50 @@
</div>
<% end %>
<%= if @conn.assigns[:current_user] && @conn.assigns[:current_user].subscription && @conn.assigns[:current_user].subscription.status == "past_due" do %>
<div class="container">
<div class="p-2 rounded-lg bg-yellow-100 sm:p-3">
<div class="flex items-center justify-between flex-wrap">
<div class="w-0 flex-1 flex items-center">
<svg class="h-6 w-6 text-yellow-800" viewBox="0 0 24 24" stroke="currentColor" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M12 9V11M12 15H12.01M5.07183 19H18.9282C20.4678 19 21.4301 17.3333 20.6603 16L13.7321 4C12.9623 2.66667 11.0378 2.66667 10.268 4L3.33978 16C2.56998 17.3333 3.53223 19 5.07183 19Z" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
<p class="ml-3 font-medium text-yellow-800">
Your latest payment failed. Please provide valid payment details to keep using Plausible.
</p>
</div>
<div class="order-3 mt-2 flex-shrink-0 w-full sm:order-2 sm:mt-0 sm:w-auto">
<div class="rounded-md shadow-sm">
<%= 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") %>
</div>
</div>
</div>
</div>
</div>
<% end %>
<%= if @conn.assigns[:current_user] && @conn.assigns[:current_user].subscription && @conn.assigns[:current_user].subscription.status == "paused" do %>
<div class="container">
<div class="p-2 rounded-lg bg-red-100 sm:p-3">
<div class="flex items-center justify-between flex-wrap">
<div class="w-0 flex-1 flex items-center">
<svg class="h-6 w-6 text-red-800" viewBox="0 0 24 24" stroke="currentColor" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M12 9V11M12 15H12.01M5.07183 19H18.9282C20.4678 19 21.4301 17.3333 20.6603 16L13.7321 4C12.9623 2.66667 11.0378 2.66667 10.268 4L3.33978 16C2.56998 17.3333 3.53223 19 5.07183 19Z" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
<p class="ml-3 font-medium text-red-800">
Your subscription is paused due to failed payments. Please provide valid payment details to keep using Plausible.
</p>
</div>
<div class="order-3 mt-2 flex-shrink-0 w-full sm:order-2 sm:mt-0 sm:w-auto">
<div class="rounded-md shadow-sm">
<%= 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") %>
</div>
</div>
</div>
</div>
</div>
<% end %>
<main class="flex-1">
<%= render @view_module, @view_template, assigns %>
</main>

@ -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

@ -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

@ -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
Loading…
Cancel
Save