diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex index 3ae8c5501..d0ff81c7e 100644 --- a/lib/plausible/billing/billing.ex +++ b/lib/plausible/billing/billing.ex @@ -211,7 +211,8 @@ defmodule Plausible.Billing do user_id: params["passthrough"], status: params["status"], next_bill_date: params["next_bill_date"], - next_bill_amount: params["unit_price"] || params["new_unit_price"] + next_bill_amount: params["unit_price"] || params["new_unit_price"], + currency_code: params["currency"] } end diff --git a/lib/plausible/billing/subscription.ex b/lib/plausible/billing/subscription.ex index 7a9d2d91e..806b217e4 100644 --- a/lib/plausible/billing/subscription.ex +++ b/lib/plausible/billing/subscription.ex @@ -10,7 +10,8 @@ defmodule Plausible.Billing.Subscription do :status, :next_bill_amount, :next_bill_date, - :user_id + :user_id, + :currency_code ] @optional_fields [:last_bill_date] @@ -25,6 +26,7 @@ defmodule Plausible.Billing.Subscription do field :next_bill_amount, :string field :next_bill_date, :date field :last_bill_date, :date + field :currency_code, :string belongs_to :user, Plausible.Auth.User diff --git a/lib/plausible_web/controllers/billing_controller.ex b/lib/plausible_web/controllers/billing_controller.ex index 81a4966f1..5de38f183 100644 --- a/lib/plausible_web/controllers/billing_controller.ex +++ b/lib/plausible_web/controllers/billing_controller.ex @@ -81,7 +81,6 @@ defmodule PlausibleWeb.BillingController do def upgrade(conn, _params) do usage = Plausible.Billing.usage(conn.assigns[:current_user]) today = Timex.today() - IO.inspect(Plausible.Billing.Plans.plans_for(conn.assigns[:current_user])) render(conn, "upgrade.html", usage: usage, @@ -96,8 +95,7 @@ defmodule PlausibleWeb.BillingController do if plan do cycle = if plan[:monthly_product_id] == plan_id, do: "monthly", else: "yearly" - cost = if cycle == "monthly", do: plan[:monthly_cost], else: plan[:yearly_cost] - plan = Map.merge(plan, %{cycle: cycle, cost: cost, product_id: plan_id}) + plan = Map.merge(plan, %{cycle: cycle, product_id: plan_id}) usage = Plausible.Billing.usage(conn.assigns[:current_user]) render(conn, "upgrade_to_plan.html", diff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.ex index 7d828a5d2..ab2eef03b 100644 --- a/lib/plausible_web/email.ex +++ b/lib/plausible_web/email.ex @@ -138,7 +138,8 @@ defmodule PlausibleWeb.Email do |> render("yearly_renewal_notification.html", %{ user: user, date: date, - next_bill_amount: user.subscription.next_bill_amount + next_bill_amount: user.subscription.next_bill_amount, + currency: user.subscription.currency_code }) end @@ -151,8 +152,7 @@ defmodule PlausibleWeb.Email do |> subject("Your Plausible subscription is about to expire") |> render("yearly_expiration_notification.html", %{ user: user, - date: date, - next_bill_amount: user.subscription.next_bill_amount + date: date }) end diff --git a/lib/plausible_web/templates/auth/user_settings.html.eex b/lib/plausible_web/templates/auth/user_settings.html.eex index c04fba616..6a9a38fea 100644 --- a/lib/plausible_web/templates/auth/user_settings.html.eex +++ b/lib/plausible_web/templates/auth/user_settings.html.eex @@ -50,7 +50,7 @@

Next bill amount

<%= if @subscription && @subscription.status in ["active", "past_due"] do %> -
$<%= @subscription.next_bill_amount %>
+
<%= PlausibleWeb.BillingView.present_currency(@subscription.currency_code) %><%= @subscription.next_bill_amount %>
<%= if @subscription.update_url do %> <%= link("Update billing info", to: @subscription.update_url, class: "text-sm text-indigo-500 font-medium") %> <% end %> diff --git a/lib/plausible_web/templates/billing/upgrade_to_plan.html.eex b/lib/plausible_web/templates/billing/upgrade_to_plan.html.eex index 14907c7fd..3adcd8902 100644 --- a/lib/plausible_web/templates/billing/upgrade_to_plan.html.eex +++ b/lib/plausible_web/templates/billing/upgrade_to_plan.html.eex @@ -10,7 +10,7 @@
- With this link you can upgrade to a plan with <%= PlausibleWeb.StatsView.large_number_format(@plan[:limit]) %> monthly pageviews. You will be billed <%= @plan[:cost] %> on a <%= @plan[:cycle] %> basis. + With this link you can upgrade to a plan with <%= PlausibleWeb.StatsView.large_number_format(@plan[:limit]) %> monthly pageviews, billed on a <%= @plan[:cycle] %> basis.
diff --git a/lib/plausible_web/templates/email/over_limit.html.eex b/lib/plausible_web/templates/email/over_limit.html.eex index e142a82ac..4fa5ba502 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 <%= @suggested_plan[:volume] %>/mo plan which runs at <%= @suggested_plan[:monthly_cost] %>/mo or <%= @suggested_plan[:yearly_cost] %>/yr when billed yearly. +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.

diff --git a/lib/plausible_web/templates/email/trial_upgrade_email.html.eex b/lib/plausible_web/templates/email/trial_upgrade_email.html.eex index a02a951ba..82f668aa3 100644 --- a/lib/plausible_web/templates/email/trial_upgrade_email.html.eex +++ b/lib/plausible_web/templates/email/trial_upgrade_email.html.eex @@ -4,7 +4,7 @@ Thanks for exploring Plausible, a simple and privacy-friendly alternative to Goo

In the last month, your account has used <%= PlausibleWeb.AuthView.delimit_integer(@usage) %> billable pageviews<%= if @custom_events > 0, do: " and custom events in total", else: "" %>. <%= if @usage <= 20_000_000 do %> -Based on that we recommend you select the <%= @suggested_plan[:volume] %>/mo plan which runs at <%= @suggested_plan[:monthly_cost] %>/mo or <%= @suggested_plan[:yearly_cost] %>/yr when billed yearly. +Based on that we recommend you select the <%= @suggested_plan[:volume] %>/mo plan.

<%= link("Upgrade now", to: "#{plausible_url()}/billing/upgrade") %>

diff --git a/lib/plausible_web/templates/email/yearly_renewal_notification.html.eex b/lib/plausible_web/templates/email/yearly_renewal_notification.html.eex index 322945cad..8d7e650b7 100644 --- a/lib/plausible_web/templates/email/yearly_renewal_notification.html.eex +++ b/lib/plausible_web/templates/email/yearly_renewal_notification.html.eex @@ -1,6 +1,6 @@ Hey <%= user_salutation(@user) %>,

-Time flies! This is a reminder that your annual subscription for Plausible Analytics is due to renew on <%= @date %>. We will automatically charge $<%= @next_bill_amount %> from your preferred billing method. +Time flies! This is a reminder that your annual subscription for Plausible Analytics is due to renew on <%= @date %>. We will automatically charge <%= PlausibleWeb.BillingView.present_currency(@currency) %><%= @next_bill_amount %> from your preferred billing method.

There's no action required if you're happy to continue using Plausible to count your website stats in a privacy-friendly way.

diff --git a/priv/repo/migrations/20210513091653_add_currency_to_subscription.exs b/priv/repo/migrations/20210513091653_add_currency_to_subscription.exs new file mode 100644 index 000000000..07393ea15 --- /dev/null +++ b/priv/repo/migrations/20210513091653_add_currency_to_subscription.exs @@ -0,0 +1,15 @@ +defmodule Plausible.Repo.Migrations.AddCurrencyToSubscription do + use Ecto.Migration + + def change do + alter table(:subscriptions) do + add :currency_code, :string + end + + execute "UPDATE subscriptions set currency_code='USD'" + + alter table(:subscriptions) do + modify :currency_code, :string, null: false + end + end +end diff --git a/test/plausible/billing/billing_test.exs b/test/plausible/billing/billing_test.exs index 0e4cc8cda..f20a6e796 100644 --- a/test/plausible/billing/billing_test.exs +++ b/test/plausible/billing/billing_test.exs @@ -181,13 +181,15 @@ defmodule Plausible.BillingTest do "passthrough" => user.id, "status" => "active", "next_bill_date" => "2019-06-01", - "unit_price" => "6.00" + "unit_price" => "6.00", + "currency" => "EUR" }) subscription = Repo.get_by(Plausible.Billing.Subscription, user_id: user.id) assert subscription.paddle_subscription_id == @subscription_id assert subscription.next_bill_date == ~D[2019-06-01] assert subscription.next_bill_amount == "6.00" + assert subscription.currency_code == "EUR" end test "create with email address" do @@ -203,7 +205,8 @@ defmodule Plausible.BillingTest do "cancel_url" => "cancel_url.com", "status" => "active", "next_bill_date" => "2019-06-01", - "unit_price" => "6.00" + "unit_price" => "6.00", + "currency" => "EUR" }) subscription = Repo.get_by(Plausible.Billing.Subscription, user_id: user.id) @@ -227,7 +230,8 @@ defmodule Plausible.BillingTest do "passthrough" => user.id, "status" => "active", "next_bill_date" => "2019-06-01", - "new_unit_price" => "12.00" + "new_unit_price" => "12.00", + "currency" => "EUR" }) subscription = Repo.get_by(Plausible.Billing.Subscription, user_id: user.id) diff --git a/test/support/factory.ex b/test/support/factory.ex index 35f33739b..34407d336 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -107,7 +107,8 @@ defmodule Plausible.Factory do update_url: "cancel.com", status: "active", next_bill_amount: "6.00", - next_bill_date: Timex.today() + next_bill_date: Timex.today(), + currency_code: "USD" } end diff --git a/test/workers/check_usage_test.exs b/test/workers/check_usage_test.exs index de338f653..f32256a4a 100644 --- a/test/workers/check_usage_test.exs +++ b/test/workers/check_usage_test.exs @@ -68,31 +68,6 @@ defmodule Plausible.Workers.CheckUsageTest do ) 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, billing_stub) - - assert_email_delivered_with( - to: [user], - html_body: - ~r/select the 100k\/mo plan which runs at \$12\/mo or \$120\/yr when billed yearly/ - ) - end - describe "timing" do test "checks usage one day after the last_bill_date", %{ user: user diff --git a/test/workers/send_trial_notifications_test.exs b/test/workers/send_trial_notifications_test.exs index 7548badc0..b014264ac 100644 --- a/test/workers/send_trial_notifications_test.exs +++ b/test/workers/send_trial_notifications_test.exs @@ -86,63 +86,63 @@ defmodule Plausible.Workers.SendTrialNotificationsTest do user = insert(:user) email = PlausibleWeb.Email.trial_upgrade_email(user, "today", {9_000, 0}) - assert email.html_body =~ "we recommend you select the 10k/mo plan which runs at $6/mo" + assert email.html_body =~ "we recommend you select the 10k/mo plan." end test "suggests 100k/mo plan" do user = insert(:user) email = PlausibleWeb.Email.trial_upgrade_email(user, "today", {90_000, 0}) - assert email.html_body =~ "we recommend you select the 100k/mo plan which runs at $12/mo" + assert email.html_body =~ "we recommend you select the 100k/mo plan." end test "suggests 200k/mo plan" do user = insert(:user) email = PlausibleWeb.Email.trial_upgrade_email(user, "today", {180_000, 0}) - assert email.html_body =~ "we recommend you select the 200k/mo plan which runs at $20/mo" + assert email.html_body =~ "we recommend you select the 200k/mo plan." end test "suggests 500k/mo plan" do user = insert(:user) email = PlausibleWeb.Email.trial_upgrade_email(user, "today", {450_000, 0}) - assert email.html_body =~ "we recommend you select the 500k/mo plan which runs at $30/mo" + assert email.html_body =~ "we recommend you select the 500k/mo plan." end test "suggests 1m/mo plan" do user = insert(:user) email = PlausibleWeb.Email.trial_upgrade_email(user, "today", {900_000, 0}) - assert email.html_body =~ "we recommend you select the 1M/mo plan which runs at $50/mo" + assert email.html_body =~ "we recommend you select the 1M/mo plan." end test "suggests 2m/mo plan" do user = insert(:user) email = PlausibleWeb.Email.trial_upgrade_email(user, "today", {1_800_000, 0}) - assert email.html_body =~ "we recommend you select the 2M/mo plan which runs at $70/mo" + assert email.html_body =~ "we recommend you select the 2M/mo plan." end test "suggests 5m/mo plan" do user = insert(:user) email = PlausibleWeb.Email.trial_upgrade_email(user, "today", {4_500_000, 0}) - assert email.html_body =~ "we recommend you select the 5M/mo plan which runs at $100/mo" + assert email.html_body =~ "we recommend you select the 5M/mo plan." end test "suggests 10m/mo plan" do user = insert(:user) email = PlausibleWeb.Email.trial_upgrade_email(user, "today", {9_000_000, 0}) - assert email.html_body =~ "we recommend you select the 10M/mo plan which runs at $150/mo" + assert email.html_body =~ "we recommend you select the 10M/mo plan." end test "suggests 20m/mo plan" do user = insert(:user) email = PlausibleWeb.Email.trial_upgrade_email(user, "today", {19_000_000, 0}) - assert email.html_body =~ "we recommend you select the 20M/mo plan which runs at $225/mo" + assert email.html_body =~ "we recommend you select the 20M/mo plan." end test "does not suggest a plan above that" do