Extract Stripe credit card deletion logic to a service object

This commit is contained in:
Arun Kumar Mohan
2020-12-11 19:42:52 -05:00
parent a745fceb53
commit 1edebade78
2 changed files with 22 additions and 10 deletions

View File

@@ -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

View 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