🌐 AI搜索 & 代理 主页
Skip to content

Commit 0e8ad0b

Browse files
throw more specific error when accessing Invoice.payment_intent (#1681)
1 parent 684cafd commit 0e8ad0b

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

stripe/_stripe_object.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,15 @@ def __getitem__(self, k: str) -> Any:
216216
% (k, k, ", ".join(list(self.keys())))
217217
)
218218
else:
219+
from stripe._invoice import Invoice
220+
221+
# super specific one-off case to help users debug this property disappearing
222+
# see also: https://go/j/DEVSDK-2835
223+
if isinstance(self, Invoice) and k == "payment_intent":
224+
raise KeyError(
225+
"The 'payment_intent' attribute is no longer available on Invoice objects. See the docs for more details: https://docs.stripe.com/changelog/basil/2025-03-31/add-support-for-multiple-partial-payments-on-invoices#why-is-this-a-breaking-change"
226+
)
227+
219228
raise err
220229

221230
def __delitem__(self, k: str) -> None:

tests/test_stripe_object.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pytest
77

88
import stripe
9+
from stripe._invoice import Invoice
910
from stripe._stripe_object import StripeObject
1011

1112
# We use this because it has a map, "restriction.currency_options" from string -> CurrencyOptions nested class.
@@ -433,3 +434,36 @@ def test_can_update_api_key(self, http_client_mock):
433434
api_key="key2",
434435
stripe_account=None,
435436
)
437+
438+
def test_invoice_payment_method_gets_special_error(self):
439+
def is_good_error(e: Exception) -> bool:
440+
return "multiple-partial-payments-on-invoices" in str(e)
441+
442+
i = Invoice()
443+
444+
with pytest.raises(AttributeError) as e:
445+
i.payment_intent # type: ignore
446+
assert is_good_error(e.value)
447+
448+
with pytest.raises(KeyError) as e:
449+
i["payment_intent"]
450+
assert is_good_error(e.value)
451+
452+
# only that property gets the special error
453+
with pytest.raises(AttributeError) as e:
454+
i.blah # type: ignore
455+
assert not is_good_error(e.value)
456+
457+
with pytest.raises(KeyError) as e:
458+
i["blah"]
459+
assert not is_good_error(e.value)
460+
461+
# other classes don't have that special error
462+
so = StripeObject()
463+
with pytest.raises(AttributeError) as e:
464+
so.payment_intent # type: ignore
465+
assert not is_good_error(e.value)
466+
467+
with pytest.raises(KeyError) as e:
468+
so["payment_intent"]
469+
assert not is_good_error(e.value)

0 commit comments

Comments
 (0)