From a8cb187e8db96889eabae9036b34a8a89ff6cf81 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Fri, 12 Feb 2021 10:17:53 +0200 Subject: [PATCH] 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 notifications --- lib/plausible/billing/billing.ex | 7 +- lib/plausible/billing/plans.ex | 93 ++++++----- lib/plausible/billing/subscription.ex | 5 +- lib/plausible_web/email.ex | 12 ++ .../templates/billing/upgrade.html.eex | 2 +- .../templates/email/over_limit.html.eex | 32 ++++ .../email/trial_upgrade_email.html.eex | 4 +- lib/plausible_web/views/email_view.ex | 56 ------- lib/workers/check_usage.ex | 61 ++++++++ ...0210209095257_add_last_payment_details.exs | 9 ++ test/plausible/billing/billing_test.exs | 6 +- test/plausible/billing/plans_test.exs | 13 ++ test/workers/check_usage_test.exs | 146 ++++++++++++++++++ .../workers/send_trial_notifications_test.exs | 16 +- 14 files changed, 363 insertions(+), 99 deletions(-) create mode 100644 lib/plausible_web/templates/email/over_limit.html.eex create mode 100644 lib/workers/check_usage.ex create mode 100644 priv/repo/migrations/20210209095257_add_last_payment_details.exs create mode 100644 test/plausible/billing/plans_test.exs create mode 100644 test/workers/check_usage_test.exs diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex index c637d65ca..2127c906e 100644 --- a/lib/plausible/billing/billing.ex +++ b/lib/plausible/billing/billing.ex @@ -65,7 +65,8 @@ defmodule Plausible.Billing do changeset = Subscription.changeset(subscription, %{ next_bill_amount: amount, - next_bill_date: api_subscription["next_payment"]["date"] + next_bill_date: api_subscription["next_payment"]["date"], + last_bill_date: params["event_time"] }) Repo.update(changeset) @@ -133,6 +134,10 @@ defmodule Plausible.Billing do pageviews + custom_events end + def last_two_billing_months_usage(_user) do + {1, 2} + end + def usage_breakdown(user) do user = Repo.preload(user, :sites) diff --git a/lib/plausible/billing/plans.ex b/lib/plausible/billing/plans.ex index ea5765e59..2e567083f 100644 --- a/lib/plausible/billing/plans.ex +++ b/lib/plausible/billing/plans.ex @@ -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 diff --git a/lib/plausible/billing/subscription.ex b/lib/plausible/billing/subscription.ex index d8ca66a96..7a9d2d91e 100644 --- a/lib/plausible/billing/subscription.ex +++ b/lib/plausible/billing/subscription.ex @@ -12,6 +12,8 @@ defmodule Plausible.Billing.Subscription do :next_bill_date, :user_id ] + + @optional_fields [:last_bill_date] @valid_statuses ["active", "past_due", "deleted", "paused"] schema "subscriptions" do @@ -22,6 +24,7 @@ defmodule Plausible.Billing.Subscription do field :status, :string field :next_bill_amount, :string field :next_bill_date, :date + field :last_bill_date, :date belongs_to :user, Plausible.Auth.User @@ -30,7 +33,7 @@ defmodule Plausible.Billing.Subscription do def changeset(model, attrs \\ %{}) do model - |> cast(attrs, @required_fields) + |> cast(attrs, @required_fields ++ @optional_fields) |> validate_required(@required_fields) |> validate_inclusion(:status, @valid_statuses) |> unique_constraint(:paddle_subscription_id) diff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.ex index 1f002f2a7..54f6056e8 100644 --- a/lib/plausible_web/email.ex +++ b/lib/plausible_web/email.ex @@ -112,6 +112,18 @@ defmodule PlausibleWeb.Email do }) end + def over_limit_email(user, usage) do + base_email() + # Temporary testing + |> to(["uku@plausible.io", "marko@plausible.io"]) + |> tag("over-limit") + |> subject("You have outgrown your Plausible subscription tier ") + |> render("over_limit.html", %{ + user: user, + usage: usage + }) + end + def cancellation_email(user) do base_email() |> to(user.email) diff --git a/lib/plausible_web/templates/billing/upgrade.html.eex b/lib/plausible_web/templates/billing/upgrade.html.eex index 52e489ce2..d12ac3dd2 100644 --- a/lib/plausible_web/templates/billing/upgrade.html.eex +++ b/lib/plausible_web/templates/billing/upgrade.html.eex @@ -60,7 +60,7 @@
-
Due today: $6
+
Due today: $6