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.

Add fingerprint-based deduplication to Spree::CreditCard (#14281)

Łukasz Adamczyk8132f3c26000
diff --git a/spree/core/app/models/spree/credit_card.rb b/spree/core/app/models/spree/credit_card.rb
index 243355fb65..eaa28beafe 100644
--- a/spree/core/app/models/spree/credit_card.rb
+++ b/spree/core/app/models/spree/credit_card.rb
@@ -37,6 +37,11 @@ module Spree
validates :name, presence: true
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? }
+
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)) }
scope :default, -> { where(default: true) }
@@ -47,6 +52,19 @@ module Spree
}
scope :not_removed, -> { where(deleted_at: nil) }
+ # Matches saved cards by the gateway's stable card fingerprint plus expiry,
+ # used to reuse an existing card instead of saving a duplicate when a gateway
+ # issues a fresh payment method id for the same physical card.
+ #
+ # @param fingerprint [String] gateway card fingerprint
+ # @param month [Integer, String] expiry month
+ # @param year [Integer, String] expiry year
+ scope :by_fingerprint, ->(fingerprint, month, year) {
+ where(fingerprint: fingerprint).
+ where("CAST(#{table_name}.month AS DECIMAL) = ?", month).
+ where("CAST(#{table_name}.year AS DECIMAL) = ?", year)
+ }
+
alias_attribute :brand, :cc_type
alias_attribute :last4, :last_digits
@@ -165,6 +183,17 @@ module Spree
private
+ # month/year are varchar columns exposed as integer attributes, so the
+ # match must go through #by_fingerprint's decimal cast rather than a plain
+ # equality (which Postgres rejects as `character varying = integer`).
+ def fingerprint_not_duplicated
+ duplicates = self.class.where(user_id: user_id, payment_method_id: payment_method_id).
+ by_fingerprint(fingerprint, month, year)
+ duplicates = duplicates.where.not(id: id) if persisted?
+
+ errors.add(:fingerprint, :taken) if duplicates.exists?
+ end
+
def require_card_numbers?
!encrypted_data.present? && !has_payment_profile?
end
diff --git a/spree/core/db/migrate/20260707000001_add_fingerprint_to_spree_credit_cards.rb b/spree/core/db/migrate/20260707000001_add_fingerprint_to_spree_credit_cards.rb
new file mode 100644
index 0000000000..cd405915ed
--- /dev/null
+++ b/spree/core/db/migrate/20260707000001_add_fingerprint_to_spree_credit_cards.rb
@@ -0,0 +1,39 @@
+class AddFingerprintToSpreeCreditCards < ActiveRecord::Migration[7.2]
+ INDEX_NAME = 'index_spree_credit_cards_unique_fingerprint'.freeze
+
+ def up
+ add_column :spree_credit_cards, :fingerprint, :string
+
+ # Prevent duplicate saved cards (same gateway fingerprint + expiry) per user
+ # and payment method at the database level, backing the application-level
+ # check against concurrent writes. Only active, fingerprinted cards are
+ # constrained, so legacy/non-gateway cards (NULL fingerprint) and
+ # soft-deleted rows are left untouched.
+ if ActiveRecord::Base.connection.adapter_name == 'Mysql2'
+ # MySQL has no partial indexes, but treats NULL as distinct in unique
+ # indexes, so NULL fingerprints are naturally allowed. COALESCE on
+ # deleted_at keeps soft-deleted rows from colliding with active ones.
+ execute <<-SQL
+ CREATE UNIQUE INDEX #{INDEX_NAME}
+ ON spree_credit_cards(
+ user_id,
+ payment_method_id,
+ fingerprint,
+ month,
+ year,
+ (COALESCE(deleted_at, CAST('1970-01-01' AS DATETIME)))
+ )
+ SQL
+ else
+ add_index :spree_credit_cards, [:user_id, :payment_method_id, :fingerprint, :month, :year],
+ unique: true,
+ where: 'fingerprint IS NOT NULL AND deleted_at IS NULL',
+ name: INDEX_NAME
+ end
+ end
+
+ def down
+ remove_index :spree_credit_cards, name: INDEX_NAME
+ remove_column :spree_credit_cards, :fingerprint
+ end
+end
diff --git a/spree/core/spec/models/spree/credit_card_spec.rb b/spree/core/spec/models/spree/credit_card_spec.rb
index 0432031023..782f296d16 100644
--- a/spree/core/spec/models/spree/credit_card_spec.rb
+++ b/spree/core/spec/models/spree/credit_card_spec.rb
@@ -146,6 +146,46 @@ describe Spree::CreditCard, type: :model do
end
end
+ describe 'fingerprint uniqueness' do
+ let(:user) { create(:user) }
+ let(:payment_method) { create(:credit_card_payment_method) }
+ let!(:existing_card) do
+ create(:credit_card, user: user, payment_method: payment_method,
+ fingerprint: 'FZqjhq46SWprIY8i', month: 11, year: 2030)
+ end
+
+ def build_duplicate(attributes = {})
+ build(
+ :credit_card,
+ { user: user, payment_method: payment_method, fingerprint: 'FZqjhq46SWprIY8i', month: 11, year: 2030 }.merge(attributes)
+ )
+ end
+
+ it 'rejects the same physical card for the same user, payment method and expiry' do
+ card = build_duplicate
+ expect(card).not_to be_valid
+ expect(card.errors[:fingerprint]).to be_present
+ end
+
+ it 'allows the same fingerprint with a different expiry' do
+ expect(build_duplicate(year: 2031)).to be_valid
+ end
+
+ it 'allows the same fingerprint under a different payment method' do
+ expect(build_duplicate(payment_method: create(:credit_card_payment_method))).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
+ end
+
+ it 'ignores soft-deleted cards' do
+ existing_card.destroy
+ expect(build_duplicate).to be_valid
+ end
+ end
+
describe '#save' do
before do
credit_card.attributes = valid_credit_card_attributes
@@ -396,6 +436,24 @@ describe Spree::CreditCard, type: :model do
expect(described_class.capturable).not_to include(credit_card_without_profile_or_payment_id)
end
end
+
+ describe '#by_fingerprint' do
+ let!(:match) { create(:credit_card, fingerprint: 'FZqjhq46SWprIY8i', month: 2, year: 2030) }
+ let!(:other_fingerprint) { create(:credit_card, fingerprint: 'differentFingerprint', month: 2, year: 2030) }
+ let!(:other_expiry) { create(:credit_card, fingerprint: 'FZqjhq46SWprIY8i', month: 12, year: 2031) }
+
+ it 'returns cards matching the fingerprint and expiry' do
+ expect(described_class.by_fingerprint('FZqjhq46SWprIY8i', 2, 2030)).to contain_exactly(match)
+ end
+
+ it 'matches when month and year are passed as strings' do
+ expect(described_class.by_fingerprint('FZqjhq46SWprIY8i', '2', '2030')).to contain_exactly(match)
+ end
+
+ it 'returns nothing when the expiry differs' do
+ expect(described_class.by_fingerprint('FZqjhq46SWprIY8i', 1, 2030)).to be_empty
+ end
+ end
end
describe '#display_brand' do