diff --git a/app/assets/javascripts/darkswarm/controllers/credit_cards_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/credit_cards_controller.js.coffee index 1b1ee5fd97..6175b4d159 100644 --- a/app/assets/javascripts/darkswarm/controllers/credit_cards_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/credit_cards_controller.js.coffee @@ -20,3 +20,6 @@ angular.module('Darkswarm').controller "CreditCardsCtrl", ($scope, $http, Credit ).finally -> window.location.reload() + + $scope.hasOneDefaultSavedCards = () -> + $scope.savedCreditCards.some((card) -> card.is_default) diff --git a/app/controllers/spree/credit_cards_controller.rb b/app/controllers/spree/credit_cards_controller.rb index 3dfe778e81..7ae0a33334 100644 --- a/app/controllers/spree/credit_cards_controller.rb +++ b/app/controllers/spree/credit_cards_controller.rb @@ -49,7 +49,10 @@ 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 + if @credit_card.is_default + remove_shop_authorizations + mark_as_default_next_credit_card if credit_cards_with_payment_profile.count > 0 + end flash[:success] = I18n.t(:card_has_been_removed, number: "x-#{@credit_card.last_digits}") else flash[:error] = I18n.t(:card_could_not_be_removed) @@ -67,6 +70,14 @@ module Spree @credit_card.user.customers.update_all(allow_charges: false) end + def mark_as_default_next_credit_card + credit_cards_with_payment_profile.first.update(is_default: true) + end + + def credit_cards_with_payment_profile + spree_current_user.credit_cards.with_payment_profile + end + def create_customer(token) Stripe::Customer.create(email: spree_current_user.email, source: token) end diff --git a/app/views/spree/users/_authorised_shops.html.haml b/app/views/spree/users/_authorised_shops.html.haml index 1d033da72f..51fdacf3ef 100644 --- a/app/views/spree/users/_authorised_shops.html.haml +++ b/app/views/spree/users/_authorised_shops.html.haml @@ -7,10 +7,11 @@ %th= t(".allow_charges?") %tr.customer{ id: "customer{{ customer.id }}", ng: { repeat: "customer in customers" } } %td.shop{ ng: { bind: 'shopsByID[customer.enterprise_id].name' } } - %td.allow_charges + %td.allow_charges{ tooltip: "{{ hasOneDefaultSavedCards() ? null : \'" + t('.no_default_saved_cards_tooltip') + "\' }}" } %input{ type: 'checkbox', name: 'allow_charges', ng: { model: 'customer.allow_charges', change: 'customer.update()', + disabled: "!hasOneDefaultSavedCards()", "true-value" => "true", "false-value" => "false" } } diff --git a/config/locales/en.yml b/config/locales/en.yml index 959e95dc97..c432924a03 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -4238,6 +4238,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using authorised_shops: shop_name: "Shop Name" allow_charges?: "Allow Charges to Default Card?" + no_default_saved_cards_tooltip: You need to mark one credit card as default to allow charges. localized_number: invalid_format: has an invalid format. Please enter a number. api: diff --git a/spec/controllers/spree/credit_cards_controller_spec.rb b/spec/controllers/spree/credit_cards_controller_spec.rb index 2a9d5ae2ad..602d384038 100644 --- a/spec/controllers/spree/credit_cards_controller_spec.rb +++ b/spec/controllers/spree/credit_cards_controller_spec.rb @@ -213,6 +213,17 @@ describe Spree::CreditCardsController, type: :controller do expect(customer1.reload.allow_charges).to be false expect(customer2.reload.allow_charges).to be false end + + context "when has any other saved cards" do + let!(:second_card) { + create(:stored_credit_card, user_id: user.id, gateway_customer_profile_id: 'cus_AZNMJ') + } + + it "should assign the second one as the default one" do + spree_delete :destroy, params + expect(Spree::CreditCard.find_by(id: second_card.id).is_default).to eq true + end + end end end end diff --git a/spec/system/consumer/account/cards_spec.rb b/spec/system/consumer/account/cards_spec.rb index c1b1c5980a..07e6422f73 100644 --- a/spec/system/consumer/account/cards_spec.rb +++ b/spec/system/consumer/account/cards_spec.rb @@ -105,5 +105,34 @@ describe "Credit Cards", js: true do expect(page).to have_content I18n.t('js.changes_saved') expect(customer.reload.allow_charges).to be true end + + it "assign the default card to the next one when the default is deleted" do + visit "/account" + find("a", text: /Credit Cards/i).click + + within(".card#card#{default_card.id}") do + click_button "Delete" + end + + expect(page).to have_content "Your card has been removed" + + within ".card#card#{non_default_card.id}" do + expect(find_field('default_card')).to be_checked + end + expect(non_default_card.reload.is_default).to be true + end + + context "when no default card" do + before do + default_card.destroy + end + + it "then all 'allow_charges' inputs are disabled" do + visit "/account" + find("a", text: /Credit Cards/i).click + + expect(find_field('allow_charges', disabled: true)).to be_truthy + end + end end end