From 9476ccf95cb57fb49d2663fcfbe537b7da144385 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Tue, 21 Apr 2020 14:11:38 +0300 Subject: [PATCH] Yearly billing (#58) * Add yearly billing options * Add new subscription names * Add note about yearly billing in the email * Add a test for change_plan function * Add allowance function back in * Test billing controller endpoint --- config/config.exs | 4 + lib/plausible/billing/billing.ex | 18 ++- lib/plausible/billing/paddle_api.ex | 27 +++- lib/plausible/billing/plans.ex | 60 +++---- .../controllers/billing_controller.ex | 18 ++- lib/plausible_web/router.ex | 3 +- .../billing/_checkout_button.html.eex | 1 - .../templates/billing/change_plan.html.eex | 144 +++++++++++------ .../billing/change_plan_preview.html.eex | 86 +++++++++++ .../templates/billing/upgrade.html.eex | 146 ++++++++++++------ .../email/trial_upgrade_email.html.eex | 1 + lib/plausible_web/views/auth_view.ex | 15 +- lib/plausible_web/views/billing_view.ex | 5 + test/plausible/billing/billing_test.exs | 14 ++ .../controllers/billing_controller_test.exs | 9 +- test/support/paddle_api_mock.ex | 12 ++ 16 files changed, 405 insertions(+), 158 deletions(-) delete mode 100644 lib/plausible_web/templates/billing/_checkout_button.html.eex create mode 100644 lib/plausible_web/templates/billing/change_plan_preview.html.eex diff --git a/config/config.exs b/config/config.exs index ac51e693d..a8695a37e 100644 --- a/config/config.exs +++ b/config/config.exs @@ -48,6 +48,10 @@ config :plausible, config :plausible, session_timeout: 1000 * 60 * 30 # 30 minutes +config :plausible, :paddle, + vendor_id: "49430", + vendor_auth_code: System.get_env("PADDLE_VENDOR_AUTH_CODE") + # Import environment specific config. This must remain at the bottom # of this file so it overrides the configuration defined above. import_config "#{Mix.env()}.exs" diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex index 2487826de..b9c01cf70 100644 --- a/lib/plausible/billing/billing.ex +++ b/lib/plausible/billing/billing.ex @@ -1,6 +1,6 @@ defmodule Plausible.Billing do use Plausible.Repo - alias Plausible.Billing.{Subscription, Plans, PaddleApi} + alias Plausible.Billing.{Subscription, PaddleApi} @paddle_api Application.fetch_env!(:plausible, :paddle_api) def active_subscription_for(user_id) do @@ -59,22 +59,30 @@ defmodule Plausible.Billing do end end - def change_plan(user, new_plan) do + def change_plan(user, new_plan_id) do subscription = active_subscription_for(user.id) - res = PaddleApi.update_subscription(subscription.paddle_subscription_id, %{ - plan_id: Plans.paddle_id_for_plan(new_plan) + res = @paddle_api.update_subscription(subscription.paddle_subscription_id, %{ + plan_id: new_plan_id }) case res do {:ok, response} -> + amount = :erlang.float_to_binary(response["next_payment"]["amount"] / 1, decimals: 2) + Subscription.changeset(subscription, %{ - paddle_plan_id: Integer.to_string(response["plan_id"]) + paddle_plan_id: Integer.to_string(response["plan_id"]), + next_bill_amount: amount, + next_bill_date: response["next_payment"]["date"], }) |> Repo.update e -> e end end + def change_plan_preview(subscription, new_plan_id) do + PaddleApi.update_subscription_preview(subscription.paddle_subscription_id, new_plan_id) + end + def needs_to_upgrade?(user) do if Timex.before?(user.trial_expiry_date, Timex.today()) do !active_subscription_for(user.id) diff --git a/lib/plausible/billing/paddle_api.ex b/lib/plausible/billing/paddle_api.ex index a619ef548..941c3f16e 100644 --- a/lib/plausible/billing/paddle_api.ex +++ b/lib/plausible/billing/paddle_api.ex @@ -1,13 +1,36 @@ 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" - @vendor_id "49430" - @vendor_auth_code System.get_env("PADDLE_VENDOR_AUTH_CODE") + @vendor_id Keyword.fetch!(Application.get_env(:plausible, :paddle), :vendor_id) + @vendor_auth_code Keyword.fetch!(Application.get_env(:plausible, :paddle), :vendor_auth_code) @headers [ {"Content-type", "application/json"}, {"Accept", "application/json"} ] + def update_subscription_preview(paddle_subscription_id, new_plan_id) do + params = %{ + vendor_id: @vendor_id, + vendor_auth_code: @vendor_auth_code, + subscription_id: paddle_subscription_id, + plan_id: new_plan_id, + prorate: true, + keep_modifiers: true, + bill_immediately: true, + quantity: 1 + } + + {:ok, response} = HTTPoison.post(@update_preview_endpoint, Poison.encode!(params), @headers) + body = Poison.decode!(response.body) + + if body["success"] do + {:ok, body["response"]} + else + {:error, body["error"]} + end + end + def update_subscription(paddle_subscription_id, params) do params = Map.merge(params, %{ vendor_id: @vendor_id, diff --git a/lib/plausible/billing/plans.ex b/lib/plausible/billing/plans.ex index 50589a399..9e10dd385 100644 --- a/lib/plausible/billing/plans.ex +++ b/lib/plausible/billing/plans.ex @@ -1,44 +1,32 @@ defmodule Plausible.Billing.Plans do - @app_env System.get_env("APP_ENV") || "dev" - - @real_plans %{ - personal: "558018", - startup: "558745", - business: "558746" - } - - @test_plans %{ - personal: "558156", - startup: "558199", - business: "558200" + @plans %{ + monthly: %{ + "10k": %{product_id: "558018", due_now: "$6"}, + "100k": %{product_id: "558745", due_now: "$12"}, + "1m": %{product_id: "558746", due_now: "$36"}, + }, + yearly: %{ + "10k": %{product_id: "572810", due_now: "$48"}, + "100k": %{product_id: "590752", due_now: "$96"}, + "1m": %{product_id: "590753", due_now: "$288"} + }, } - def paddle_id_for_plan(plan) do - if @app_env == "prod" do - @real_plans[plan] - else - @test_plans[plan] - end - end - - def is?(subscription, plan) do - paddle_id_for_plan(plan) == subscription.paddle_plan_id + def plans do + @plans end def allowance(subscription) do - cond do - subscription.paddle_plan_id == "572810" -> # Personal annual - 10_000 - subscription.paddle_plan_id == "free_10k" -> - 10_000 - is?(subscription, :personal) -> - 10_000 - is?(subscription, :startup) -> - 100_000 - is?(subscription, :business) -> - 1_000_000 - true -> - raise "Subscription not found for #{subscription.paddle_plan_id}" - end + 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, + } + + allowed_volume[subscription.paddle_plan_id] end end diff --git a/lib/plausible_web/controllers/billing_controller.ex b/lib/plausible_web/controllers/billing_controller.ex index e93408f06..a29d30482 100644 --- a/lib/plausible_web/controllers/billing_controller.ex +++ b/lib/plausible_web/controllers/billing_controller.ex @@ -8,6 +8,7 @@ defmodule PlausibleWeb.BillingController do def change_plan_form(conn, _params) do subscription = Billing.active_subscription_for(conn.assigns[:current_user].id) + if subscription do render(conn, "change_plan.html", subscription: subscription, layout: {PlausibleWeb.LayoutView, "focus.html"}) else @@ -15,16 +16,25 @@ defmodule PlausibleWeb.BillingController do end end - def change_plan(conn, %{"plan_name" => plan}) when plan in ["personal", "startup", "business"] do - new_plan = String.to_existing_atom(plan) + def change_plan_preview(conn, %{"plan_id" => new_plan_id}) do + subscription = Billing.active_subscription_for(conn.assigns[:current_user].id) + + if subscription do + {:ok, preview_info} = Billing.change_plan_preview(subscription, new_plan_id) + render(conn, "change_plan_preview.html", subscription: subscription, preview_info: preview_info, layout: {PlausibleWeb.LayoutView, "focus.html"}) + else + redirect(conn, to: "/billing/upgrade") + end + end - case Billing.change_plan(conn.assigns[:current_user], new_plan) do + def change_plan(conn, %{"new_plan_id" => new_plan_id}) do + case Billing.change_plan(conn.assigns[:current_user], new_plan_id) do {:ok, _subscription} -> conn |> put_flash(:success, "Plan changed successfully") |> redirect(to: "/settings") {:error, e} -> - Sentry.capture_message("Error changing plans", extra: %{errors: inspect(e), new_plan: new_plan, user_id: conn.assigns[:current_user].id}) + Sentry.capture_message("Error changing plans", extra: %{errors: inspect(e), new_plan_id: new_plan_id, user_id: conn.assigns[:current_user].id}) conn |> put_flash(:error, "Something went wrong. Please try again or contact support at uku@plausible.io") |> redirect(to: "/settings") diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex index 710a5b0f1..efa3edbef 100644 --- a/lib/plausible_web/router.ex +++ b/lib/plausible_web/router.ex @@ -98,7 +98,8 @@ defmodule PlausibleWeb.Router do post "/contact", PageController, :submit_contact_form get "/billing/change-plan", BillingController, :change_plan_form - post "/billing/change-plan/:plan_name", BillingController, :change_plan + get "/billing/change-plan/preview/:plan_id", BillingController, :change_plan_preview + post "/billing/change-plan/:new_plan_id", BillingController, :change_plan get "/billing/upgrade", BillingController, :upgrade get "/billing/success", BillingController, :success diff --git a/lib/plausible_web/templates/billing/_checkout_button.html.eex b/lib/plausible_web/templates/billing/_checkout_button.html.eex deleted file mode 100644 index 3865b025b..000000000 --- a/lib/plausible_web/templates/billing/_checkout_button.html.eex +++ /dev/null @@ -1 +0,0 @@ - diff --git a/lib/plausible_web/templates/billing/change_plan.html.eex b/lib/plausible_web/templates/billing/change_plan.html.eex index 0c131755e..a4abc18b9 100644 --- a/lib/plausible_web/templates/billing/change_plan.html.eex +++ b/lib/plausible_web/templates/billing/change_plan.html.eex @@ -1,56 +1,100 @@ -
-

Change subscription plan

+ -
- If you choose to change your plan, your current subscription will - be prorated. -
+
+

Change subscription plan

+
-
- 10,000 / mo - $6 / mo - <%= if Plausible.Billing.Plans.is?(@subscription, :personal) do %> -
- - Selected -
- <% else %> - <%= button("Select", class: "button button-sm", method: :post, to: "/billing/change-plan/personal") %> - <% end %> -
-
- 100,000 / mo - $12 / mo - <%= if Plausible.Billing.Plans.is?(@subscription, :startup) do %> -
- - Selected -
- <% else %> - <%= button("Select", class: "button button-sm", method: :post, to: "/billing/change-plan/startup") %> - <% end %> -
-
- 1,000,000 / mo - $36 / mo - <%= if Plausible.Billing.Plans.is?(@subscription, :business) do %> -
- - Selected +
+
+
+ Select your new plan +
+ +
+ Depending on which plan you choose, your card might be charged immediately and your + next payment date could change. You can preview these changes before committing. +
+ +
+ + + + + + +
+ +
+
+
+ + + + + + + + + + + + + + + + + + + + + +
+ Monthly pageviews + + Price per month +
10k + $6 + $4 +
100k + $12 + $8 +
1m + $36 + $24 +
+
- <% else %> - <%= button("Select", class: "button button-sm", method: :post, to: "/billing/change-plan/business") %> - <% end %> -
+
- - - - + +
+
+ +
+ Questions? Contact <%= link("uku@plausible.io", to: "mailto: uku@plausible.io", class: "text-indigo-500") %>
+ + + + diff --git a/lib/plausible_web/templates/billing/change_plan_preview.html.eex b/lib/plausible_web/templates/billing/change_plan_preview.html.eex new file mode 100644 index 000000000..3bd61818d --- /dev/null +++ b/lib/plausible_web/templates/billing/change_plan_preview.html.eex @@ -0,0 +1,86 @@ +
+

Confirm new subscription plan

+
+ +
+
+
Due now
+
+ Your card will be charged a pro-rated amount for the current billing period +
+ +
+
+
+ + + + + + + + + + + + + +
+ Amount + + Date +
$<%= @preview_info["immediate_payment"]["amount"] %><%= present_date(@preview_info["immediate_payment"]["date"]) %>
+
+
+
+ +
+ +
Next payment
+ + +
+
+
+ + + + + + + + + + + + + +
+ Amount + + Date +
$<%= @preview_info["next_payment"]["amount"] %><%= present_date(@preview_info["next_payment"]["date"]) %>
+
+
+
+ +
+ + + Back + + + + <%= button("Confirm plan change", to: "/billing/change-plan/#{@preview_info["plan_id"]}", method: :post, class: "inline-flex items-center px-4 py-2 border border-transparent text-sm leading-5 font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-500 focus:outline-none focus:border-indigo-700 focus:shadow-outline-indigo active:bg-indigo-700 transition ease-in-out duration-150") %> + +
+
+
+ +
+ Questions? Contact <%= link("uku@plausible.io", to: "mailto: uku@plausible.io", class: "text-indigo-500") %> +
+ + + + diff --git a/lib/plausible_web/templates/billing/upgrade.html.eex b/lib/plausible_web/templates/billing/upgrade.html.eex index daade1718..6806b20b3 100644 --- a/lib/plausible_web/templates/billing/upgrade.html.eex +++ b/lib/plausible_web/templates/billing/upgrade.html.eex @@ -1,56 +1,102 @@ -
-

Upgrade your free trial

- -
- <%= cond do %> - <% Timex.equal?(@user.trial_expiry_date, @today) -> %> - Today is the last day of your trial. Please select a plan and enter - your billing info to access your stats after the trial ends. - <% Timex.after?(@user.trial_expiry_date, @today) -> %> -

- Your free trial ends on <%= Timex.format!(@user.trial_expiry_date, "{WDshort} {D} {Mshort}") %>. Please select a plan and enter - your billing info to access your stats after the trial ends. -

- <% Timex.before?(@user.trial_expiry_date, @today) -> %> -

- Your free trial ended on <%= Timex.format!(@user.trial_expiry_date, "{Mshort} {D}")%>. To access your stats going forward, please select a plan and enter your billing info. -

- <% end %> - -

- If you go over your limit in the future, we will contact you about upgrading. - Don't worry, we will never bill you unexpectedly or stop recording without telling you first. -

-

- All plans are on a monthly basis, cancel any time. -

-
-
+ -
- 10,000 / mo - $6 / mo - <%= render("_checkout_button.html", conn: @conn, plan: :personal) %> -
-
- 100,000 / mo - $12 / mo - <%= render("_checkout_button.html", conn: @conn, plan: :startup) %> -
-
- 1,000,000 / mo - $36 / mo - <%= render("_checkout_button.html", conn: @conn, plan: :business) %> -
-
- Your usage: <%= PlausibleWeb.AuthView.delimit_integer(@usage) %> pageviews in the last 30 days -
+
+

Upgrade your free trial

+
+ +
+
+
+ You've used <%= PlausibleWeb.AuthView.delimit_integer(@usage) %> pageviews in the last 30 days +
+ +
-
- Questions? Contact <%= link("uku@plausible.io", to: "mailto: uku@plausible.io", class: "text-indigo-500") %> + + + + + +
+ +
+
+
+ + + + + + + + + + + + + + + + + + + + + +
+ Monthly pageviews + + Price per month +
10k + $6 + $4 +
100k + $12 + $8 +
1m + $36 + $24 +
+
+
+
+ +
+
Due today: $6
+ + + +
- - +
+

+ 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("uku@plausible.io", to: "mailto: uku@plausible.io", class: "text-indigo-500") %> +
+ + + + 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 fc0e60d63..12154fbbd 100644 --- a/lib/plausible_web/templates/email/trial_upgrade_email.html.eex +++ b/lib/plausible_web/templates/email/trial_upgrade_email.html.eex @@ -4,6 +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(@pageviews) %> pageviews. Based on that we recommend you select the <%= suggested_plan_name(@pageviews) %> plan which runs at <%= suggested_plan_cost(@pageviews) %>. +You can also go with yealy billing to get 33% off on your plan.

<%= link("Upgrade now", to: "https://plausible.io/billing/upgrade") %>

diff --git a/lib/plausible_web/views/auth_view.ex b/lib/plausible_web/views/auth_view.ex index 51c82ee0d..2c1d95dd6 100644 --- a/lib/plausible_web/views/auth_view.ex +++ b/lib/plausible_web/views/auth_view.ex @@ -2,14 +2,13 @@ defmodule PlausibleWeb.AuthView do use PlausibleWeb, :view @subscription_names %{ - "558018" => "Personal", - "572810" => "Personal (A)", - "558745" => "Startup", - "558746" => "Business", - "558156" => "Personal (T)", - "558199" => "Startup (T)", - "558200" => "Business (T)", - "free_10k" => "Free 10k" + "558018" => "10k / monthly", + "558745" => "100k / monthly", + "558746" => "1m / monthly", + "572810" => "10k / yearly", + "590752" => "100k / yearly", + "590753" => "1m / yearly", + "free_10k" => "10k / free" } def subscription_name(subscription) do diff --git a/lib/plausible_web/views/billing_view.ex b/lib/plausible_web/views/billing_view.ex index d3fe4a254..353f9b9ab 100644 --- a/lib/plausible_web/views/billing_view.ex +++ b/lib/plausible_web/views/billing_view.ex @@ -1,6 +1,11 @@ defmodule PlausibleWeb.BillingView do use PlausibleWeb, :view + def present_date(date) do + Date.from_iso8601!(date) + |> Timex.format!("{D} {Mshort} {YYYY}") + end + def reccommended_plan(usage) do cond do usage < 9000 -> diff --git a/test/plausible/billing/billing_test.exs b/test/plausible/billing/billing_test.exs index f09309a16..b990dff3b 100644 --- a/test/plausible/billing/billing_test.exs +++ b/test/plausible/billing/billing_test.exs @@ -198,4 +198,18 @@ defmodule Plausible.BillingTest do assert res == {:ok, nil} end end + + describe "change_plan" do + test "sets the next bill amount and date" do + user = insert(:user) + insert(:subscription, user: user) + + Billing.change_plan(user, "123123") + + subscription = Repo.get_by(Plausible.Billing.Subscription, user_id: user.id) + assert subscription.paddle_plan_id == "123123" + assert subscription.next_bill_date == ~D[2019-07-10] + assert subscription.next_bill_amount == "6.00" + end + end end diff --git a/test/plausible_web/controllers/billing_controller_test.exs b/test/plausible_web/controllers/billing_controller_test.exs index e5cfddf6d..1376187c2 100644 --- a/test/plausible_web/controllers/billing_controller_test.exs +++ b/test/plausible_web/controllers/billing_controller_test.exs @@ -22,8 +22,15 @@ defmodule PlausibleWeb.BillingControllerTest do describe "POST /change-plan" do setup [:create_user, :log_in] - test "calls Paddle API to update subscription" do + test "calls Paddle API to update subscription", %{conn: conn, user: user} do + insert(:subscription, user: user) + + post(conn, "/billing/change-plan/123123") + subscription = Plausible.Repo.get_by(Plausible.Billing.Subscription, user_id: user.id) + assert subscription.paddle_plan_id == "123123" + assert subscription.next_bill_date == ~D[2019-07-10] + assert subscription.next_bill_amount == "6.00" end end end diff --git a/test/support/paddle_api_mock.ex b/test/support/paddle_api_mock.ex index 3aa0189b8..3789438fd 100644 --- a/test/support/paddle_api_mock.ex +++ b/test/support/paddle_api_mock.ex @@ -7,4 +7,16 @@ defmodule Plausible.PaddleApi.Mock do } }} end + + def update_subscription(_, %{plan_id: new_plan_id}) do + new_plan_id = String.to_integer(new_plan_id) + + {:ok, %{ + "plan_id" => new_plan_id, + "next_payment" => %{ + "date" => "2019-07-10", + "amount" => 6 + } + }} + end end