From f00457c5d14ba91bec5d2b9becbbe47449c4ddea Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Tue, 29 Mar 2022 13:00:25 +0300 Subject: [PATCH] Change enterprise plans --- lib/plausible/billing/paddle_api.ex | 16 +++- lib/plausible/billing/plans.ex | 16 +++- .../controllers/billing_controller.ex | 57 +++++--------- .../billing/change_enterprise_plan.html.eex | 2 +- .../billing/upgrade_enterprise_plan.html.eex | 43 ----------- .../billing/upgrade_to_plan.html.eex | 76 ++++++++++++------- 6 files changed, 100 insertions(+), 110 deletions(-) delete mode 100644 lib/plausible_web/templates/billing/upgrade_enterprise_plan.html.eex diff --git a/lib/plausible/billing/paddle_api.ex b/lib/plausible/billing/paddle_api.ex index 42a2742fa..a23487292 100644 --- a/lib/plausible/billing/paddle_api.ex +++ b/lib/plausible/billing/paddle_api.ex @@ -1,5 +1,4 @@ defmodule Plausible.Billing.PaddleApi do - @update_preview_endpoint "https://vendors.paddle.com/api/2.0/subscription/preview_update" @update_endpoint "https://vendors.paddle.com/api/2.0/subscription/users/update" @get_endpoint "https://vendors.paddle.com/api/2.0/subscription/users" @headers [ @@ -21,7 +20,13 @@ defmodule Plausible.Billing.PaddleApi do quantity: 1 } - {:ok, response} = HTTPoison.post(@update_preview_endpoint, Jason.encode!(params), @headers) + {:ok, response} = + HTTPoison.post( + vendors_domain() <> "/api/2.0/subscription/preview_update", + Jason.encode!(params), + @headers + ) + body = Jason.decode!(response.body) if body["success"] do @@ -125,6 +130,13 @@ defmodule Plausible.Billing.PaddleApi do end end + def vendors_domain() do + case Application.get_env(:plausible, :environment) do + "dev" -> "https://sandbox-vendors.paddle.com" + _ -> "https://vendors.paddle.com" + end + end + defp get_config() do Application.get_env(:plausible, :paddle) end diff --git a/lib/plausible/billing/plans.ex b/lib/plausible/billing/plans.ex index e156697f7..40ac2a218 100644 --- a/lib/plausible/billing/plans.ex +++ b/lib/plausible/billing/plans.ex @@ -97,7 +97,10 @@ defmodule Plausible.Billing.Plans do Map.fetch!(found, :limit) else enterprise_plan = - Repo.get_by(Plausible.Billing.EnterprisePlan, user_id: subscription.user_id) + Repo.get_by(Plausible.Billing.EnterprisePlan, + user_id: subscription.user_id, + paddle_plan_id: subscription.paddle_plan_id + ) if enterprise_plan do enterprise_plan.monthly_pageview_limit @@ -111,6 +114,17 @@ defmodule Plausible.Billing.Plans do end end + def get_enterprise_plan(user) do + if user.subscription do + Repo.get_by(Plausible.Billing.EnterprisePlan, + user_id: user.id, + paddle_plan_id: user.subscription.paddle_plan_id + ) + else + Repo.get_by(Plausible.Billing.EnterprisePlan, user_id: user.id) + end + end + def suggested_plan(user, usage) do Enum.find(plans_for(user), fn plan -> usage < plan[:limit] end) end diff --git a/lib/plausible_web/controllers/billing_controller.ex b/lib/plausible_web/controllers/billing_controller.ex index a057678b1..012a327e1 100644 --- a/lib/plausible_web/controllers/billing_controller.ex +++ b/lib/plausible_web/controllers/billing_controller.ex @@ -30,35 +30,16 @@ defmodule PlausibleWeb.BillingController do end def upgrade_enterprise_plan(conn, %{"plan_id" => plan_id}) do - user = - conn.assigns[:current_user] - |> Repo.preload(:enterprise_plan) - - if user.enterprise_plan && user.enterprise_plan.id == String.to_integer(plan_id) do - usage = Plausible.Billing.usage(conn.assigns[:current_user]) - - render(conn, "upgrade_to_plan.html", - usage: usage, - user: user, - layout: {PlausibleWeb.LayoutView, "focus.html"} - ) - else - render_error(conn, 404) - end - end - - def upgrade_to_plan(conn, %{"plan_id" => plan_id}) do - plan = Plausible.Billing.Plans.for_product_id(plan_id) + user = conn.assigns[:current_user] + plan = Repo.get_by(Plausible.Billing.EnterprisePlan, user_id: user.id, id: plan_id) if plan do - cycle = if plan[:monthly_product_id] == plan_id, do: "monthly", else: "yearly" - plan = Map.merge(plan, %{cycle: cycle, product_id: plan_id}) usage = Plausible.Billing.usage(conn.assigns[:current_user]) render(conn, "upgrade_to_plan.html", usage: usage, + user: user, plan: plan, - user: conn.assigns[:current_user], layout: {PlausibleWeb.LayoutView, "focus.html"} ) else @@ -79,8 +60,10 @@ defmodule PlausibleWeb.BillingController do cond do subscription && user.enterprise_plan -> - redirect(conn, - to: Routes.billing_path(conn, :change_enterprise_plan, user.enterprise_plan.id) + render(conn, "change_enterprise_plan_contact_us.html", + user: user, + plan: user.enterprise_plan, + layout: {PlausibleWeb.LayoutView, "focus.html"} ) subscription -> @@ -95,31 +78,30 @@ defmodule PlausibleWeb.BillingController do end def change_enterprise_plan(conn, %{"plan_id" => plan_id}) do - user = - conn.assigns[:current_user] - |> Repo.preload(:enterprise_plan) + user = conn.assigns[:current_user] + + current_plan = + Repo.get_by(Plausible.Billing.EnterprisePlan, + user_id: user.id, + paddle_plan_id: user.subscription.paddle_plan_id + ) + + new_plan = Repo.get_by(Plausible.Billing.EnterprisePlan, user_id: user.id, id: plan_id) cond do is_nil(user.subscription) -> redirect(conn, to: "/billing/upgrade") - is_nil(user.enterprise_plan) -> + is_nil(current_plan) -> render_error(conn, 404) - user.enterprise_plan.id !== String.to_integer(plan_id) -> + is_nil(new_plan) || new_plan.paddle_plan_id == user.subscription.paddle_plan_id -> render_error(conn, 404) - user.enterprise_plan.paddle_plan_id == user.subscription.paddle_plan_id -> - render(conn, "change_enterprise_plan_contact_us.html", - user: user, - plan: user.enterprise_plan, - layout: {PlausibleWeb.LayoutView, "focus.html"} - ) - true -> render(conn, "change_enterprise_plan.html", user: user, - plan: user.enterprise_plan, + plan: new_plan, layout: {PlausibleWeb.LayoutView, "focus.html"} ) end @@ -127,6 +109,7 @@ defmodule PlausibleWeb.BillingController do def change_plan_preview(conn, %{"plan_id" => new_plan_id}) do subscription = Billing.active_subscription_for(conn.assigns[:current_user].id) + IO.inspect(subscription) if subscription do {:ok, preview_info} = Billing.change_plan_preview(subscription, new_plan_id) diff --git a/lib/plausible_web/templates/billing/change_enterprise_plan.html.eex b/lib/plausible_web/templates/billing/change_enterprise_plan.html.eex index 3c1e452c7..70182fd7a 100644 --- a/lib/plausible_web/templates/billing/change_enterprise_plan.html.eex +++ b/lib/plausible_web/templates/billing/change_enterprise_plan.html.eex @@ -17,7 +17,7 @@ return currency + this.localizedPlan.price.net }, fetchPlan() { - fetchJsonp('https://checkout.paddle.com/api/2.0/prices?product_ids=<%= @plan.paddle_plan_id %>') + fetchJsonp('<%= Plausible.Billing.PaddleApi.checkout_domain() %>/api/2.0/prices?product_ids=<%= @plan.paddle_plan_id %>') .then(res => res.json()) .then((data) => { this.localizedPlan = data.response.products[0] diff --git a/lib/plausible_web/templates/billing/upgrade_enterprise_plan.html.eex b/lib/plausible_web/templates/billing/upgrade_enterprise_plan.html.eex deleted file mode 100644 index 3604ce423..000000000 --- a/lib/plausible_web/templates/billing/upgrade_enterprise_plan.html.eex +++ /dev/null @@ -1,43 +0,0 @@ -
-

Upgrade your free trial

-
- -
-
-
-
- You've used <%= PlausibleWeb.AuthView.delimit_integer(@usage) %> billable pageviews in the last 30 days -
- -
- With this link you can upgrade to an enterprise plan with <%= PlausibleWeb.StatsView.large_number_format(@plan[:limit]) %> monthly pageviews, billed on a <%= @plan[:cycle] %> basis. -
- -
- - - -
-
- -
-

- What happens if I go over my page views limit? -

-
- You will never be charged extra for an occasional traffic spike. There are no surprise fees and your card will never be charged unexpectedly.

- If your page views exceed your plan for two consecutive months, we will contact you to upgrade to a higher plan for the following month. You will have two weeks to make a decision. You can decide to continue with a higher plan or to cancel your account at that point. -
- -
-
-
- -
- Questions? Contact <%= link("support@plausible.io", to: "mailto:support@plausible.io", class: "text-indigo-500") %> -
- -<%= render("_paddle_script.html") %> 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 65f6bfdbe..bb36983f4 100644 --- a/lib/plausible_web/templates/billing/upgrade_to_plan.html.eex +++ b/lib/plausible_web/templates/billing/upgrade_to_plan.html.eex @@ -1,37 +1,61 @@ + + +

Upgrade your free trial

-
-
-
-
- You've used <%= PlausibleWeb.AuthView.delimit_integer(@usage) %> billable pageviews in the last 30 days -
+
+
+
+ +
+
@@ -40,4 +64,4 @@ Questions? Contact <%= link("support@plausible.io", to: "mailto:support@plausible.io", class: "text-indigo-500") %>
-<%= render("_paddle_script.html") %> \ No newline at end of file +<%= render("_paddle_script.html") %>