From e26a02a0fb7c26baf4921903115e299c089fb642 Mon Sep 17 00:00:00 2001 From: jamey Date: Sun, 22 Feb 2026 16:51:44 +0000 Subject: [PATCH] fix setup flow stale state and Stripe URL issues MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Onboarding: re-fetch setup_status() after provider/Stripe connect instead of manually patching the local assigns, which could miss admin_created and leave users stuck on the setup page with no way forward. Dev config: respect PHX_HOST for endpoint URL so Stripe checkout redirects to the correct host instead of always using localhost. Stripe setup: detect private/LAN IPs (10.x, 172.16-31.x, 192.168.x) as unreachable, not just localhost — prevents creating webhook endpoints that Stripe can never reach. Co-Authored-By: Claude Opus 4.6 --- config/dev.exs | 4 ++++ lib/berrypod/stripe/setup.ex | 17 +++++++++++++++-- lib/berrypod_web/live/setup/onboarding.ex | 16 ++-------------- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/config/dev.exs b/config/dev.exs index 15877c2..62a6996 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -21,6 +21,10 @@ config :berrypod, Berrypod.Repo, config :berrypod, BerrypodWeb.Endpoint, # Binding to loopback ipv4 address prevents access from other machines. # Change to `ip: {0, 0, 0, 0}` to allow access from other machines. + url: [ + host: System.get_env("PHX_HOST") || "localhost", + port: String.to_integer(System.get_env("PORT") || "4000") + ], http: [ip: {0, 0, 0, 0}, port: String.to_integer(System.get_env("PORT") || "4000")], check_origin: false, code_reloader: true, diff --git a/lib/berrypod/stripe/setup.ex b/lib/berrypod/stripe/setup.ex index 811f4a8..460c247 100644 --- a/lib/berrypod/stripe/setup.ex +++ b/lib/berrypod/stripe/setup.ex @@ -79,12 +79,25 @@ defmodule Berrypod.Stripe.Setup do end @doc """ - Returns true if the app is running on localhost (Stripe can't reach it). + Returns true if the app URL is unreachable from Stripe's servers + (localhost, loopback, or private/LAN IPs). """ def localhost? do url = BerrypodWeb.Endpoint.url() uri = URI.parse(url) - uri.host in ["localhost", "127.0.0.1", "0.0.0.0", "::1"] + unreachable_host?(uri.host) + end + + defp unreachable_host?(host) when host in ["localhost", "0.0.0.0", "::1"], do: true + + defp unreachable_host?(host) do + case :inet.parse_address(String.to_charlist(host)) do + {:ok, {127, _, _, _}} -> true + {:ok, {10, _, _, _}} -> true + {:ok, {172, b, _, _}} when b >= 16 and b <= 31 -> true + {:ok, {192, 168, _, _}} -> true + _ -> false + end end defp maybe_create_webhook(api_key) do diff --git a/lib/berrypod_web/live/setup/onboarding.ex b/lib/berrypod_web/live/setup/onboarding.ex index 583ca3e..39ce190 100644 --- a/lib/berrypod_web/live/setup/onboarding.ex +++ b/lib/berrypod_web/live/setup/onboarding.ex @@ -150,12 +150,7 @@ defmodule BerrypodWeb.Setup.Onboarding do case Products.create_provider_connection(params) do {:ok, connection} -> Products.enqueue_sync(connection) - - setup = %{ - socket.assigns.setup - | provider_connected: true, - provider_type: type - } + setup = Setup.setup_status() {:noreply, socket @@ -183,14 +178,7 @@ defmodule BerrypodWeb.Setup.Onboarding do case StripeSetup.connect(api_key) do {:ok, _result} -> - setup = %{socket.assigns.setup | stripe_connected: true} - - setup = - if setup.admin_created and setup.provider_connected do - %{setup | setup_complete: true} - else - setup - end + setup = Setup.setup_status() {:noreply, socket