Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

SD-942 hide outdated cards in checkout (#10503)

Kacper Mękarski5105877687b4
diff --git a/core/app/models/concerns/spree/user_payment_source.rb b/core/app/models/concerns/spree/user_payment_source.rb
index 2d0cd6406d..d9a745d50c 100644
--- a/core/app/models/concerns/spree/user_payment_source.rb
+++ b/core/app/models/concerns/spree/user_payment_source.rb
@@ -9,7 +9,7 @@ module Spree
end
def payment_sources
- credit_cards.with_payment_profile
+ credit_cards.with_payment_profile.not_expired
end
def drop_payment_source(source)
diff --git a/core/app/models/spree/credit_card.rb b/core/app/models/spree/credit_card.rb
index 07abf430c5..30fc99cd2f 100644
--- a/core/app/models/spree/credit_card.rb
+++ b/core/app/models/spree/credit_card.rb
@@ -37,6 +37,11 @@ module Spree
scope :with_payment_profile, -> { where.not(gateway_customer_profile_id: nil) }
scope :default, -> { where(default: true) }
+ scope :not_expired, lambda {
+ where('CAST(spree_credit_cards.year AS DECIMAL) > ?', Time.current.year).
+ or(where('CAST(spree_credit_cards.year AS DECIMAL) = ?', Time.current.year).
+ where('CAST(spree_credit_cards.month AS DECIMAL) >= ?', Time.current.month))
+ }
# needed for some of the ActiveMerchant gateways (eg. SagePay)
alias_attribute :brand, :cc_type
diff --git a/core/spec/models/spree/concerns/user_methods_spec.rb b/core/spec/models/spree/concerns/user_methods_spec.rb
index 595ea202c4..0b824bf2f2 100644
--- a/core/spec/models/spree/concerns/user_methods_spec.rb
+++ b/core/spec/models/spree/concerns/user_methods_spec.rb
@@ -77,4 +77,34 @@ describe Spree::UserMethods do
test_user.destroy
end
end
+
+ describe '#payment_sources' do
+ subject { test_user.payment_sources }
+
+ let(:next_year) { DateTime.now.year + 1 }
+ let(:previous_year) { DateTime.now.year - 1 }
+ let(:gateway_customer_profile_id) { 'SDS-4231' }
+ let(:other_user) { create :user }
+
+ let!(:valid_credit_card) { create(:credit_card, user: test_user, gateway_customer_profile_id: gateway_customer_profile_id, year: next_year) }
+ let!(:other_user_credit_card) { create(:credit_card, user: other_user, gateway_customer_profile_id: gateway_customer_profile_id, year: next_year) }
+ let!(:blank_payment_profile_credit_card) { create(:credit_card, user: test_user, gateway_customer_profile_id: nil, year: next_year) }
+ let!(:outdated_credit_card) { create(:credit_card, user: test_user, gateway_customer_profile_id: gateway_customer_profile_id, year: previous_year) }
+
+ it 'includes only not expired credit cards with payment profile that belong to subject user' do
+ expect(subject).to include(valid_credit_card)
+ end
+
+ it 'does not include credit cards that belong to other user' do
+ expect(subject).not_to include(other_user_credit_card)
+ end
+
+ it 'does not include credit cards without payment profile' do
+ expect(subject).not_to include(blank_payment_profile_credit_card)
+ end
+
+ it 'does not include outdated credit cards' do
+ expect(subject).not_to include(outdated_credit_card)
+ end
+ end
end
diff --git a/core/spec/models/spree/credit_card_spec.rb b/core/spec/models/spree/credit_card_spec.rb
index 0614a1e91e..dad4b0e2c6 100644
--- a/core/spec/models/spree/credit_card_spec.rb
+++ b/core/spec/models/spree/credit_card_spec.rb
@@ -338,4 +338,35 @@ describe Spree::CreditCard, type: :model do
expect { second.update!(default: true) }.not_to raise_error
end
+
+ describe 'scopes' do
+ describe '#not_expired' do
+ let(:previous_year) { DateTime.now.year - 1 }
+ let(:current_year) { DateTime.now.year }
+ let(:next_year) { DateTime.now.year + 1 }
+ let(:past_month) { DateTime.now.month - 1 }
+ let(:current_month) { DateTime.now.month }
+ let(:future_month) { DateTime.now.month + 1 }
+
+ let!(:outdated_credit_card_one) { create(:credit_card, year: previous_year, month: past_month) }
+ let!(:outdated_credit_card_two) { create(:credit_card, year: previous_year, month: current_month) }
+ let!(:outdated_credit_card_three) { create(:credit_card, year: previous_year, month: future_month) }
+ let!(:outdated_credit_card_four) { create(:credit_card, year: current_year, month: past_month) }
+ let!(:not_expired_credit_card_one) { create(:credit_card, year: current_year, month: current_month) }
+ let!(:not_expired_credit_card_two) { create(:credit_card, year: current_year, month: future_month) }
+ let!(:not_expired_credit_card_three) { create(:credit_card, year: next_year, month: past_month) }
+ let!(:not_expired_credit_card_four) { create(:credit_card, year: next_year, month: current_month) }
+ let!(:not_expired_credit_card_five) { create(:credit_card, year: next_year, month: future_month) }
+
+ it 'includes only not expired credit cards' do
+ expect(described_class.not_expired).to include(not_expired_credit_card_one, not_expired_credit_card_two, not_expired_credit_card_three,
+ not_expired_credit_card_four, not_expired_credit_card_five)
+ end
+
+ it 'does not include outdated credit cards' do
+ expect(described_class.not_expired).not_to include(outdated_credit_card_one, outdated_credit_card_two, outdated_credit_card_three,
+ outdated_credit_card_four)
+ end
+ end
+ end
end