From d1ef27ec9ce276b2aded0409450c9cb84e187c4e Mon Sep 17 00:00:00 2001
From: Uku Taht <uku.taht@gmail.com>
Date: Fri, 7 May 2021 11:10:18 +0300
Subject: [PATCH] Make sure upgrading to unlisted plans works OK

---
 lib/plausible/billing/plans.ex                      | 4 ++--
 lib/plausible_web/controllers/billing_controller.ex | 3 +++
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/lib/plausible/billing/plans.ex b/lib/plausible/billing/plans.ex
index 619c2a9a..714b2947 100644
--- a/lib/plausible/billing/plans.ex
+++ b/lib/plausible/billing/plans.ex
@@ -72,7 +72,7 @@ defmodule Plausible.Billing.Plans do
     }
   ]
 
-  @yearly_plans_v1 [
+  @unlisted_plans_v1 [
     %{limit: 150_000_000, yearly_product_id: "648089", yearly_cost: "$4800"}
   ]
 
@@ -85,7 +85,7 @@ defmodule Plausible.Billing.Plans do
   end
 
   def for_product_id(product_id) do
-    Enum.find(@plans_v1, fn plan ->
+    Enum.find(@plans_v1 ++ @unlisted_plans_v1, fn plan ->
       product_id in [plan[:monthly_product_id], plan[:yearly_product_id]]
     end)
   end
diff --git a/lib/plausible_web/controllers/billing_controller.ex b/lib/plausible_web/controllers/billing_controller.ex
index 4f31637d..52660ff2 100644
--- a/lib/plausible_web/controllers/billing_controller.ex
+++ b/lib/plausible_web/controllers/billing_controller.ex
@@ -94,6 +94,9 @@ defmodule PlausibleWeb.BillingController do
     plan = Plausible.Billing.Plans.for_product_id(plan_id)
 
     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})
       usage = Plausible.Billing.usage(conn.assigns[:current_user])
 
       render(conn, "upgrade_to_plan.html",
-- 
GitLab