diff --git a/app/controllers/spree/credit_cards_controller.rb b/app/controllers/spree/credit_cards_controller.rb index b084fc4707..652e9878b0 100644 --- a/app/controllers/spree/credit_cards_controller.rb +++ b/app/controllers/spree/credit_cards_controller.rb @@ -47,6 +47,7 @@ module Spree # Using try because we may not have a card here if @credit_card.try(:destroy) + remove_shop_authorizations if @credit_card.is_default flash[:success] = I18n.t(:card_has_been_removed, number: "x-#{@credit_card.last_digits}") else flash[:error] = I18n.t(:card_could_not_be_removed) diff --git a/spec/controllers/spree/credit_cards_controller_spec.rb b/spec/controllers/spree/credit_cards_controller_spec.rb index 280119aa69..82da0f59d4 100644 --- a/spec/controllers/spree/credit_cards_controller_spec.rb +++ b/spec/controllers/spree/credit_cards_controller_spec.rb @@ -188,6 +188,26 @@ describe Spree::CreditCardsController, type: :controller do expect(flash[:success]).to eq I18n.t(:card_has_been_removed, number: "x-#{card.last_digits}") expect(response).to redirect_to spree.account_path(anchor: 'cards') end + + context "the card is the default card and there are existing authorizations for the user" do + before do + card.update_attribute(:is_default, true) + end + let!(:customer1) { create(:customer, allow_charges: true) } + let!(:customer2) { create(:customer, allow_charges: true) } + + it "removes the authorizations" do + customer1.user = card.user + customer2.user = card.user + customer1.save + customer2.save + expect(customer1.reload.allow_charges).to be true + expect(customer2.reload.allow_charges).to be true + spree_delete :destroy, params + expect(customer1.reload.allow_charges).to be false + expect(customer2.reload.allow_charges).to be false + end + end end end end