remove cloned cards after removing the platform card

This commit is contained in:
Andy Brett
2020-11-20 14:33:27 -08:00
parent bc3fd8c50c
commit 9c544ef2f4
2 changed files with 18 additions and 5 deletions

View File

@@ -63,8 +63,9 @@ module Spree
# It destroys the whole customer object
def destroy_at_stripe
stripe_customer = Stripe::Customer.retrieve(@credit_card.gateway_customer_profile_id, {})
Stripe::CreditCardCloner.new.destroy_clones(@credit_card)
stripe_customer = Stripe::Customer.retrieve(@credit_card.gateway_customer_profile_id, {})
stripe_customer&.delete unless stripe_customer.deleted?
end

View File

@@ -17,11 +17,23 @@
module Stripe
class CreditCardCloner
def find_or_clone(credit_card, connected_account_id)
if card = find_cloned_card(credit_card, connected_account_id)
card
def find_or_clone(card, connected_account_id)
if cloned_card = find_cloned_card(card, connected_account_id)
cloned_card
else
clone(credit_card, connected_account_id)
clone(card, connected_account_id)
end
end
def destroy_clones(card)
card.user.customers.each do |customer|
next unless stripe_account = customer.enterprise.stripe_account&.stripe_user_id
customer_id, _payment_method_id = find_cloned_card(card, stripe_account)
next unless customer_id
customer = Stripe::Customer.retrieve(customer_id, { stripe_account: stripe_account })
customer&.delete unless customer.deleted?
end
end