mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Extract Stripe credit card deletion logic to a service object
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
require 'stripe/credit_card_clone_destroyer'
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Spree
|
||||
class CreditCardsController < BaseController
|
||||
@@ -42,7 +42,7 @@ module Spree
|
||||
@credit_card = Spree::CreditCard.find_by(id: params[:id])
|
||||
if @credit_card
|
||||
authorize! :destroy, @credit_card
|
||||
destroy_at_stripe
|
||||
Stripe::CreditCardRemover.new(@credit_card).call
|
||||
end
|
||||
|
||||
# Using try because we may not have a card here
|
||||
@@ -64,14 +64,6 @@ module Spree
|
||||
@credit_card.user.customers.update_all(allow_charges: false)
|
||||
end
|
||||
|
||||
# It destroys the whole customer object
|
||||
def destroy_at_stripe
|
||||
Stripe::CreditCardCloneDestroyer.new.destroy_clones(@credit_card)
|
||||
|
||||
stripe_customer = Stripe::Customer.retrieve(@credit_card.gateway_customer_profile_id, {})
|
||||
stripe_customer&.delete unless stripe_customer.deleted?
|
||||
end
|
||||
|
||||
def create_customer(token)
|
||||
Stripe::Customer.create(email: spree_current_user.email, source: token)
|
||||
end
|
||||
|
||||
20
app/services/stripe/credit_card_remover.rb
Normal file
20
app/services/stripe/credit_card_remover.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'stripe/credit_card_clone_destroyer'
|
||||
|
||||
module Stripe
|
||||
class CreditCardRemover
|
||||
def initialize(credit_card)
|
||||
@credit_card = credit_card
|
||||
end
|
||||
|
||||
def call
|
||||
Stripe::CreditCardCloneDestroyer.new.destroy_clones(@credit_card)
|
||||
|
||||
stripe_customer = Stripe::Customer.retrieve(@credit_card.gateway_customer_profile_id, {})
|
||||
return unless stripe_customer
|
||||
|
||||
stripe_customer.delete unless stripe_customer.deleted?
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user