Skip to content

Commit e9362ea

Browse files
throw more specific error when accessing Invoice.payment_intent (#1736)
1 parent d76bd0f commit e9362ea

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

lib/stripe/stripe_object.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,19 @@ def update_attributes(values, opts = {}, dirty: true)
171171
end
172172

173173
def [](key)
174-
@values[key.to_sym]
174+
key_sym = key.to_sym
175+
return @values[key_sym] if @values.key?(key_sym)
176+
177+
# super specific one-off case to help users debug this property disappearing
178+
# see also: https://go/j/DEVSDK-2835
179+
if is_a?(Invoice) && key_sym == :payment_intent
180+
raise KeyError,
181+
"The 'payment_intent' attribute is no longer available on Invoice objects. " \
182+
"See the docs for more details: https://docs.stripe.com/changelog/basil/2025-03-31/" \
183+
"add-support-for-multiple-partial-payments-on-invoices#why-is-this-a-breaking-change"
184+
end
185+
186+
nil
175187
end
176188

177189
def []=(key, value)
@@ -432,6 +444,15 @@ class << self; self; end
432444
begin
433445
super
434446
rescue NoMethodError => e
447+
# super specific one-off case to help users debug this property disappearing
448+
# see also: https://go/j/DEVSDK-2835
449+
if is_a?(Invoice) && name == :payment_intent
450+
raise NoMethodError,
451+
"The 'payment_intent' attribute is no longer available on Invoice objects. " \
452+
"See the docs for more details: https://docs.stripe.com/changelog/basil/2025-03-31/" \
453+
"add-support-for-multiple-partial-payments-on-invoices#why-is-this-a-breaking-change"
454+
end
455+
435456
# If we notice the accessed name of our set of transient values we can
436457
# give the user a slightly more helpful error message. If not, just
437458
# raise right away.

test/stripe/stripe_object_test.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,5 +785,44 @@ class WithAdditiveObjectParam < Stripe::StripeObject # rubocop:todo Lint/Constan
785785
assert account.company.is_a?(Stripe::Account::Company) if account.respond_to?(:company) && account.company
786786
end
787787
end
788+
789+
should "raise special error for Invoice.payment_intent" do
790+
is_good_error = lambda do |e|
791+
e.message.include?("multiple-partial-payments-on-invoices")
792+
end
793+
794+
i = Stripe::Invoice.construct_from({})
795+
796+
# Test attribute access
797+
e = assert_raises(NoMethodError) do
798+
i.payment_intent
799+
end
800+
assert is_good_error.call(e)
801+
802+
# Test hash access
803+
e = assert_raises(KeyError) do
804+
i[:payment_intent]
805+
end
806+
assert is_good_error.call(e)
807+
808+
# Only that property gets the special error
809+
e = assert_raises(NoMethodError) do
810+
i.blah
811+
end
812+
assert !is_good_error.call(e)
813+
814+
# Other properties don't get the special error with hash access
815+
assert_nil i[:blah]
816+
817+
# Other classes don't have that special error
818+
so = Stripe::StripeObject.construct_from({})
819+
e = assert_raises(NoMethodError) do
820+
so.payment_intent
821+
end
822+
assert !is_good_error.call(e)
823+
824+
# Hash access on non-Invoice objects returns nil
825+
assert_nil so[:payment_intent]
826+
end
788827
end
789828
end

0 commit comments

Comments
 (0)