Skip to content

Commit

Permalink
Fix return value of Customer#delete_discount
Browse files Browse the repository at this point in the history
`Customer#delete_discount` has been broken for some time in that it
tries to re-initialize `self` (which is a customer) with a received
discount response. This is incorrect and leads to various problems.

Here, we redefine the return value of `delete_discount` as a discount,
and have it no longer mutate the object on which is was called. We add a
comment as well just to help flag some of the behavior which could
potentially be confusing.

Fixes #963.
  • Loading branch information
brandur committed Feb 8, 2021
1 parent de27275 commit b2c3b92
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/stripe/resources/customer.rb
Expand Up @@ -28,9 +28,14 @@ class << self
alias detach_source delete_source
end

# Deletes a discount associated with the customer.
#
# Returns the deleted discount. The customer object is not updated,
# so you must call `refresh` on it to get a new version with the
# discount removed.
def delete_discount
resp, opts = execute_resource_request(:delete, resource_url + "/discount")
initialize_from(resp.data, opts, true)
Util.convert_to_stripe_object(resp.data, opts)
end
end
end
4 changes: 2 additions & 2 deletions test/stripe/customer_test.rb
Expand Up @@ -56,9 +56,9 @@ class CustomerTest < Test::Unit::TestCase
context "#delete_discount" do
should "delete a discount" do
customer = Stripe::Customer.retrieve("cus_123")
customer = customer.delete_discount
discount = customer.delete_discount
assert_requested :delete, "#{Stripe.api_base}/v1/customers/cus_123/discount"
assert customer.is_a?(Stripe::Customer)
assert discount.is_a?(Stripe::Discount)
end
end

Expand Down

0 comments on commit b2c3b92

Please sign in to comment.