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
|
|
|
|
|
2020-05-26 16:09:34 +03:00
|
|
|
def admin_email do
|
|
|
|
Application.get_env(:plausible, :admin_email)
|
|
|
|
end
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
def change_plan_form(conn, _params) do
|
|
|
|
subscription = Billing.active_subscription_for(conn.assigns[:current_user].id)
|
2020-04-21 14:11:38 +03:00
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
if subscription do
|
2020-06-08 10:35:13 +03:00
|
|
|
render(conn, "change_plan.html",
|
|
|
|
subscription: subscription,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
else
|
|
|
|
redirect(conn, to: "/billing/upgrade")
|
|
|
|
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} ->
|
2020-06-08 10:35:13 +03:00
|
|
|
Sentry.capture_message("Error changing plans",
|
|
|
|
extra: %{
|
|
|
|
errors: inspect(e),
|
|
|
|
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
|
|
|
|
|
|
|
|
def upgrade(conn, _params) do
|
|
|
|
usage = Plausible.Billing.usage(conn.assigns[:current_user])
|
|
|
|
today = Timex.today()
|
|
|
|
|
2020-06-08 10:35:13 +03:00
|
|
|
render(conn, "upgrade.html",
|
|
|
|
usage: usage,
|
|
|
|
today: today,
|
|
|
|
user: conn.assigns[:current_user],
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
2021-03-16 13:03:50 +03:00
|
|
|
def upgrade_to_plan(conn, %{"plan_id" => plan_id}) do
|
|
|
|
plan = Plausible.Billing.Plans.for_product_id(plan_id)
|
|
|
|
|
|
|
|
if plan do
|
|
|
|
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
|
|
|
|
|
2020-08-18 12:48:01 +03:00
|
|
|
def upgrade_success(conn, _params) do
|
|
|
|
render(conn, "upgrade_success.html", layout: {PlausibleWeb.LayoutView, "focus.html"})
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|