Track billing cycles (#697)
* Add dummy check usage worker * Add and persist last_bill_date * Remove name clash * Correct timing and suggestions for over limit emails * Fix tests for trial upgrade notificationspull/709/head
parent
77628d04c9
commit
a8cb187e8d
@ -1,44 +1,67 @@
|
||||
defmodule Plausible.Billing.Plans do
|
||||
@plans %{
|
||||
monthly: %{
|
||||
"10k": %{product_id: "558018", due_now: "$6"},
|
||||
"100k": %{product_id: "558745", due_now: "$12"},
|
||||
"200k": %{product_id: "597485", due_now: "$18"},
|
||||
"500k": %{product_id: "597487", due_now: "$27"},
|
||||
"1m": %{product_id: "597642", due_now: "$48"},
|
||||
"2m": %{product_id: "597309", due_now: "$69"},
|
||||
"5m": %{product_id: "597311", due_now: "$99"},
|
||||
"10m": %{product_id: "642352", due_now: "$150"},
|
||||
"20m": %{product_id: "642355", due_now: "$225"}
|
||||
},
|
||||
yearly: %{
|
||||
"10k": %{product_id: "572810", due_now: "$48"},
|
||||
"100k": %{product_id: "590752", due_now: "$96"},
|
||||
"200k": %{product_id: "597486", due_now: "$144"},
|
||||
"500k": %{product_id: "597488", due_now: "$216"},
|
||||
"1m": %{product_id: "597643", due_now: "$384"},
|
||||
"2m": %{product_id: "597310", due_now: "$552"},
|
||||
"5m": %{product_id: "597312", due_now: "$792"},
|
||||
"10m": %{product_id: "642354", due_now: "$1200"},
|
||||
"20m": %{product_id: "642356", due_now: "$1800"}
|
||||
}
|
||||
}
|
||||
@monthly_plans [
|
||||
%{product_id: "558018", cost: "$6", limit: 10_000, cycle: "monthly"},
|
||||
%{product_id: "558745", cost: "$12", limit: 100_000, cycle: "monthly"},
|
||||
%{product_id: "597485", cost: "$18", limit: 200_000, cycle: "monthly"},
|
||||
%{product_id: "597487", cost: "$27", limit: 500_000, cycle: "monthly"},
|
||||
%{product_id: "597642", cost: "$48", limit: 1_000_000, cycle: "monthly"},
|
||||
%{product_id: "597309", cost: "$69", limit: 2_000_000, cycle: "monthly"},
|
||||
%{product_id: "597311", cost: "$99", limit: 5_000_000, cycle: "monthly"},
|
||||
%{product_id: "642352", cost: "$150", limit: 10_000_000, cycle: "monthly"},
|
||||
%{product_id: "642355", cost: "$225", limit: 20_000_000, cycle: "monthly"}
|
||||
]
|
||||
|
||||
@yearly_plans [
|
||||
%{product_id: "572810", cost: "$48", limit: 10_000, cycle: "yearly"},
|
||||
%{product_id: "590752", cost: "$96", limit: 100_000, cycle: "yearly"},
|
||||
%{product_id: "597486", cost: "$144", limit: 200_000, cycle: "yearly"},
|
||||
%{product_id: "597488", cost: "$216", limit: 500_000, cycle: "yearly"},
|
||||
%{product_id: "597643", cost: "$384", limit: 1_000_000, cycle: "yearly"},
|
||||
%{product_id: "597310", cost: "$552", limit: 2_000_000, cycle: "yearly"},
|
||||
%{product_id: "597312", cost: "$792", limit: 5_000_000, cycle: "yearly"},
|
||||
%{product_id: "642354", cost: "$1200", limit: 10_000_000, cycle: "yearly"},
|
||||
%{product_id: "642356", cost: "$1800", limit: 20_000_000, cycle: "yearly"}
|
||||
]
|
||||
|
||||
@all_plans @monthly_plans ++ @yearly_plans
|
||||
|
||||
def plans do
|
||||
@plans
|
||||
monthly =
|
||||
@monthly_plans
|
||||
|> Enum.map(fn plan -> {String.to_atom(number_format(plan[:limit])), plan} end)
|
||||
|> Enum.into(%{})
|
||||
|
||||
yearly =
|
||||
@yearly_plans
|
||||
|> Enum.map(fn plan -> {String.to_atom(number_format(plan[:limit])), plan} end)
|
||||
|> Enum.into(%{})
|
||||
|
||||
%{
|
||||
monthly: monthly,
|
||||
yearly: yearly
|
||||
}
|
||||
end
|
||||
|
||||
def suggested_plan_name(usage) do
|
||||
plan = suggested_plan(usage)
|
||||
number_format(plan[:limit]) <> "/mo"
|
||||
end
|
||||
|
||||
def suggested_plan_cost(usage) do
|
||||
plan = suggested_plan(usage)
|
||||
plan[:cost] <> "/mo"
|
||||
end
|
||||
|
||||
defp suggested_plan(usage) do
|
||||
Enum.find(@monthly_plans, fn plan -> usage < plan[:limit] end)
|
||||
end
|
||||
|
||||
def allowance(subscription) do
|
||||
allowed_volume = %{
|
||||
"free_10k" => 10_000,
|
||||
"558018" => 10_000,
|
||||
"572810" => 10_000,
|
||||
"558745" => 100_000,
|
||||
"590752" => 100_000,
|
||||
"558746" => 1_000_000,
|
||||
"590753" => 1_000_000
|
||||
}
|
||||
Enum.find(@all_plans, fn plan -> plan[:product_id] == subscription.paddle_plan_id end)
|
||||
|> Map.fetch!(:limit)
|
||||
end
|
||||
|
||||
allowed_volume[subscription.paddle_plan_id]
|
||||
defp number_format(num) do
|
||||
PlausibleWeb.StatsView.large_number_format(num)
|
||||
end
|
||||
end
|
||||
|
||||
@ -0,0 +1,32 @@
|
||||
Hey <%= user_salutation(@user) %>,
|
||||
<br /><br />
|
||||
Thanks for being a Plausible Analytics subscriber!
|
||||
<br /><br />
|
||||
This is a friendly reminder that your traffic has exceeded your subscription tier two months in a row. Congrats on all that traffic!
|
||||
<br /><br />
|
||||
We don't enforce any hard limits at the moment, we're still counting your stats and you have access to your dashboard, but we kindly ask you to upgrade your subscription plan to accommodate your new traffic levels.
|
||||
<br /><br />
|
||||
In the last month, your account has used <%= @usage %> billable pageviews.
|
||||
<%= if @usage <= 20_000_000 do %>
|
||||
|
||||
Based on that we recommend you select the <%= Plausible.Billing.Plans.suggested_plan_name(@usage) %> plan which runs at <%= Plausible.Billing.Plans.suggested_plan_cost(@usage) %>. You can also go with yearly billing to get 33% off.
|
||||
<br /><br />
|
||||
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.
|
||||
<br /><br />
|
||||
<a href="https://plausible.io/settings">Click here</a> 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 %>
|
||||
<br /><br />
|
||||
Were the last two months extraordinary and you don't expect these higher traffic levels to continue? Reply to this email and we'll figure it out.
|
||||
<br /><br />
|
||||
Have questions or need help with anything? Just reply to this email and we'll gladly help.
|
||||
<br /><br />
|
||||
Thanks again for using our product and for your support!
|
||||
<br /><br />
|
||||
Uku and Marko
|
||||
<br /><br />
|
||||
--
|
||||
<br /><br />
|
||||
<%= plausible_url() %><br />
|
||||
{{{ pm:unsubscribe }}}
|
||||
@ -0,0 +1,61 @@
|
||||
defmodule Plausible.Workers.CheckUsage do
|
||||
use Plausible.Repo
|
||||
use Oban.Worker, queue: :check_usage
|
||||
|
||||
defmacro yesterday() do
|
||||
quote do
|
||||
fragment("now() - INTERVAL '1 day'")
|
||||
end
|
||||
end
|
||||
|
||||
defmacro last_day_of_month(day) do
|
||||
quote do
|
||||
fragment(
|
||||
"(date_trunc('month', ?::date) + interval '1 month' - interval '1 day')::date",
|
||||
unquote(day)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
defmacro day_of_month(date) do
|
||||
quote do
|
||||
fragment("EXTRACT(day from ?::date)", unquote(date))
|
||||
end
|
||||
end
|
||||
|
||||
defmacro least(left, right) do
|
||||
quote do
|
||||
fragment("least(?, ?)", unquote(left), unquote(right))
|
||||
end
|
||||
end
|
||||
|
||||
@impl Oban.Worker
|
||||
def perform(_args, _job, billing_mod \\ Plausible.Billing) do
|
||||
yesterday = Timex.today() |> Timex.shift(days: -1)
|
||||
|
||||
active_subscribers =
|
||||
Repo.all(
|
||||
from u in Plausible.Auth.User,
|
||||
join: s in Plausible.Billing.Subscription,
|
||||
on: s.user_id == u.id,
|
||||
where: s.status == "active",
|
||||
# Accounts for situations like last_bill_date==2021-01-31 AND today==2021-03-01. Since February never reaches the 31st day, the account is checked on 2021-03-01.
|
||||
where:
|
||||
least(day_of_month(s.last_bill_date), day_of_month(last_day_of_month(^yesterday))) ==
|
||||
day_of_month(^yesterday),
|
||||
preload: [subscription: s]
|
||||
)
|
||||
|
||||
for subscriber <- active_subscribers do
|
||||
allowance = Plausible.Billing.Plans.allowance(subscriber.subscription)
|
||||
{last_last_month, last_month} = billing_mod.last_two_billing_months_usage(subscriber)
|
||||
|
||||
if last_last_month > allowance && last_month > allowance do
|
||||
template = PlausibleWeb.Email.over_limit_email(subscriber, last_month)
|
||||
Plausible.Mailer.send_email(template)
|
||||
end
|
||||
end
|
||||
|
||||
:ok
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,9 @@
|
||||
defmodule Plausible.Repo.Migrations.AddLastPaymentDetails do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
alter table(:subscriptions) do
|
||||
add :last_bill_date, :date
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,13 @@
|
||||
defmodule Plausible.Billing.PlansTest do
|
||||
use Plausible.DataCase
|
||||
use Bamboo.Test, shared: true
|
||||
alias Plausible.Billing.Plans
|
||||
|
||||
test "suggested plan name" do
|
||||
assert Plans.suggested_plan_name(110_000) == "200k/mo"
|
||||
end
|
||||
|
||||
test "suggested plan cost" do
|
||||
assert Plans.suggested_plan_cost(110_000) == "$18/mo"
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,146 @@
|
||||
defmodule Plausible.Workers.CheckUsageTest do
|
||||
use Plausible.DataCase
|
||||
use Bamboo.Test
|
||||
import Double
|
||||
import Plausible.TestUtils
|
||||
alias Plausible.Workers.CheckUsage
|
||||
alias Plausible.Billing.Plans
|
||||
|
||||
setup [:create_user, :create_site]
|
||||
@paddle_id_10k Plans.plans()[:monthly][:"10k"][:product_id]
|
||||
|
||||
test "ignores user without subscription" do
|
||||
CheckUsage.perform(nil, nil)
|
||||
|
||||
assert_no_emails_delivered()
|
||||
end
|
||||
|
||||
test "ignores user with subscription but no usage", %{user: user} do
|
||||
insert(:subscription,
|
||||
user: user,
|
||||
paddle_plan_id: @paddle_id_10k,
|
||||
last_bill_date: Timex.shift(Timex.today(), days: -1)
|
||||
)
|
||||
|
||||
CheckUsage.perform(nil, nil)
|
||||
|
||||
assert_no_emails_delivered()
|
||||
end
|
||||
|
||||
test "does not send an email if account has been over the limit for one billing month", %{
|
||||
user: user
|
||||
} do
|
||||
billing_stub =
|
||||
stub(Plausible.Billing, :last_two_billing_months_usage, fn _user -> {9_000, 11_000} end)
|
||||
|
||||
insert(:subscription,
|
||||
user: user,
|
||||
paddle_plan_id: @paddle_id_10k,
|
||||
last_bill_date: Timex.shift(Timex.today(), days: -1)
|
||||
)
|
||||
|
||||
CheckUsage.perform(nil, nil, billing_stub)
|
||||
|
||||
assert_no_emails_delivered()
|
||||
end
|
||||
|
||||
test "sends an email when an account is over their limit for two consecutive billing months", %{
|
||||
user: user
|
||||
} do
|
||||
billing_stub =
|
||||
stub(Plausible.Billing, :last_two_billing_months_usage, fn _user -> {11_000, 11_000} end)
|
||||
|
||||
insert(:subscription,
|
||||
user: user,
|
||||
paddle_plan_id: @paddle_id_10k,
|
||||
last_bill_date: Timex.shift(Timex.today(), days: -1)
|
||||
)
|
||||
|
||||
CheckUsage.perform(nil, nil, billing_stub)
|
||||
|
||||
assert_email_delivered_with(
|
||||
# to: [user],
|
||||
subject: "You have outgrown your Plausible subscription tier "
|
||||
)
|
||||
end
|
||||
|
||||
describe "timing" do
|
||||
test "checks usage one day after the last_bill_date", %{
|
||||
user: user
|
||||
} do
|
||||
billing_stub =
|
||||
stub(Plausible.Billing, :last_two_billing_months_usage, fn _user -> {11_000, 11_000} end)
|
||||
|
||||
insert(:subscription,
|
||||
user: user,
|
||||
paddle_plan_id: @paddle_id_10k,
|
||||
last_bill_date: Timex.shift(Timex.today(), days: -1)
|
||||
)
|
||||
|
||||
CheckUsage.perform(nil, nil, billing_stub)
|
||||
|
||||
assert_email_delivered_with(
|
||||
# to: [user],
|
||||
subject: "You have outgrown your Plausible subscription tier "
|
||||
)
|
||||
end
|
||||
|
||||
test "for yearly subscriptions, does not check exactly one month after last_bill_date", %{
|
||||
user: user
|
||||
} do
|
||||
billing_stub =
|
||||
stub(Plausible.Billing, :last_two_billing_months_usage, fn _user -> {11_000, 11_000} end)
|
||||
|
||||
insert(:subscription,
|
||||
user: user,
|
||||
paddle_plan_id: @paddle_id_10k,
|
||||
last_bill_date: Timex.shift(Timex.today(), months: -1)
|
||||
)
|
||||
|
||||
CheckUsage.perform(nil, nil, billing_stub)
|
||||
|
||||
assert_no_emails_delivered()
|
||||
end
|
||||
|
||||
test "for yearly subscriptions, checks usage one month + one day after the last_bill_date", %{
|
||||
user: user
|
||||
} do
|
||||
billing_stub =
|
||||
stub(Plausible.Billing, :last_two_billing_months_usage, fn _user -> {11_000, 11_000} end)
|
||||
|
||||
insert(:subscription,
|
||||
user: user,
|
||||
paddle_plan_id: @paddle_id_10k,
|
||||
last_bill_date: Timex.shift(Timex.today(), months: -1, days: -1)
|
||||
)
|
||||
|
||||
CheckUsage.perform(nil, nil, billing_stub)
|
||||
|
||||
assert_email_delivered_with(
|
||||
# to: [user],
|
||||
subject: "You have outgrown your Plausible subscription tier "
|
||||
)
|
||||
end
|
||||
|
||||
test "for yearly subscriptions, checks usage multiple months + one day after the last_bill_date",
|
||||
%{
|
||||
user: user
|
||||
} do
|
||||
billing_stub =
|
||||
stub(Plausible.Billing, :last_two_billing_months_usage, fn _user -> {11_000, 11_000} end)
|
||||
|
||||
insert(:subscription,
|
||||
user: user,
|
||||
paddle_plan_id: @paddle_id_10k,
|
||||
last_bill_date: Timex.shift(Timex.today(), months: -2, days: -1)
|
||||
)
|
||||
|
||||
CheckUsage.perform(nil, nil, billing_stub)
|
||||
|
||||
assert_email_delivered_with(
|
||||
# to: [user],
|
||||
subject: "You have outgrown your Plausible subscription tier "
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue