Skip to content

Commit

Permalink
Keep consistent Stripe customer in V2 and V3 Elements
Browse files Browse the repository at this point in the history
  • Loading branch information
brchristian committed Aug 8, 2020
1 parent 4f67582 commit 5d43fed
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions app/models/spree/payment_method/stripe_credit_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ def create_profile(payment)
creditcard = source
end

user_stripe_payment_sources = payment.source.user&.wallet&.wallet_payment_sources&.select do |wps|
wps.payment_source.payment_method.type == 'Spree::PaymentMethod::StripeCreditCard'
end
if user_stripe_payment_sources.present?
customer_id = user_stripe_payment_sources.map {|ps| ps.payment_source&.gateway_customer_profile_id }.compact.last
options[:customer] = customer_id
end

response = gateway.store(creditcard, options)
if response.success?
if v3_intents?
Expand All @@ -130,8 +138,8 @@ def create_profile(payment)
else
payment.source.update!(
cc_type: payment.source.cc_type,
gateway_customer_profile_id: response.params['id'],
gateway_payment_profile_id: response.params['default_source'] || response.params['default_card']
gateway_customer_profile_id: options[:customer] ? response.params['customer'] : response.params['id'],
gateway_payment_profile_id: options[:customer] ? response.params['id'] : response.params['default_source'] || response.params['default_card']
)
end
else
Expand Down

0 comments on commit 5d43fed

Please sign in to comment.