Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(data-warehouse): guide users to manual setup on Stripe webhook account error (#62317)

posthog[bot]15ba5a6b7f85
diff --git a/posthog/temporal/data_imports/sources/stripe/stripe.py b/posthog/temporal/data_imports/sources/stripe/stripe.py
index 53f43d2563ea..cd04eb963c51 100644
--- a/posthog/temporal/data_imports/sources/stripe/stripe.py
+++ b/posthog/temporal/data_imports/sources/stripe/stripe.py
@@ -558,6 +558,24 @@ def _all_known_webhook_events() -> list[str]:
return [e for e in possible_event_values if any(e.startswith(f"{p}.") for p in prefixes_set)]
+def _is_stripe_account_access_error(error: Exception, error_str: str) -> bool:
+ """Detect Stripe's account-access/account-mismatch rejection (code ``account_invalid``).
+
+ A restricted key sent with a ``stripe_account`` header that doesn't match the key's own
+ account makes Stripe reject the request for the account rather than the webhook scope, so it
+ never matches the permission/403/forbidden branch. Surfacing the raw message strands the user;
+ classifying it lets us point them at the manual-setup fallback instead.
+ """
+ if getattr(error, "code", None) == "account_invalid":
+ return True
+ lowered = error_str.lower()
+ return (
+ "does not have access to account" in lowered
+ or "application access may have been revoked" in lowered
+ or "no such account" in lowered
+ )
+
+
def create_webhook(api_key: str, stripe_account_id: str | None, webhook_url: str) -> WebhookCreationResult:
logger = LOGGER.bind()
@@ -593,12 +611,25 @@ def create_webhook(api_key: str, stripe_account_id: str | None, webhook_url: str
return WebhookCreationResult(success=True, extra_inputs=extra_inputs)
except Exception as e:
- error_str = str(e)
+ error_str = _clean_stripe_error_message(str(e))
logger.warning(
"Failed to create Stripe webhook",
error=error_str,
)
+ # Check account access before the permission branch — an account-access rejection can carry a
+ # 403 and would otherwise be misclassified as a missing webhook scope.
+ if _is_stripe_account_access_error(e, error_str):
+ return WebhookCreationResult(
+ success=False,
+ error=(
+ "Stripe rejected the request because your API key isn't authorized for the configured "
+ "Stripe account. The 'Account id' in your source settings only applies to Stripe Connect "
+ "platform accounts — remove or correct it if your key belongs directly to the account, "
+ "then retry. Otherwise, set up the webhook manually below."
+ ),
+ )
+
if "permission" in error_str.lower() or "403" in error_str or "forbidden" in error_str.lower():
return WebhookCreationResult(
success=False,
diff --git a/posthog/temporal/tests/data_imports/stripe/test_stripe_source.py b/posthog/temporal/tests/data_imports/stripe/test_stripe_source.py
index c6154ab136bd..e0b6c7b242d1 100644
--- a/posthog/temporal/tests/data_imports/stripe/test_stripe_source.py
+++ b/posthog/temporal/tests/data_imports/stripe/test_stripe_source.py
@@ -43,6 +43,12 @@ from .data import BALANCE_TRANSACTIONS
pytestmark = pytest.mark.usefixtures("minio_client")
+def _exception_with_code(message: str, code: str) -> Exception:
+ error = Exception(message)
+ error.code = code # type: ignore[attr-defined]
+ return error
+
+
@pytest.fixture
def external_data_source(team):
source = ExternalDataSource.objects.create(
@@ -982,3 +988,28 @@ class TestCreateWebhook:
assert result.success is False
assert result.error is not None
assert "permission" in result.error.lower()
+
+ @parameterized.expand(
+ [
+ # (name, exception)
+ ("message", Exception("The provided key does not have access to account 'acct_123'.")),
+ ("revoked", Exception("Application access may have been revoked.")),
+ ("account_invalid_code", _exception_with_code("403 Forbidden", "account_invalid")),
+ ]
+ )
+ def test_account_access_error_points_to_manual_setup(self, _name, exception):
+ from posthog.temporal.data_imports.sources.stripe.stripe import create_webhook
+
+ mock_client = mock.MagicMock()
+ mock_client.webhook_endpoints.create.side_effect = exception
+
+ with mock.patch("posthog.temporal.data_imports.sources.stripe.stripe.StripeClient", return_value=mock_client):
+ result = create_webhook("rk_test", "acct_123", "https://x")
+
+ assert result.success is False
+ assert result.error is not None
+ # Account-access errors must surface the actionable account guidance, never the raw fallback
+ # or the generic webhook-scope permission message.
+ assert "account" in result.error.lower()
+ assert "Failed to create webhook automatically" not in result.error
+ assert "permission to create webhooks" not in result.error