From 9db24f54d5c70a85e1aae1cc5e462728e1005b3d Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Mon, 1 Mar 2021 10:11:49 +0200 Subject: [PATCH] Enable over limit notifications --- lib/plausible/billing/plans.ex | 35 ++++++++++++++----- lib/plausible_web/email.ex | 17 ++------- .../templates/email/over_limit.html.eex | 2 +- test/workers/check_usage_test.exs | 32 ++++++++++++++--- 4 files changed, 57 insertions(+), 29 deletions(-) diff --git a/lib/plausible/billing/plans.ex b/lib/plausible/billing/plans.ex index 2e567083f..743ac9be4 100644 --- a/lib/plausible/billing/plans.ex +++ b/lib/plausible/billing/plans.ex @@ -12,15 +12,27 @@ defmodule Plausible.Billing.Plans do ] @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"} + %{product_id: "572810", cost: "$48", monthly_cost: "$4", limit: 10_000, cycle: "yearly"}, + %{product_id: "590752", cost: "$96", monthly_cost: "$8", limit: 100_000, cycle: "yearly"}, + %{product_id: "597486", cost: "$144", monthly_cost: "$12", limit: 200_000, cycle: "yearly"}, + %{product_id: "597488", cost: "$216", monthly_cost: "$18", limit: 500_000, cycle: "yearly"}, + %{product_id: "597643", cost: "$384", monthly_cost: "$32", limit: 1_000_000, cycle: "yearly"}, + %{product_id: "597310", cost: "$552", monthly_cost: "$46", limit: 2_000_000, cycle: "yearly"}, + %{product_id: "597312", cost: "$792", monthly_cost: "$66", limit: 5_000_000, cycle: "yearly"}, + %{ + product_id: "642354", + cost: "$1200", + monthly_cost: "$100", + limit: 10_000_000, + cycle: "yearly" + }, + %{ + product_id: "642356", + cost: "$1800", + monthly_cost: "$150", + limit: 20_000_000, + cycle: "yearly" + } ] @all_plans @monthly_plans ++ @yearly_plans @@ -52,6 +64,11 @@ defmodule Plausible.Billing.Plans do plan[:cost] <> "/mo" end + def suggested_plan_cost_yearly(usage) do + plan = Enum.find(@yearly_plans, fn plan -> usage < plan[:limit] end) + plan[:monthly_cost] <> "/mo" + end + defp suggested_plan(usage) do Enum.find(@monthly_plans, fn plan -> usage < plan[:limit] end) end diff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.ex index 44244dc13..6df1d1db9 100644 --- a/lib/plausible_web/email.ex +++ b/lib/plausible_web/email.ex @@ -113,23 +113,10 @@ defmodule PlausibleWeb.Email do end def over_limit_email(user, usage, last_cycle) do - yearly_plan_ids = - Plausible.Billing.Plans.plans() - |> Map.fetch!(:yearly) - |> Enum.map(fn {_limit, plan} -> plan[:product_id] end) - - subject_pre = - if user.subscription.paddle_plan_id in yearly_plan_ids do - "[YEARLY] " - else - "" - end - base_email() - # Temporary testing - |> to(["uku@plausible.io"]) + |> to(user) |> tag("over-limit") - |> subject("#{subject_pre}You have outgrown your Plausible subscription tier ") + |> subject("You have outgrown your Plausible subscription tier ") |> render("over_limit.html", %{ user: user, usage: usage, diff --git a/lib/plausible_web/templates/email/over_limit.html.eex b/lib/plausible_web/templates/email/over_limit.html.eex index 3d8466ad6..d03389a25 100644 --- a/lib/plausible_web/templates/email/over_limit.html.eex +++ b/lib/plausible_web/templates/email/over_limit.html.eex @@ -8,7 +8,7 @@ We don't enforce any hard limits at the moment, we're still counting your stats

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 <%= 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. +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) %> or <%= Plausible.Billing.Plans.suggested_plan_cost_yearly(@usage) %> when billed yearly.

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.

diff --git a/test/workers/check_usage_test.exs b/test/workers/check_usage_test.exs index 42f69eaf4..862272d42 100644 --- a/test/workers/check_usage_test.exs +++ b/test/workers/check_usage_test.exs @@ -64,11 +64,35 @@ defmodule Plausible.Workers.CheckUsageTest do CheckUsage.perform(nil, nil, billing_stub) assert_email_delivered_with( - # to: [user], + to: [user], subject: "You have outgrown your Plausible subscription tier " ) end + test "includes both monthly and yearly price", %{ + user: user + } do + billing_stub = + Plausible.Billing + |> stub(:last_two_billing_months_usage, fn _user -> {11_000, 11_000} end) + |> stub(:last_two_billing_cycles, fn _user -> + {Date.range(Timex.today(), Timex.today()), Date.range(Timex.today(), Timex.today())} + 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], + html_body: ~r/select the 100k\/mo plan which runs at \$12\/mo or \$8\/mo when billed yearly/ + ) + end + describe "timing" do test "checks usage one day after the last_bill_date", %{ user: user @@ -89,7 +113,7 @@ defmodule Plausible.Workers.CheckUsageTest do CheckUsage.perform(nil, nil, billing_stub) assert_email_delivered_with( - # to: [user], + to: [user], subject: "You have outgrown your Plausible subscription tier " ) end @@ -134,7 +158,7 @@ defmodule Plausible.Workers.CheckUsageTest do CheckUsage.perform(nil, nil, billing_stub) assert_email_delivered_with( - # to: [user], + to: [user], subject: "You have outgrown your Plausible subscription tier " ) end @@ -159,7 +183,7 @@ defmodule Plausible.Workers.CheckUsageTest do CheckUsage.perform(nil, nil, billing_stub) assert_email_delivered_with( - # to: [user], + to: [user], subject: "You have outgrown your Plausible subscription tier " ) end