From a3cb1e6ecc68e5ee49b58eff7c78fc4e10d784cc Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Thu, 21 Jul 2022 14:42:36 +0200 Subject: [PATCH 1/6] Can `Allow charges` only if one card is marked as `default` --- .../darkswarm/controllers/credit_cards_controller.js.coffee | 4 ++++ app/views/spree/users/_authorised_shops.html.haml | 1 + 2 files changed, 5 insertions(+) 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..8e14d96627 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,7 @@ angular.module('Darkswarm').controller "CreditCardsCtrl", ($scope, $http, Credit ).finally -> window.location.reload() + + $scope.hasOneDefaultSavedCards = () -> + $scope.savedCreditCards.length > 0 && $scope.savedCreditCards.some((card) -> card.is_default) + diff --git a/app/views/spree/users/_authorised_shops.html.haml b/app/views/spree/users/_authorised_shops.html.haml index 1d033da72f..05aefc9119 100644 --- a/app/views/spree/users/_authorised_shops.html.haml +++ b/app/views/spree/users/_authorised_shops.html.haml @@ -12,5 +12,6 @@ name: 'allow_charges', ng: { model: 'customer.allow_charges', change: 'customer.update()', + disabled: "!hasOneDefaultSavedCards()", "true-value" => "true", "false-value" => "false" } } From 875c22346eb0aa83204de526a9c6987a984ce287 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Thu, 21 Jul 2022 14:59:17 +0200 Subject: [PATCH 2/6] Add a tooltip on `` when input is disabled, ie no default saved cards Co-Authored-By: Maikel --- app/views/spree/users/_authorised_shops.html.haml | 2 +- config/locales/en.yml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/views/spree/users/_authorised_shops.html.haml b/app/views/spree/users/_authorised_shops.html.haml index 05aefc9119..51fdacf3ef 100644 --- a/app/views/spree/users/_authorised_shops.html.haml +++ b/app/views/spree/users/_authorised_shops.html.haml @@ -7,7 +7,7 @@ %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', diff --git a/config/locales/en.yml b/config/locales/en.yml index d54cf9ecdb..d3242cafe5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -4232,6 +4232,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: To allow charges, you should have one credit cards marked as default localized_number: invalid_format: has an invalid format. Please enter a number. api: From 34c8748b9ca42055a2d7d45253e0fd6ac34c97a7 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Thu, 21 Jul 2022 15:58:00 +0200 Subject: [PATCH 3/6] Mark the next credit cards with payment profile as default --- app/controllers/spree/credit_cards_controller.rb | 13 ++++++++++++- .../spree/credit_cards_controller_spec.rb | 11 +++++++++++ spec/system/consumer/account/cards_spec.rb | 16 ++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) 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/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..12d79f1731 100644 --- a/spec/system/consumer/account/cards_spec.rb +++ b/spec/system/consumer/account/cards_spec.rb @@ -105,5 +105,21 @@ 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 end end From 0bb90d764c325464da921bfdee16db2908891156 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Fri, 22 Jul 2022 14:44:11 +0200 Subject: [PATCH 4/6] Allow charges is disabled if no saved cards marked as default --- spec/system/consumer/account/cards_spec.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/spec/system/consumer/account/cards_spec.rb b/spec/system/consumer/account/cards_spec.rb index 12d79f1731..07e6422f73 100644 --- a/spec/system/consumer/account/cards_spec.rb +++ b/spec/system/consumer/account/cards_spec.rb @@ -121,5 +121,18 @@ describe "Credit Cards", js: true do 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 From be568cdfa68570be5ee1bfc008e991070656bda5 Mon Sep 17 00:00:00 2001 From: jibees Date: Tue, 26 Jul 2022 12:49:44 +0200 Subject: [PATCH 5/6] Update config/locales/en.yml Co-authored-by: Maikel --- config/locales/en.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/locales/en.yml b/config/locales/en.yml index d3242cafe5..373c426796 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -4232,7 +4232,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: To allow charges, you should have one credit cards marked as default + 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: From 59c9f0957b49bc1452427a80869bbe27fae1065f Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 27 Jul 2022 11:14:18 +1000 Subject: [PATCH 6/6] Simplify default card logic --- .../darkswarm/controllers/credit_cards_controller.js.coffee | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 8e14d96627..6175b4d159 100644 --- a/app/assets/javascripts/darkswarm/controllers/credit_cards_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/credit_cards_controller.js.coffee @@ -22,5 +22,4 @@ angular.module('Darkswarm').controller "CreditCardsCtrl", ($scope, $http, Credit $scope.hasOneDefaultSavedCards = () -> - $scope.savedCreditCards.length > 0 && $scope.savedCreditCards.some((card) -> card.is_default) - + $scope.savedCreditCards.some((card) -> card.is_default)