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.

Fix guest credit card fingerprint validation (#14372)

Chiucbcfa98ce154
diff --git a/spree/core/app/models/spree/credit_card.rb b/spree/core/app/models/spree/credit_card.rb
index eaa28beafe..1707205333 100644
--- a/spree/core/app/models/spree/credit_card.rb
+++ b/spree/core/app/models/spree/credit_card.rb
@@ -38,9 +38,9 @@ module Spree
end
# Prevents saving the same physical card (same gateway fingerprint + expiry)
- # twice for a user and payment method. Skipped when the gateway does not
- # provide a fingerprint.
- validate :fingerprint_not_duplicated, if: -> { fingerprint.present? }
+ # twice for a user and payment method. Guest payment sources are not saved
+ # cards owned by a customer, so they are excluded from this validation.
+ validate :fingerprint_not_duplicated, if: -> { fingerprint.present? && user_id.present? }
scope :with_payment_profile, -> { where.not(gateway_customer_profile_id: nil) }
scope :capturable, -> { where.not(gateway_customer_profile_id: nil).or(where.not(gateway_payment_profile_id: nil)) }
diff --git a/spree/core/spec/models/spree/credit_card_spec.rb b/spree/core/spec/models/spree/credit_card_spec.rb
index 782f296d16..f30786368c 100644
--- a/spree/core/spec/models/spree/credit_card_spec.rb
+++ b/spree/core/spec/models/spree/credit_card_spec.rb
@@ -175,6 +175,13 @@ describe Spree::CreditCard, type: :model do
expect(build_duplicate(payment_method: create(:credit_card_payment_method))).to be_valid
end
+ it 'allows the same fingerprint for guest payment sources' do
+ create(:credit_card, user: nil, payment_method: payment_method,
+ fingerprint: 'FZqjhq46SWprIY8i', month: 11, year: 2030)
+
+ expect(build_duplicate(user: nil)).to be_valid
+ end
+
it 'does not enforce uniqueness when the fingerprint is missing' do
create(:credit_card, user: user, payment_method: payment_method, fingerprint: nil, month: 11, year: 2030)
expect(build_duplicate(fingerprint: nil)).to be_valid