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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex 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 Spreeend# 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.rbindex 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 doexpect(build_duplicate(payment_method: create(:credit_card_payment_method))).to be_validend+ 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' docreate(:credit_card, user: user, payment_method: payment_method, fingerprint: nil, month: 11, year: 2030)expect(build_duplicate(fingerprint: nil)).to be_valid