Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Streamline creating/updating/deleting subscriptions in development (#5328)
RobertJoonascb465511bb20
diff --git a/config/dev.exs b/config/dev.exsindex ca0dbec390..564dcb86a0 100644--- a/config/dev.exs+++ b/config/dev.exs@@ -27,5 +27,7 @@ config :plausible, PlausibleWeb.Endpoint,]]+config :plausible, paddle_api: Plausible.Billing.DevPaddleApiMock+config :phoenix, :stacktrace_depth, 20config :phoenix, :plug_init_mode, :runtimediff --git a/lib/plausible/billing/paddle_api.ex b/lib/plausible/billing/paddle_api.exindex 3c1ebfb8dd..6daffd6a15 100644--- a/lib/plausible/billing/paddle_api.ex+++ b/lib/plausible/billing/paddle_api.ex@@ -149,7 +149,7 @@ defmodule Plausible.Billing.PaddleApi doenddef checkout_domain() do- if Application.get_env(:plausible, :environment) in ["dev", "staging"] do+ if Application.get_env(:plausible, :environment) == "staging" do"https://sandbox-checkout.paddle.com"else"https://checkout.paddle.com"@@ -157,7 +157,7 @@ defmodule Plausible.Billing.PaddleApi doenddef vendors_domain() do- if Application.get_env(:plausible, :environment) in ["dev", "staging"] do+ if Application.get_env(:plausible, :environment) == "staging" do"https://sandbox-vendors.paddle.com"else"https://vendors.paddle.com"diff --git a/lib/plausible/billing/plans.ex b/lib/plausible/billing/plans.exindex 5b17aba608..d96adc2db6 100644--- a/lib/plausible/billing/plans.ex+++ b/lib/plausible/billing/plans.ex@@ -36,7 +36,7 @@ defmodule Plausible.Billing.Plans doowned_plan = get_regular_plan(subscription)cond do- Application.get_env(:plausible, :environment) in ["dev", "staging"] -> @sandbox_plans+ Application.get_env(:plausible, :environment) == "staging" -> @sandbox_plansis_nil(owned_plan) -> @plans_v4subscription && Subscriptions.expired?(subscription) -> @plans_v4owned_plan.kind == :business -> @plans_v4@@ -52,7 +52,7 @@ defmodule Plausible.Billing.Plans doowned_plan = get_regular_plan(subscription)cond do- Application.get_env(:plausible, :environment) in ["dev", "staging"] -> @sandbox_plans+ Application.get_env(:plausible, :environment) == "staging" -> @sandbox_planssubscription && Subscriptions.expired?(subscription) -> @plans_v4owned_plan && owned_plan.generation < 4 -> @plans_v3true -> @plans_v4@@ -227,7 +227,7 @@ defmodule Plausible.Billing.Plans doenddefp sandbox_plans() do- if Application.get_env(:plausible, :environment) in ["dev", "staging"] do+ if Application.get_env(:plausible, :environment) == "staging" do@sandbox_planselse[]diff --git a/lib/plausible_web/components/billing/billing.ex b/lib/plausible_web/components/billing/billing.exindex 80abd6408b..5a404cd198 100644--- a/lib/plausible_web/components/billing/billing.ex+++ b/lib/plausible_web/components/billing/billing.ex@@ -233,25 +233,21 @@ defmodule PlausibleWeb.Components.Billing doslot :inner_block, required: truedef paddle_button(assigns) do+ js_action_expr =+ start_paddle_checkout_expr(assigns.paddle_product_id, assigns.team, assigns.user)+confirmed =if assigns.confirm_message, do: "confirm(\"#{assigns.confirm_message}\")", else: "true"- passthrough =- if assigns.team do- "ee:#{ee?()};user:#{assigns.user.id};team:#{assigns.team.id}"- else- "ee:#{ee?()};user:#{assigns.user.id}"- end-assigns =assigns|> assign(:confirmed, confirmed)- |> assign(:passthrough, passthrough)+ |> assign(:js_action_expr, js_action_expr)~H"""<buttonid={@id}- onclick={"if (#{@confirmed}) {Paddle.Checkout.open(#{Jason.encode!(%{product: @paddle_product_id, email: @user.email, disableLogout: true, passthrough: @passthrough, success: Routes.billing_path(PlausibleWeb.Endpoint, :upgrade_success), theme: "none"})})}"}+ onclick={"if (#{@confirmed}) {#{@js_action_expr}}"}class={["text-sm w-full mt-6 block rounded-md py-2 px-3 text-center font-semibold leading-6 text-white",!@checkout_disabled && "bg-indigo-600 hover:bg-indigo-500",@@ -263,17 +259,46 @@ defmodule PlausibleWeb.Components.Billing do"""end- def paddle_script(assigns) do- ~H"""- <script type="text/javascript" src="https://cdn.paddle.com/paddle/paddle.js">- </script>- <script :if={Application.get_env(:plausible, :environment) in ["dev", "staging"]}>- Paddle.Environment.set('sandbox')- </script>- <script>- Paddle.Setup({vendor: <%= Application.get_env(:plausible, :paddle) |> Keyword.fetch!(:vendor_id) %> })- </script>- """+ if Mix.env() == :dev do+ def start_paddle_checkout_expr(paddle_product_id, _team, _user) do+ "window.location = '#{Routes.dev_subscription_path(PlausibleWeb.Endpoint, :create_form, paddle_product_id)}'"+ end++ def paddle_script(assigns), do: ~H""+ else+ def start_paddle_checkout_expr(paddle_product_id, team, user) do+ passthrough =+ if team do+ "ee:#{ee?()};user:#{user.id};team:#{team.id}"+ else+ "ee:#{ee?()};user:#{user.id}"+ end++ paddle_checkout_params =+ Jason.encode!(%{+ product: paddle_product_id,+ email: user.email,+ disableLogout: true,+ passthrough: passthrough,+ success: Routes.billing_path(PlausibleWeb.Endpoint, :upgrade_success),+ theme: "none"+ })++ "Paddle.Checkout.open(#{paddle_checkout_params})"+ end++ def paddle_script(assigns) do+ ~H"""+ <script type="text/javascript" src="https://cdn.paddle.com/paddle/paddle.js">+ </script>+ <script :if={Application.get_env(:plausible, :environment) == "staging"}>+ Paddle.Environment.set('sandbox')+ </script>+ <script>+ Paddle.Setup({vendor: <%= Application.get_env(:plausible, :paddle) |> Keyword.fetch!(:vendor_id) %> })+ </script>+ """+ endenddef upgrade_link(assigns) dodiff --git a/lib/plausible_web/controllers/api/paddle_controller.ex b/lib/plausible_web/controllers/api/paddle_controller.exindex bd3e8b0b40..132c0df283 100644--- a/lib/plausible_web/controllers/api/paddle_controller.ex+++ b/lib/plausible_web/controllers/api/paddle_controller.ex@@ -88,7 +88,7 @@ defmodule PlausibleWeb.Api.PaddleController do@paddle_currency_reference_plan_id "857097"@paddle_sandbox_currency_reference_plan_id "63842"defp get_currency_reference_plan_id() do- if Application.get_env(:plausible, :environment) in ["dev", "staging"] do+ if Application.get_env(:plausible, :environment) == "staging" do@paddle_sandbox_currency_reference_plan_idelse@paddle_currency_reference_plan_id@@ -99,7 +99,7 @@ defmodule PlausibleWeb.Api.PaddleController do@paddle_sandbox_key File.read!("priv/paddle_sandbox.pem")defp get_paddle_key() do- if Application.get_env(:plausible, :environment) in ["dev", "staging"] do+ if Application.get_env(:plausible, :environment) == "staging" do@paddle_sandbox_keyelse@paddle_prod_keydiff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.exindex ca5beedbd8..91a0c0169c 100644--- a/lib/plausible_web/router.ex+++ b/lib/plausible_web/router.ex@@ -115,6 +115,22 @@ defmodule PlausibleWeb.Router doendend+ on_ee do+ if Mix.env() in [:dev, :test] do+ scope "/dev", PlausibleWeb do+ pipe_through :browser++ get "/billing/create-subscription-form/:plan_id", DevSubscriptionController, :create_form+ get "/billing/update-subscription-form", DevSubscriptionController, :update_form+ get "/billing/cancel-subscription-form", DevSubscriptionController, :cancel_form++ post "/billing/create-subscription/:plan_id", DevSubscriptionController, :create+ post "/billing/update-subscription", DevSubscriptionController, :update+ post "/billing/cancel-subscription", DevSubscriptionController, :cancel+ end+ end+ end+# Routes for plug integration testingif Mix.env() in [:test, :ce_test] doscope "/plug-tests", PlausibleWeb dodiff --git a/lib/plausible_web/templates/settings/subscription.html.heex b/lib/plausible_web/templates/settings/subscription.html.heexindex 3d0c411692..92d2cc6b3c 100644--- a/lib/plausible_web/templates/settings/subscription.html.heex+++ b/lib/plausible_web/templates/settings/subscription.html.heex@@ -94,6 +94,14 @@<.button_link theme="danger" href={@subscription.cancel_url}>Cancel my subscription</.button_link>+ <%= if Application.get_env(:plausible, :environment) == "dev" do %>+ <a+ href={@subscription.update_url}+ class="ml-2 font-medium rounded-md px-3.5 py-2.5 text-sm shadow-sm border border-gray-300 dark:border-gray-500 text-yellow-600 dark:text-yellow-400 bg-white dark:bg-gray-900"+ >+ [DEV ONLY] Change subscription status+ </a>+ <% end %></div><% true -> %><div class="mt-8">diff --git a/priv/plan_prices.json b/priv/plan_prices.jsonnew file mode 100644index 0000000000..1427c8153b--- /dev/null+++ b/priv/plan_prices.json@@ -0,0 +1,109 @@+{+ "558746": 36,+ "590753": 288,+ "648089": 4800,+ "558018": 6,+ "572810": 48,+ "558745": 12,+ "590752": 96,+ "597485": 18,+ "597486": 144,+ "597487": 27,+ "597488": 216,+ "597642": 48,+ "597643": 384,+ "597309": 69,+ "597310": 552,+ "597311": 99,+ "597312": 792,+ "642352": 150,+ "642354": 1200,+ "642355": 225,+ "642356": 1800,+ "650652": 330,+ "650653": 2640,+ "654177": 6,+ "653232": 60,+ "654178": 12,+ "653234": 120,+ "653237": 20,+ "653236": 200,+ "653238": 30,+ "653239": 300,+ "653240": 50,+ "653242": 500,+ "653253": 70,+ "653254": 700,+ "653255": 100,+ "653256": 1000,+ "654181": 150,+ "653257": 1500,+ "654182": 225,+ "653258": 2250,+ "654183": 330,+ "653259": 3300,+ "749342": 9,+ "749343": 90,+ "749344": 19,+ "749345": 190,+ "749346": 29,+ "749347": 290,+ "749348": 49,+ "749349": 490,+ "749350": 69,+ "749352": 690,+ "749353": 89,+ "749355": 890,+ "749356": 129,+ "749357": 1290,+ "749358": 169,+ "749359": 1690,+ "857481": 19,+ "857482": 190,+ "857483": 39,+ "857484": 390,+ "857486": 59,+ "857487": 590,+ "857490": 99,+ "857491": 990,+ "857493": 139,+ "857494": 1390,+ "857495": 179,+ "857496": 1790,+ "857498": 259,+ "857500": 2590,+ "857501": 339,+ "857502": 3390,+ "857097": 9,+ "857079": 90,+ "857098": 19,+ "857080": 190,+ "857099": 29,+ "857081": 290,+ "857100": 49,+ "857082": 490,+ "857101": 69,+ "857083": 690,+ "857102": 89,+ "857084": 890,+ "857103": 129,+ "857085": 1290,+ "857104": 169,+ "857086": 1690,+ "857105": 19,+ "857087": 190,+ "857106": 39,+ "857088": 390,+ "857107": 59,+ "857089": 590,+ "857108": 99,+ "857090": 990,+ "857109": 139,+ "857091": 1390,+ "857110": 179,+ "857092": 1790,+ "857111": 259,+ "857093": 2590,+ "857112": 339,+ "857094": 3390+}\ No newline at end of filediff --git a/test/support/dev/billing/dev_paddle_api_mock.ex b/test/support/dev/billing/dev_paddle_api_mock.exnew file mode 100644index 0000000000..75d1c2746b--- /dev/null+++ b/test/support/dev/billing/dev_paddle_api_mock.ex@@ -0,0 +1,75 @@+defmodule Plausible.Billing.DevPaddleApiMock do+ @moduledoc """+ This module mocks API requests made to Paddle in the :dev environment.++ Note that not creating and cancelling subscriptions are handled separately. See+ `Plausible.Billing.DevSubscriptions`.+ """++ import Ecto.Query+ alias Plausible.Repo+ alias Plausible.Billing.EnterprisePlan++ @prices_file_path Application.app_dir(:plausible, ["priv", "plan_prices.json"])+ @prices File.read!(@prices_file_path) |> Jason.decode!()++ def all_prices() do+ enterprise_plan_prices =+ Repo.all(from p in EnterprisePlan, select: {p.paddle_plan_id, 123})+ |> Map.new()++ Map.merge(@prices, enterprise_plan_prices)+ end++ @doc """+ Mocks the real `Plausible.Billing.PaddleApi.fetch_prices`, but:++ 1. instead of filtering by a list of product IDs simply returns the prices for+ all plans++ 2. always returns "EUR" as the currency++ The prices of all production plans are duplicated into `/priv/plan_prices.json`+ in order to avoid relying on Paddle in the :dev env.+ """+ def fetch_prices(_, _) do+ prices_as_money =+ all_prices()+ |> Map.new(fn {plan_id, price} ->+ {plan_id, Money.from_integer(price * 100, "EUR")}+ end)++ {:ok, prices_as_money}+ end++ def update_subscription_preview(paddle_subscription_id, new_plan_id) do+ {:ok,+ %{+ "immediate_payment" => %{+ "amount" => all_prices()[new_plan_id],+ "currency" => "EUR",+ "date" => Date.utc_today() |> Date.to_iso8601()+ },+ "next_payment" => %{+ "amount" => all_prices()[new_plan_id],+ "currency" => "EUR",+ "date" => Date.utc_today() |> Date.shift(month: 1) |> Date.to_iso8601()+ },+ "plan_id" => String.to_integer(new_plan_id),+ "subscription_id" => paddle_subscription_id+ }}+ end++ def update_subscription(_, %{plan_id: plan_id}) do+ {:ok,+ %{+ "next_payment" => %{+ "amount" => all_prices()[plan_id],+ "date" => Date.utc_today() |> Date.shift(month: 1) |> Date.to_iso8601()+ },+ "plan_id" => String.to_integer(plan_id)+ }}+ end++ def get_invoices(_), do: {:error, :no_invoices}+enddiff --git a/test/support/dev/billing/dev_subscriptions.ex b/test/support/dev/billing/dev_subscriptions.exnew file mode 100644index 0000000000..ee58731569--- /dev/null+++ b/test/support/dev/billing/dev_subscriptions.ex@@ -0,0 +1,141 @@+defmodule Plausible.Billing.DevSubscriptions do+ @moduledoc """+ Module for conveniently handling subscriptions in the :dev environment.+ """+ use Plausible++ on_ee do+ import Ecto.Query++ alias Plausible.{Repo, Billing}+ alias Plausible.Billing.{Plan, EnterprisePlan, DevPaddleApiMock}+ alias PlausibleWeb.Router.Helpers, as: Routes++ def create_after_1s(team_id, plan_id) do+ Task.start(fn ->+ Process.sleep(1000)+ create(team_id, plan_id)+ end)+ end++ def create(team_id, plan_id, opts \\ []) do+ plan_or_enterprise_plan = get_plan_or_enterprise_plan(plan_id)++ if plan_or_enterprise_plan do+ if Keyword.get(opts, :clean_existing, true) do+ delete(team_id)+ end++ %{+ "event_time" => to_string(NaiveDateTime.utc_now(:second)),+ "alert_name" => "subscription_created",+ "passthrough" => "ee:true;user:0;team:#{team_id}",+ "email" => "",+ "subscription_id" => Ecto.UUID.generate(),+ "subscription_plan_id" => plan_id,+ "update_url" => Routes.dev_subscription_path(PlausibleWeb.Endpoint, :update_form),+ "cancel_url" => Routes.dev_subscription_path(PlausibleWeb.Endpoint, :cancel_form),+ "status" => "active",+ "next_bill_date" => next_bill_date(plan_or_enterprise_plan, plan_id),+ "unit_price" => "#{to_string(DevPaddleApiMock.all_prices()[plan_id])}.00",+ "currency" => "EUR"+ }+ |> Plausible.Billing.subscription_created()++ {:ok, Repo.reload(get_team_subscription(team_id))}+ else+ {:error, "Plan with id '#{plan_id}' not found."}+ end+ end++ def update(team_id, new_status) do+ if subscription = get_team_subscription(team_id) do+ current_plan_id = subscription.paddle_plan_id+ current_plan = get_plan_or_enterprise_plan(current_plan_id)++ next_bill_date =+ case new_status do+ "active" -> next_bill_date(current_plan, current_plan_id)+ "past_due" -> Date.utc_today() |> Date.shift(day: 5) |> Date.to_iso8601()+ "paused" -> subscription.next_bill_date |> Date.to_iso8601()+ end++ %{+ "subscription_id" => subscription.paddle_subscription_id,+ "subscription_plan_id" => current_plan_id,+ "update_url" => subscription.update_url,+ "cancel_url" => subscription.cancel_url,+ "old_status" => to_string(subscription.status),+ "status" => new_status,+ "next_bill_date" => next_bill_date,+ "new_unit_price" => "#{to_string(DevPaddleApiMock.all_prices()[current_plan_id])}.00",+ "currency" => "EUR"+ }+ |> Plausible.Billing.subscription_updated()++ :ok+ else+ {:error, :no_subscription}+ end+ end++ def delete(team_id, opts \\ []) do+ delete_enterprise? = Keyword.get(opts, :delete_enterprise?, false)++ if subscription = get_team_subscription(team_id) do+ Repo.delete(subscription)++ if delete_enterprise? do+ Plausible.Repo.delete_all(from(e in EnterprisePlan, where: e.team_id == ^team_id))+ end++ :ok+ else+ {:error, :no_subscription}+ end+ end++ def cancel(team_id, opts \\ []) do+ set_expired? = Keyword.get(opts, :set_expired?, false)++ if subscription = get_team_subscription(team_id) do+ %{"subscription_id" => subscription.paddle_subscription_id, "status" => "deleted"}+ |> Billing.subscription_cancelled()++ subscription = Repo.reload!(subscription)++ if set_expired? do+ subscription+ |> Ecto.Changeset.change(%{next_bill_date: Date.utc_today() |> Date.add(-1)})+ |> Repo.update()+ else+ {:ok, subscription}+ end+ else+ {:error, :no_subscription}+ end+ end++ defp get_plan_or_enterprise_plan(plan_id) do+ Billing.Plans.find(plan_id) || Repo.get_by(EnterprisePlan, paddle_plan_id: plan_id)+ end++ defp get_team_subscription(team_id) do+ Plausible.Teams.Team+ |> Plausible.Repo.get(team_id)+ |> Plausible.Teams.with_subscription()+ |> Map.get(:subscription)+ end++ defp next_bill_date(%EnterprisePlan{} = plan, _) do+ if plan.billing_interval == :monthly, do: next_month(), else: next_year()+ end++ defp next_bill_date(%Plan{} = plan, plan_id) do+ if plan_id == plan.monthly_product_id, do: next_month(), else: next_year()+ end++ defp next_month(), do: Date.utc_today() |> Date.shift(month: 1) |> Date.to_iso8601()+ defp next_year(), do: Date.utc_today() |> Date.shift(year: 1) |> Date.to_iso8601()+ end+enddiff --git a/test/support/dev/controllers/dev_subscription_controller.ex b/test/support/dev/controllers/dev_subscription_controller.exnew file mode 100644index 0000000000..4702c5b2ec--- /dev/null+++ b/test/support/dev/controllers/dev_subscription_controller.ex@@ -0,0 +1,87 @@+defmodule PlausibleWeb.DevSubscriptionController do+ use Plausible++ on_ee do+ use PlausibleWeb, :controller++ alias Plausible.Billing.DevSubscriptions++ plug PlausibleWeb.RequireAccountPlug++ plug Plausible.Plugs.AuthorizeTeamAccess, [:owner, :admin, :billing]++ def create_form(conn, %{"plan_id" => plan_id}) do+ render(conn, "create_dev_subscription.html",+ back_link: Routes.billing_path(conn, :choose_plan),+ plan_id: plan_id+ )+ end++ def update_form(conn, _params) do+ team = conn.assigns.current_team |> Plausible.Teams.with_subscription()++ if is_nil(team.subscription),+ do: raise("Can't render subscription update form without subscription")++ render(conn, "update_dev_subscription.html",+ back_link: Routes.settings_path(conn, :subscription),+ current_status: team.subscription.status+ )+ end++ def cancel_form(conn, _params) do+ team = conn.assigns.current_team |> Plausible.Teams.with_subscription()++ if is_nil(team.subscription),+ do: raise("Can't render subscription cancel form without subscription")++ render(conn, "cancel_dev_subscription.html",+ back_link: Routes.settings_path(conn, :subscription),+ enterprise_plan?: Plausible.Teams.Billing.enterprise_configured?(team)+ )+ end++ def create(conn, %{"plan_id" => plan_id}) do+ team = conn.assigns.current_team+ DevSubscriptions.create_after_1s(team.id, plan_id)+ redirect(conn, to: Routes.billing_path(PlausibleWeb.Endpoint, :upgrade_success))+ end++ def update(conn, %{"status" => status}) do+ team = conn.assigns.current_team++ :ok = DevSubscriptions.update(team.id, status)++ conn+ |> put_flash(:success, "Subscription status set to '#{status}'")+ |> redirect(to: Routes.settings_path(conn, :subscription))+ end++ def cancel(conn, %{"action" => action}) do+ team = conn.assigns.current_team++ flash_msg =+ case action do+ "cancel" ->+ DevSubscriptions.cancel(team.id)+ "Subscription cancelled"++ "cancel_and_expire" ->+ DevSubscriptions.cancel(team.id, set_expired?: true)+ "Subscription cancelled and set as 'expired'"++ "delete" ->+ DevSubscriptions.delete(team.id)+ "Subscription deleted"++ "delete_enterprise" ->+ DevSubscriptions.delete(team.id, delete_enterprise?: true)+ "Subscription and enterprise plans deleted"+ end++ conn+ |> put_flash(:success, flash_msg)+ |> redirect(to: Routes.settings_path(conn, :subscription))+ end+ end+enddiff --git a/test/support/dev/templates/dev_subscription/cancel_dev_subscription.heex b/test/support/dev/templates/dev_subscription/cancel_dev_subscription.heexnew file mode 100644index 0000000000..361f8ab1a2--- /dev/null+++ b/test/support/dev/templates/dev_subscription/cancel_dev_subscription.heex@@ -0,0 +1,89 @@+<.focus_box>+ <div class="text-lg mb-6 font-bold dark:text-gray-100">+ [Test Mode] Cancel your subscription+ </div>+ <form action={Routes.dev_subscription_path(@conn, :cancel)} method="POST" class="text-sm">+ <div class="block text-gray-500 dark:text-gray-200 text-sm">+ In development mode you can choose how exactly you want to "cancel" your subscription.<br /><br />+ </div>++ <label>+ <input type="radio" name="action" value="cancel" checked /> Just cancel the subscription+ </label>+ <br />+ <p class="text-xs ml-4 pl-1 italic text-gray-500">+ Access to stats will still be granted until next_bill_date+ </p>+ <br />++ <label>+ <input type="radio" name="action" value="cancel_and_expire" />+ Cancel the subscription and make it "expired"+ </label>+ <br />+ <p class="text-xs ml-4 pl-1 italic text-gray-500">+ Cancel the subscription and set next_bill_date to yesterday+ </p>+ <br />++ <label>+ <input type="radio" name="action" value="delete" /> Delete the subscription+ </label>+ <br />+ <p class="text-xs ml-4 pl-1 italic text-gray-500">Back to "trial" or "expired trial" state</p>++ <%= if @enterprise_plan? do %>+ <br />+ <label>+ <input type="radio" name="action" value="delete_enterprise" />+ Delete the subscription and enterprise plan+ </label>+ <br />+ <p class="text-xs ml-4 pl-1 italic text-gray-500">+ Also restores the ability to subscribe to regular plans+ </p>+ <% end %>++ <div class="flex items-center justify-between mt-10">+ <span class="flex rounded-md shadow-sm">+ <a+ href={@back_link}+ type="button"+ class="inline-flex items-center px-4 py-2 border border-gray-300 dark:border-gray-500 text-sm leading-5 font-medium rounded-md text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-800 hover:text-gray-500 dark:hover:text-gray-200 focus:outline-none focus:border-blue-300 focus:ring active:text-gray-800 dark:active:text-gray-200 active:bg-gray-50 transition ease-in-out duration-150"+ >+ Back+ </a>+ </span>+ <button+ id="submit-button"+ type="submit"+ class="font-medium rounded-md px-3.5 py-2.5 text-sm shadow-sm border border-gray-300 dark:border-gray-500 text-red-700 bg-white dark:bg-gray-900 hover:text-red-500 dark:hover:text-red-400 dark:text-red-500"+ >+ Cancel Subscription+ </button>+ </div>++ <script>+ const cancelButton = document.getElementById('submit-button')+ const radios = document.querySelectorAll('input[name="action"]')++ const labels = {+ cancel: 'Cancel Subscription',+ cancel_and_expire: 'Cancel and Set Expired',+ delete: 'Delete Subscription',+ delete_enterprise: 'Delete Subscription'+ };++ function updateButtonText() {+ const selected = document.querySelector('input[name="action"]:checked');+ console.log(selected)++ cancelButton.innerText = labels[selected.value];+ }++ radios.forEach(function(radio) {+ radio.addEventListener('change', updateButtonText);+ })+ </script>+ </form>+</.focus_box>diff --git a/test/support/dev/templates/dev_subscription/create_dev_subscription.heex b/test/support/dev/templates/dev_subscription/create_dev_subscription.heexnew file mode 100644index 0000000000..a847fd1615--- /dev/null+++ b/test/support/dev/templates/dev_subscription/create_dev_subscription.heex@@ -0,0 +1,38 @@+<.focus_box>+ <div class="text-lg mb-6 font-bold dark:text-gray-100">+ [Test Mode] Subscribe to plan {@plan_id}+ </div>+ <div class="block text-gray-500 dark:text-gray-200 text-sm">+ To make subscriptions easier to handle on localhost, there's no integration with Paddle and everything works in-house instead.<br /><br />+ Normally when proceeding to upgrade to a plan from<b><code>/billing/choose-plan</code></b> you would go through a Paddle checkout flow:+ <br /><br />++ <ul>+ <li>1. Providing payment details</li>+ <li>2. Paddle processing the information and trying to charge the card</li>+ <li>3. Receiving a webhook from Paddle (<b><code>subscription_created</code></b>)</li>+ <li>4. We create the subscription</li>+ </ul>++ <br />+ This local implementation skips steps 1-3, redirecting you first to <b><code>/billing/upgrade-success</code></b>, and after a short artificial delay, to <b><code>/settings</code></b>, with the subscription created.+ <br /><br /> The real Paddle integration can be tested in "Sandbox" mode on staging.+ </div>++ <div class="flex items-center justify-between mt-10">+ <a+ href={@back_link}+ type="button"+ class="inline-flex items-center px-4 py-2 border border-gray-300 dark:border-gray-500 text-sm leading-5 font-medium rounded-md text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-800 hover:text-gray-500 dark:hover:text-gray-200 focus:outline-none focus:border-blue-300 focus:ring active:text-gray-800 dark:active:text-gray-200 active:bg-gray-50 transition ease-in-out duration-150"+ >+ Back+ </a>+ <.button_link+ href={Routes.dev_subscription_path(PlausibleWeb.Endpoint, :create, @plan_id)}+ method="post"+ mt?={false}+ >+ Subscribe to plan+ </.button_link>+ </div>+</.focus_box>diff --git a/test/support/dev/templates/dev_subscription/update_dev_subscription.heex b/test/support/dev/templates/dev_subscription/update_dev_subscription.heexnew file mode 100644index 0000000000..9e02f7b23d--- /dev/null+++ b/test/support/dev/templates/dev_subscription/update_dev_subscription.heex@@ -0,0 +1,127 @@+<.focus_box>+ <div class="text-lg mb-6 font-bold dark:text-gray-100">+ [Test Mode] Update your subscription status+ </div>+ <form action={Routes.dev_subscription_path(@conn, :update)} method="POST" class="text-sm">+ <div class="block text-gray-500 dark:text-gray-200 text-sm">+ Simulate subscription status change events. On production, this happens through Paddle webhooks.+ This implementation is calling the underlying functions as close as possible to how it happens on+ production. Check out the below links to find out more in Paddle documentation.<br /><br />+ -+ <a class="text-indigo-600" href="https://developer.paddle.com/classic">+ Paddle "Classic" docs homepage+ </a>+ <br /> -+ <a+ class="text-indigo-600"+ href="https://developer.paddle.com/classic/reference/zg9joji1mzu0mdi2-subscription-status-reference"+ >+ Paddle subscription status reference+ </a>+ <br /> -+ <a+ class="text-indigo-600"+ href="https://developer.paddle.com/classic/guides/zg9joji1mzu0mduy-payment-failures"+ >+ Payment Failures+ </a>+ <br />+ <br />+ </div>++ <%= if @current_status == Plausible.Billing.Subscription.Status.active() do %>+ Current subscription status: <b class="text-green-400">ACTIVE</b>+ <br /><br />++ <label>+ <input type="radio" name="status" value="past_due" /> Set to+ <b class="text-yellow-400">PAST DUE</b>+ </label>+ <br />+ <p class="text-xs ml-4 pl-1 italic text-gray-500">+ Simulate a "subscription_updated" webhook notifying us about a failed payment.+ </p>+ <br />++ <label>+ <input type="radio" name="status" value="paused" /> Set to+ <b class="text-red-400">PAUSED</b>+ </label>+ <br />+ <p class="text-xs ml-4 pl-1 italic text-gray-500">+ Simulate a "subscription_updated" webhook notifying us about the failure of the final payment attempt.+ </p>+ <br />+ <% end %>++ <%= if @current_status == Plausible.Billing.Subscription.Status.past_due() do %>+ Current subscription status: <b class="text-yellow-400">PAST DUE</b>+ <br /><br />++ <label>+ <input type="radio" name="status" value="active" /> Set to+ <b class="text-green-400">ACTIVE</b>+ </label>+ <br />+ <p class="text-xs ml-4 pl-1 italic text-gray-500">+ Simulate a "subscription_updated" webhook notifying us about a successful payment.+ </p>+ <br />++ <label>+ <input type="radio" name="status" value="paused" /> Set to+ <b class="text-red-400">PAUSED</b>+ </label>+ <br />+ <p class="text-xs ml-4 pl-1 italic text-gray-500">+ Simulate a "subscription_updated" webhook notifying us about the failure of the final payment attempt.+ </p>+ <br />+ <% end %>++ <%= if @current_status == Plausible.Billing.Subscription.Status.paused() do %>+ Current subscription status: <b class="text-red-400">PAUSED</b>+ <br /><br />++ <label>+ <input type="radio" name="status" value="active" /> Set to+ <b class="text-green-400">ACTIVE</b>+ </label>+ <br />+ <p class="text-xs ml-4 pl-1 italic text-gray-500">+ Simulate a "subscription_payment_succeeded" webhook notifying us about a successful payment. On production+ this action would correspond to the customer updating their billing details, and Paddle charging their card+ successfully.+ </p>+ <br />++ <label>+ <input type="radio" name="status" value="past_due" disabled />+ <p class="inline text-gray-500">Set to <b>PAST DUE</b> (not available)</p>+ </label>+ <br />+ <p class="text-xs ml-4 pl-1 italic text-gray-500">+ A status change from "paused" to "past_due" cannot happen naturally, and is therefore prohibited in this flow. Please update first to "active", and then to "past_due".+ </p>+ <br />+ <% end %>++ <div class="flex items-center justify-between mt-10">+ <span class="flex rounded-md shadow-sm">+ <a+ href={@back_link}+ type="button"+ class="inline-flex items-center px-4 py-2 border border-gray-300 dark:border-gray-500 text-sm leading-5 font-medium rounded-md text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-800 hover:text-gray-500 dark:hover:text-gray-200 focus:outline-none focus:border-blue-300 focus:ring active:text-gray-800 dark:active:text-gray-200 active:bg-gray-50 transition ease-in-out duration-150"+ >+ Back+ </a>+ </span>+ <button id="submit-button" type="submit" class="button">+ Update Subscription+ </button>+ </div>+ </form>+ <script>+ document.querySelectorAll('input[name="status"]')[0].checked = true+ </script>+</.focus_box>diff --git a/test/support/dev/views/dev_subscription_view.ex b/test/support/dev/views/dev_subscription_view.exnew file mode 100644index 0000000000..1e051e91ee--- /dev/null+++ b/test/support/dev/views/dev_subscription_view.ex@@ -0,0 +1,12 @@+defmodule PlausibleWeb.DevSubscriptionView do+ use Plausible++ on_ee do+ use Phoenix.View,+ root: "test/support/dev/templates"++ require Plausible.Billing.Subscription.Status+ import PlausibleWeb.Components.Generic+ alias PlausibleWeb.Router.Helpers, as: Routes+ end+end