tarakan/lib/tarakan_web/controllers/billing_controller.ex
Maxfield Luke af6077b9c3
All checks were successful
CI and deploy / Test (push) Successful in 4m52s
CI and deploy / Deploy production (push) Successful in 23s
Initial commit on Forgejo
Fresh repository history for elektrine/tarakan hosted at
https://git.elektrine.com/elektrine/tarakan.
2026-07-29 04:43:40 -04:00

40 lines
1.2 KiB
Elixir

defmodule TarakanWeb.BillingController do
@moduledoc """
Stripe exit points for subscription billing.
Plain POST endpoints (authenticated) that create a Checkout or Billing
Portal session and 303-redirect to Stripe. LiveViews use the same
`Tarakan.Billing` functions directly; these exist for non-LiveView pages
like /pricing.
"""
use TarakanWeb, :controller
alias Tarakan.Billing
def checkout(conn, %{"plan" => plan}) do
case Billing.ensure_checkout(conn.assigns.current_scope, plan) do
{:ok, checkout_url} ->
redirect(conn, external: checkout_url)
{:error, _reason} ->
conn
|> put_flash(:error, "Checkout could not be started. Try again shortly.")
|> redirect(to: ~p"/pricing")
end
end
def checkout(conn, _params), do: checkout(conn, %{"plan" => "enterprise"})
def portal(conn, _params) do
case Billing.portal_session(conn.assigns.current_scope.account) do
{:ok, portal_url} ->
redirect(conn, external: portal_url)
{:error, _reason} ->
conn
|> put_flash(:error, "The billing portal is unavailable for this account.")
|> redirect(to: ~p"/accounts/billing")
end
end
end