2019-09-02 14:29:19 +03:00
|
|
|
defmodule PlausibleWeb.BillingController do
|
|
|
|
use PlausibleWeb, :controller
|
|
|
|
use Plausible.Repo
|
|
|
|
alias Plausible.Billing
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
plug PlausibleWeb.RequireAccountPlug
|
|
|
|
|
2021-10-20 17:49:11 +03:00
|
|
|
def upgrade(conn, _params) do
|
|
|
|
user =
|
|
|
|
conn.assigns[:current_user]
|
|
|
|
|> Repo.preload(:enterprise_plan)
|
|
|
|
|
|
|
|
cond do
|
|
|
|
user.subscription && user.subscription.status == "active" ->
|
|
|
|
redirect(conn, to: Routes.billing_path(conn, :change_plan_form))
|
|
|
|
|
|
|
|
user.enterprise_plan ->
|
|
|
|
redirect(conn,
|
|
|
|
to: Routes.billing_path(conn, :upgrade_enterprise_plan, user.enterprise_plan.id)
|
|
|
|
)
|
|
|
|
|
|
|
|
true ->
|
|
|
|
render(conn, "upgrade.html",
|
|
|
|
usage: Plausible.Billing.usage(user),
|
|
|
|
user: user,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
|
|
|
end
|
2020-05-26 16:09:34 +03:00
|
|
|
end
|
|
|
|
|
2021-10-20 17:49:11 +03:00
|
|
|
def upgrade_enterprise_plan(conn, %{"plan_id" => plan_id}) do
|
|
|
|
user =
|
|
|
|
conn.assigns[:current_user]
|
|
|
|
|> Repo.preload(:enterprise_plan)
|
2020-04-21 14:11:38 +03:00
|
|
|
|
2021-10-20 17:49:11 +03:00
|
|
|
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,
|
2020-06-08 10:35:13 +03:00
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
else
|
2021-10-20 17:49:11 +03:00
|
|
|
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)
|
|
|
|
|
|
|
|
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,
|
|
|
|
plan: plan,
|
|
|
|
user: conn.assigns[:current_user],
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
|
|
|
else
|
|
|
|
render_error(conn, 404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def upgrade_success(conn, _params) do
|
|
|
|
render(conn, "upgrade_success.html", layout: {PlausibleWeb.LayoutView, "focus.html"})
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_plan_form(conn, _params) do
|
|
|
|
user =
|
|
|
|
conn.assigns[:current_user]
|
|
|
|
|> Repo.preload(:enterprise_plan)
|
|
|
|
|
|
|
|
subscription = Billing.active_subscription_for(user.id)
|
|
|
|
|
|
|
|
cond do
|
|
|
|
subscription && user.enterprise_plan ->
|
|
|
|
redirect(conn,
|
|
|
|
to: Routes.billing_path(conn, :change_enterprise_plan, user.enterprise_plan.id)
|
|
|
|
)
|
|
|
|
|
|
|
|
subscription ->
|
|
|
|
render(conn, "change_plan.html",
|
|
|
|
subscription: subscription,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
|
|
|
|
|
|
|
true ->
|
|
|
|
redirect(conn, to: Routes.billing_path(conn, :upgrade))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_enterprise_plan(conn, %{"plan_id" => plan_id}) do
|
|
|
|
user =
|
|
|
|
conn.assigns[:current_user]
|
|
|
|
|> Repo.preload(:enterprise_plan)
|
|
|
|
|
|
|
|
cond do
|
|
|
|
is_nil(user.subscription) ->
|
|
|
|
redirect(conn, to: "/billing/upgrade")
|
|
|
|
|
|
|
|
is_nil(user.enterprise_plan) ->
|
|
|
|
render_error(conn, 404)
|
|
|
|
|
|
|
|
user.enterprise_plan.id !== String.to_integer(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,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-21 14:11:38 +03:00
|
|
|
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)
|
2020-06-08 10:35:13 +03:00
|
|
|
|
|
|
|
render(conn, "change_plan_preview.html",
|
|
|
|
subscription: subscription,
|
|
|
|
preview_info: preview_info,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2020-04-21 14:11:38 +03:00
|
|
|
else
|
|
|
|
redirect(conn, to: "/billing/upgrade")
|
|
|
|
end
|
|
|
|
end
|
2019-09-02 14:29:19 +03:00
|
|
|
|
2020-04-21 14:11:38 +03:00
|
|
|
def change_plan(conn, %{"new_plan_id" => new_plan_id}) do
|
|
|
|
case Billing.change_plan(conn.assigns[:current_user], new_plan_id) do
|
2019-09-02 14:29:19 +03:00
|
|
|
{:ok, _subscription} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:success, "Plan changed successfully")
|
|
|
|
|> redirect(to: "/settings")
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
{:error, e} ->
|
2021-04-19 14:44:17 +03:00
|
|
|
# https://developer.paddle.com/api-reference/intro/api-error-codes
|
|
|
|
msg =
|
|
|
|
case e do
|
|
|
|
%{"code" => 147} ->
|
|
|
|
"We were unable to charge your card. Make sure your payment details are up to date and try again."
|
|
|
|
|
|
|
|
%{"message" => msg} when not is_nil(msg) ->
|
|
|
|
msg
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
"Something went wrong. Please try again or contact support at support@plausible.io"
|
|
|
|
end
|
|
|
|
|
2020-06-08 10:35:13 +03:00
|
|
|
Sentry.capture_message("Error changing plans",
|
|
|
|
extra: %{
|
|
|
|
errors: inspect(e),
|
2021-04-19 14:44:17 +03:00
|
|
|
message: msg,
|
2020-06-08 10:35:13 +03:00
|
|
|
new_plan_id: new_plan_id,
|
|
|
|
user_id: conn.assigns[:current_user].id
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
conn
|
2020-06-08 10:35:13 +03:00
|
|
|
|> put_flash(
|
|
|
|
:error,
|
|
|
|
"Something went wrong. Please try again or contact support at support@plausible.io"
|
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
|> redirect(to: "/settings")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|