Merge pull request #9437 from jibees/8897-preselect-default-card-and-do-not-allow-to-charge-if-no-card-is-marked-as-default

User account, Credit cards: Do not allow to check "Allow charges" if no credit cards marked as default
This commit is contained in:
Filipe
2022-08-11 16:34:42 +01:00
committed by GitHub
6 changed files with 58 additions and 2 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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