diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex
index 47f71c278..e292cb06d 100644
--- a/lib/plausible/billing/billing.ex
+++ b/lib/plausible/billing/billing.ex
@@ -107,13 +107,19 @@ defmodule Plausible.Billing do
PaddleApi.update_subscription_preview(subscription.paddle_subscription_id, new_plan_id)
end
- def needs_to_upgrade?(%Plausible.Auth.User{trial_expiry_date: nil}), do: true
+ def needs_to_upgrade?(%Plausible.Auth.User{trial_expiry_date: nil}), do: {true, :no_trial}
def needs_to_upgrade?(user) do
- if Timex.before?(user.trial_expiry_date, Timex.today()) do
- !subscription_is_active?(user.subscription)
- else
- false
+ trial_is_over = Timex.before?(user.trial_expiry_date, Timex.today())
+ subscription_active = subscription_is_active?(user.subscription)
+
+ grace_period_ended =
+ user.grace_period_end && Timex.before?(user.grace_period_end, Timex.today())
+
+ cond do
+ trial_is_over && !subscription_active -> {true, :no_active_subscription}
+ grace_period_ended -> {true, :grace_period_ended}
+ true -> false
end
end
diff --git a/lib/plausible/billing/site_locker.ex b/lib/plausible/billing/site_locker.ex
index e7cbedb65..986a15f85 100644
--- a/lib/plausible/billing/site_locker.ex
+++ b/lib/plausible/billing/site_locker.ex
@@ -2,10 +2,16 @@ defmodule Plausible.Billing.SiteLocker do
use Plausible.Repo
def check_sites_for(user) do
- if Plausible.Billing.needs_to_upgrade?(user) do
- set_lock_status_for(user, true)
- else
- set_lock_status_for(user, false)
+ case Plausible.Billing.needs_to_upgrade?(user) do
+ {true, :grace_period_ended} ->
+ set_lock_status_for(user, true)
+ send_grace_period_end_email(user)
+
+ {true, _} ->
+ set_lock_status_for(user, true)
+
+ _ ->
+ set_lock_status_for(user, false)
end
end
@@ -26,4 +32,20 @@ defmodule Plausible.Billing.SiteLocker do
Repo.update_all(site_q, set: [locked: status])
end
+
+ defp send_grace_period_end_email(user) do
+ {_, last_cycle} = Plausible.Billing.last_two_billing_cycles(user)
+ {_, last_cycle_usage} = Plausible.Billing.last_two_billing_months_usage(user)
+ suggested_plan = Plausible.Billing.Plans.suggested_plan(user, last_cycle)
+
+ template =
+ PlausibleWeb.Email.dashboard_locked(
+ user,
+ last_cycle_usage,
+ last_cycle,
+ suggested_plan
+ )
+
+ Plausible.Mailer.send_email_safe(template)
+ end
end
diff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.ex
index 698282ab5..c132f7d52 100644
--- a/lib/plausible_web/email.ex
+++ b/lib/plausible_web/email.ex
@@ -142,6 +142,19 @@ defmodule PlausibleWeb.Email do
})
end
+ def dashboard_locked(user, usage, last_cycle, suggested_plan) do
+ base_email()
+ |> to(user)
+ |> tag("dashboard-locked")
+ |> subject("[Action required] Your Plausible dashboard is now locked")
+ |> render("dashboard_locked.html", %{
+ user: user,
+ usage: usage,
+ last_cycle: last_cycle,
+ suggested_plan: suggested_plan
+ })
+ end
+
def yearly_renewal_notification(user) do
date = Timex.format!(user.subscription.next_bill_date, "{Mfull} {D}, {YYYY}")
diff --git a/lib/plausible_web/templates/email/dashboard_locked.html.eex b/lib/plausible_web/templates/email/dashboard_locked.html.eex
new file mode 100644
index 000000000..4030d7cf9
--- /dev/null
+++ b/lib/plausible_web/templates/email/dashboard_locked.html.eex
@@ -0,0 +1,25 @@
+Hey <%= user_salutation(@user) %>,
+
+Last week we sent a reminder that your traffic has exceeded your Plausible Analytics subscription tier two months in a row.
+
+Your dashboard is now locked. We're still counting your stats, but you no longer have access to the stats. As you have outgrown your subscription tier, we kindly ask you to upgrade your subscription to accommodate your new traffic levels.
+
+Click here to go to your site settings. In the last billing cycle (<%= date_format(@last_cycle.first) %> to <%= date_format(@last_cycle.last) %>), your account has used <%= PlausibleWeb.StatsView.large_number_format(@usage) %> billable pageviews.
+<%= if @usage <= 20_000_000 do %>
+Based on that we recommend you select the <%= @suggested_plan[:volume] %>/mo plan.
+
+You can upgrade your subscription using our self-serve platform. The new charge will be prorated to reflect the amount you have already paid and the time until your current subscription is supposed to expire.
+
+Click here to go to your site settings. You can upgrade your subscription tier by clicking the 'change plan' link.
+<% else %>
+This is more than our standard plans, so please reply back to this email to get a quote for your volume.
+<% end %>
+
+The new charge will be prorated to reflect the amount you have already paid and the time until your current subscription is supposed to expire. We will unlock your dashboard immediately after your account has been upgraded to the appropriate tier.
+
+Thanks for understanding and for being a Plausible subscriber!
+
+Thanks,
+Uku and Marko
+--
+<%= plausible_url() %>
diff --git a/test/plausible/billing/site_locker_test.exs b/test/plausible/billing/site_locker_test.exs
index b61812f4b..e42a2e0d4 100644
--- a/test/plausible/billing/site_locker_test.exs
+++ b/test/plausible/billing/site_locker_test.exs
@@ -49,6 +49,17 @@ defmodule Plausible.Billing.SiteLockerTest do
refute Repo.reload!(site).locked
end
+ test "does not lock user who has an active subscription and is on grace period" do
+ user = insert(:user, grace_period_end: Timex.shift(Timex.today(), days: 1))
+ insert(:subscription, status: "active", user: user)
+ user = Repo.preload(user, :subscription)
+ site = insert(:site, members: [user])
+
+ SiteLocker.check_sites_for(user)
+
+ refute Repo.reload!(site).locked
+ end
+
test "locks user who cancelled subscription and the cancelled subscription has expired" do
user = insert(:user)
@@ -67,6 +78,31 @@ defmodule Plausible.Billing.SiteLockerTest do
refute Repo.reload!(site).locked
end
+ test "locks all sites if user has active subscription but grace period has ended" do
+ user = insert(:user, grace_period_end: Timex.shift(Timex.today(), days: -1))
+ insert(:subscription, status: "active", user: user)
+ user = Repo.preload(user, :subscription)
+ site = insert(:site, members: [user])
+
+ SiteLocker.check_sites_for(user)
+
+ assert Repo.reload!(site).locked
+ end
+
+ test "sends email if grace period has ended" do
+ user = insert(:user, grace_period_end: Timex.shift(Timex.today(), days: -1))
+ insert(:subscription, status: "active", user: user)
+ user = Repo.preload(user, :subscription)
+ insert(:site, members: [user])
+
+ SiteLocker.check_sites_for(user)
+
+ assert_email_delivered_with(
+ to: [user],
+ subject: "[Action required] Your Plausible dashboard is now locked"
+ )
+ end
+
test "locks all sites if user has no trial or active subscription" do
user =
insert(:user, trial_expiry_date: Timex.today() |> Timex.shift(days: -1))
diff --git a/test/support/factory.ex b/test/support/factory.ex
index dc2249a55..4ffbf7070 100644
--- a/test/support/factory.ex
+++ b/test/support/factory.ex
@@ -112,6 +112,7 @@ defmodule Plausible.Factory do
status: "active",
next_bill_amount: "6.00",
next_bill_date: Timex.today(),
+ last_bill_date: Timex.today(),
currency_code: "USD"
}
end