Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: Fix account scoping for event notification handler callback clients
pr_url: https://github.com/stripe/stripe-python/pull/1911
semver_level: patch
---

- Fix callback clients to use the event's Stripe context and preserve the original client's non-account configuration.
- Fix API errors when using an event notification handler with a client configured with a Stripe account.
1 change: 0 additions & 1 deletion stripe/_stripe_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ def with_stripe_context(
"""
return StripeClient(
api_key=self._requestor.api_key, # type: ignore
stripe_account=self._requestor._options.stripe_account,
stripe_context=stripe_context,
stripe_version=self._requestor._options.stripe_version,
base_addresses=self._requestor._options.base_addresses,
Expand Down
6 changes: 5 additions & 1 deletion tests/test_event_notification_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class TestEventNotificationHandler:
def stripe_client(self, http_client_mock: HTTPClientMock) -> StripeClient:
return StripeClient(
api_key="sk_test_1234",
stripe_account="acct_123",
stripe_context=StripeContext.parse("original_context_123"),
http_client=http_client_mock.get_mock_http_client(),
)
Expand Down Expand Up @@ -249,12 +250,14 @@ def test_handler_uses_event_stripe_context(
) -> None:
"""Test that the handler receives a client with stripe_context from the event"""
received_context: Optional[StripeContext | str] = None
received_account: Optional[str] = None

def handler(
event: V1BillingMeterErrorReportTriggeredEventNotification,
client: StripeClient,
) -> None:
nonlocal received_context
nonlocal received_account, received_context
received_account = client._requestor._options.stripe_account
received_context = client._requestor._options.stripe_context

event_handler.on_v1_billing_meter_error_report_triggered(handler)
Expand All @@ -268,6 +271,7 @@ def handler(
event_handler.handle(v1_billing_meter_payload, sig_header)

assert str(received_context) == "event_context_456"
assert received_account is None

def test_stripe_context_restored_after_handler_success(
self,
Expand Down