Skip to content
Merged
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
17 changes: 14 additions & 3 deletions lib/stripe_mock/request_handlers/customers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ def delete_customer_discount(route, method_url, params, headers)
cus
end

# Helper to wrap a legacy source into a temporary PaymentMethod object
def legacy_source_to_payment_method(source)
Data.mock_payment_method({id: source[:id],
card: source.clone, # overwrite with the legacy source as the card subobject
created: Time.now.to_i,
customer: source[:customer],
type: 'card'}) || {}

end

def retrieve_payment_method(route, method_url, params, headers)
stripe_account = headers && headers[:stripe_account] || Stripe.api_key

Expand All @@ -171,14 +181,15 @@ def retrieve_payment_method(route, method_url, params, headers)
payment_method = payment_methods[payment_method_id]

if payment_method && payment_method[:customer] == customer_id
return payment_method
return payment_method.clone
end

# If not found or not attached correctly, attempt to find in the customer's legacy sources.
# If not found in payment_methods, attempt to find in the customer's legacy sources.
if customer[:sources] && customer[:sources][:data]
source = customer[:sources][:data].detect { |src| src[:id] == payment_method_id }
if source && source[:customer] == customer_id
return source
# Convert legacy source to a temporary PaymentMethod structure
return legacy_source_to_payment_method(source)
end
end

Expand Down