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 @@ -
| + Monthly pageviews + | ++ Price per month + | +
|---|---|
| 10k | ++ $6 + $4 + | +
| 100k | ++ $12 + $8 + | +
| 1m | ++ $36 + $24 + | +
| + Amount + | ++ Date + | +
|---|---|
| $<%= @preview_info["immediate_payment"]["amount"] %> | +<%= present_date(@preview_info["immediate_payment"]["date"]) %> | +
| + Amount + | ++ Date + | +
|---|---|
| $<%= @preview_info["next_payment"]["amount"] %> | +<%= present_date(@preview_info["next_payment"]["date"]) %> | +
- 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. -
-| + Monthly pageviews + | ++ Price per month + | +
|---|---|
| 10k | ++ $6 + $4 + | +
| 100k | ++ $12 + $8 + | +
| 1m | ++ $36 + $24 + | +