From f16666e374bb5578314f1ea41816fc6f8345f343 Mon Sep 17 00:00:00 2001
From: Uku Taht <uku.taht@gmail.com>
Date: Thu, 13 May 2021 12:42:01 +0300
Subject: [PATCH] Localize billing screens

---
 lib/plausible/billing/billing.ex              |  3 ++-
 lib/plausible/billing/subscription.ex         |  4 ++-
 .../controllers/billing_controller.ex         |  4 +--
 lib/plausible_web/email.ex                    |  6 ++---
 .../templates/auth/user_settings.html.eex     |  2 +-
 .../billing/upgrade_to_plan.html.eex          |  2 +-
 .../templates/email/over_limit.html.eex       |  2 +-
 .../email/trial_upgrade_email.html.eex        |  2 +-
 .../yearly_renewal_notification.html.eex      |  2 +-
 ...513091653_add_currency_to_subscription.exs | 15 +++++++++++
 test/plausible/billing/billing_test.exs       | 10 +++++---
 test/support/factory.ex                       |  3 ++-
 test/workers/check_usage_test.exs             | 25 -------------------
 .../workers/send_trial_notifications_test.exs | 18 ++++++-------
 14 files changed, 47 insertions(+), 51 deletions(-)
 create mode 100644 priv/repo/migrations/20210513091653_add_currency_to_subscription.exs

diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex
index 3ae8c550..d0ff81c7 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 7a9d2d91..806b217e 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 81a4966f..5de38f18 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 7d828a5d..ab2eef03 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 c04fba61..6a9a38fe 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 @@
     <div class="h-32 px-2 py-4 my-4 text-center bg-gray-100 rounded dark:bg-gray-900" style="width: 11.75rem;">
       <h4 class="font-black dark:text-gray-100">Next bill amount</h4>
       <%= if @subscription && @subscription.status in ["active", "past_due"] do %>
-        <div class="py-2 text-xl font-medium dark:text-gray-100">$<%= @subscription.next_bill_amount %></div>
+        <div class="py-2 text-xl font-medium dark:text-gray-100"><%= PlausibleWeb.BillingView.present_currency(@subscription.currency_code) %><%= @subscription.next_bill_amount %></div>
         <%= 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 14907c7f..3adcd890 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 @@
       </div>
 
       <div class="w-full py-4 dark:text-gray-100">
-        <span>With this link you can upgrade to a plan with <b><%= PlausibleWeb.StatsView.large_number_format(@plan[:limit]) %> monthly pageviews</b></span>. You will be billed <%= @plan[:cost] %> on a <%= @plan[:cycle] %> basis.
+        <span>With this link you can upgrade to a plan with <b><%= PlausibleWeb.StatsView.large_number_format(@plan[:limit]) %> monthly pageviews</b></span>, billed on a <%= @plan[:cycle] %> basis.
       </div>
 
       <div class="mt-6 text-left">
diff --git a/lib/plausible_web/templates/email/over_limit.html.eex b/lib/plausible_web/templates/email/over_limit.html.eex
index e142a82a..4fa5ba50 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
 <br /><br />
 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.
 <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 />
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 a02a951b..82f668aa 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
 <br /><br />
 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.
 <br /><br />
 <%= link("Upgrade now", to: "#{plausible_url()}/billing/upgrade") %>
 <br /><br />
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 322945ca..8d7e650b 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) %>,
 <br /><br />
-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.
 <br /><br />
 There's no action required if you're happy to continue using Plausible to count your website stats in a privacy-friendly way.
 <br /><br />
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 00000000..07393ea1
--- /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 0e4cc8cd..f20a6e79 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 35f33739..34407d33 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 de338f65..f32256a4 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 7548badc..b014264a 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
-- 
GitLab