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(warehouse_sources): pin the API version on Stripe webhook endpoints (#94427)

Daniel RC620f66195f24
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/source.py
index 9c3bf7fe7eb2..98fa9ad65e88 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/source.py
@@ -491,7 +491,13 @@ If automatic creation failed with a permissions error, the fix depends on how yo
self, config: StripeSourceConfig, webhook_url: str, team_id: int, api_version: str | None = None
) -> WebhookCreationResult:
api_key = self._get_api_key(config, team_id)
- return create_webhook(api_key, config.stripe_account_id, webhook_url, auth_method=config.auth_method.selection)
+ return create_webhook(
+ api_key,
+ config.stripe_account_id,
+ webhook_url,
+ api_version=self.resolve_api_version(api_version),
+ auth_method=config.auth_method.selection,
+ )
def get_desired_webhook_events(
self, config: StripeSourceConfig, eligible_schema_names: list[str]
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py
index 91f947eda87c..65db09013929 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/stripe.py
@@ -1491,6 +1491,8 @@ def create_webhook(
api_key: str,
stripe_account_id: str | None,
webhook_url: str,
+ *,
+ api_version: str,
auth_method: Literal["api_key", "oauth"] = "api_key",
) -> WebhookCreationResult:
logger = LOGGER.bind()
@@ -1507,7 +1509,7 @@ def create_webhook(
client = StripeClient(
api_key,
stripe_account=stripe_account_id,
- stripe_version="2024-09-30.acacia",
+ stripe_version=api_version,
max_network_retries=2,
base_addresses=_stripe_base_addresses(),
http_client=_tracked_stripe_http_client(),
@@ -1518,6 +1520,11 @@ def create_webhook(
"url": webhook_url,
"enabled_events": filtered_events, # type: ignore
"description": "PostHog data warehouse webhook",
+ # Stripe renders delivered events at the account's default version unless the
+ # endpoint pins one, so an account on basil or later would send reshaped Invoice /
+ # Subscription payloads whose moved fields the canonical schema reads as NULL.
+ # The SDK types this as a Literal of the versions it shipped with, hence the ignore.
+ "api_version": api_version, # type: ignore[typeddict-item]
}
)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
index 32973d9e95e8..596441577ac2 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
@@ -1474,6 +1474,7 @@ class TestCreateWebhookPermissionErrorCopy:
api_key="sk_test_123",
stripe_account_id=None,
webhook_url="https://example.com/webhook",
+ api_version=STRIPE_API_VERSION_ACACIA,
auth_method=auth_method,
)
@@ -1495,6 +1496,7 @@ class TestCreateWebhookLimitErrorCopy:
api_key="sk_test_123",
stripe_account_id=None,
webhook_url="https://example.com/webhook",
+ api_version=STRIPE_API_VERSION_ACACIA,
)
assert result.success is False
diff --git a/products/warehouse_sources/backend/temporal/data_imports/tests/e2e/stripe/test_stripe_source.py b/products/warehouse_sources/backend/temporal/data_imports/tests/e2e/stripe/test_stripe_source.py
index f40f91fbbc8f..429244b34846 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/tests/e2e/stripe/test_stripe_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/tests/e2e/stripe/test_stripe_source.py
@@ -1119,7 +1119,7 @@ class TestCreateWebhook:
"products.warehouse_sources.backend.temporal.data_imports.sources.stripe.stripe.StripeClient",
return_value=mock_client,
):
- result = create_webhook("rk_test", None, url)
+ result = create_webhook("rk_test", None, url, api_version=STRIPE_API_VERSION_ACACIA)
assert result.success
assert result.extra_inputs == {"signing_secret": "whsec_abc"}
@@ -1128,6 +1128,9 @@ class TestCreateWebhook:
assert params["url"] == url
# Refactor guard: create must still register exactly the full known event set.
assert params["enabled_events"] == _all_known_webhook_events()
+ # An unpinned endpoint renders events at the account's own default API version, which
+ # silently empties the moved columns for any account on basil or later.
+ assert params["api_version"] == STRIPE_API_VERSION_ACACIA
def test_permission_error_returns_actionable_failure(self):
from products.warehouse_sources.backend.temporal.data_imports.sources.stripe.stripe import create_webhook
@@ -1139,12 +1142,33 @@ class TestCreateWebhook:
"products.warehouse_sources.backend.temporal.data_imports.sources.stripe.stripe.StripeClient",
return_value=mock_client,
):
- result = create_webhook("rk_test", None, "https://x")
+ result = create_webhook("rk_test", None, "https://x", api_version=STRIPE_API_VERSION_ACACIA)
assert result.success is False
assert result.error is not None
assert "permission" in result.error.lower()
+ def test_source_pins_the_resolved_version_on_the_endpoint(self):
+ endpoint = mock.MagicMock()
+ endpoint.secret = "whsec_abc"
+ mock_client = mock.MagicMock()
+ mock_client.webhook_endpoints.create.return_value = endpoint
+ config = StripeSourceConfig.from_dict(
+ {"auth_method": {"selection": "api_key", "stripe_secret_key": "sk_test_123"}}
+ )
+
+ with mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.stripe.stripe.StripeClient",
+ return_value=mock_client,
+ ):
+ result = StripeSource().create_webhook(config, "https://x", team_id=1)
+
+ assert result.success
+ _, kwargs = mock_client.webhook_endpoints.create.call_args
+ # The source's resolved pin has to survive all the way to the Stripe call — dropping it
+ # anywhere in between creates an unpinned endpoint and no error.
+ assert kwargs["params"]["api_version"] == StripeSource.default_version
+
@parameterized.expand(
[
# (name, exception)
@@ -1163,7 +1187,7 @@ class TestCreateWebhook:
"products.warehouse_sources.backend.temporal.data_imports.sources.stripe.stripe.StripeClient",
return_value=mock_client,
):
- result = create_webhook("rk_test", "acct_123", "https://x")
+ result = create_webhook("rk_test", "acct_123", "https://x", api_version=STRIPE_API_VERSION_ACACIA)
assert result.success is False
assert result.error is not None