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.

Scope paymenet source uniqueness to payment method (#15020)

Kacper Mękarski6167a24e348d
diff --git a/spree/core/app/models/spree/payment_source.rb b/spree/core/app/models/spree/payment_source.rb
index 63348b1572..adf6c4dcf0 100644
--- a/spree/core/app/models/spree/payment_source.rb
+++ b/spree/core/app/models/spree/payment_source.rb
@@ -17,7 +17,9 @@ module Spree
#
# Validations
#
- validates_uniqueness_of :gateway_payment_profile_id, scope: :type
+ # A gateway profile id is only unique within the gateway account that issued it, so the same
+ # buyer needs a separate source under each payment method they pay through.
+ validates_uniqueness_of :gateway_payment_profile_id, scope: %i[type payment_method_id]
#
# Delegations
diff --git a/spree/core/db/migrate/20261030100001_scope_payment_sources_profile_id_uniqueness_to_payment_method.rb b/spree/core/db/migrate/20261030100001_scope_payment_sources_profile_id_uniqueness_to_payment_method.rb
new file mode 100644
index 0000000000..24cca8e62b
--- /dev/null
+++ b/spree/core/db/migrate/20261030100001_scope_payment_sources_profile_id_uniqueness_to_payment_method.rb
@@ -0,0 +1,38 @@
+class ScopePaymentSourcesProfileIdUniquenessToPaymentMethod < ActiveRecord::Migration[8.1]
+ LEGACY_INDEX_NAME = 'index_payment_sources_on_type_and_gateway_payment_profile_id'.freeze
+ INDEX_NAME = 'index_payment_sources_on_type_profile_id_and_payment_method'.freeze
+
+ def up
+ remove_index :spree_payment_sources, name: LEGACY_INDEX_NAME
+ add_index :spree_payment_sources, [:type, :gateway_payment_profile_id, :payment_method_id],
+ unique: true, name: INDEX_NAME
+ end
+
+ # The legacy index cannot come back once a buyer holds the same gateway profile under two
+ # payment methods, which is what `up` exists to allow. Checked before anything is dropped so a
+ # refusal leaves both indexes intact on MySQL, which has no transactional DDL.
+ def down
+ if cross_payment_method_duplicates?
+ raise ActiveRecord::IrreversibleMigration,
+ 'Payment sources share a gateway_payment_profile_id across payment methods, which ' \
+ 'the globally unique index cannot represent. Reconcile those rows before rolling back.'
+ end
+
+ remove_index :spree_payment_sources, name: INDEX_NAME
+ add_index :spree_payment_sources, [:type, :gateway_payment_profile_id],
+ unique: true, name: LEGACY_INDEX_NAME
+ end
+
+ private
+
+ # NULLs are distinct in a unique index, so only fully populated pairs could ever have collided.
+ def cross_payment_method_duplicates?
+ connection.select_value(<<~SQL).present?
+ SELECT 1 FROM spree_payment_sources
+ WHERE type IS NOT NULL AND gateway_payment_profile_id IS NOT NULL
+ GROUP BY type, gateway_payment_profile_id
+ HAVING COUNT(*) > 1
+ LIMIT 1
+ SQL
+ end
+end
diff --git a/spree/core/spec/models/spree/payment_source_spec.rb b/spree/core/spec/models/spree/payment_source_spec.rb
index b4bc0a5bf8..6f85f4b5f4 100644
--- a/spree/core/spec/models/spree/payment_source_spec.rb
+++ b/spree/core/spec/models/spree/payment_source_spec.rb
@@ -6,6 +6,25 @@ RSpec.describe Spree::PaymentSource, type: :model do
let(:payment_source) { create(:payment_source, customer: user, payment_method: payment_method) }
let(:user) { create(:user) }
+ describe 'validations' do
+ let(:other_payment_method) { create(:custom_payment_method) }
+
+ before { create(:payment_source, payment_method: payment_method, gateway_payment_profile_id: 'profile-1') }
+
+ it 'rejects a duplicate gateway payment profile under the same payment method' do
+ duplicate = build(:payment_source, payment_method: payment_method, gateway_payment_profile_id: 'profile-1')
+
+ expect(duplicate).not_to be_valid
+ expect(duplicate.errors[:gateway_payment_profile_id]).to include('has already been taken')
+ end
+
+ it 'allows the same gateway payment profile under a different payment method' do
+ other = build(:payment_source, payment_method: other_payment_method, gateway_payment_profile_id: 'profile-1')
+
+ expect(other).to be_valid
+ end
+ end
+
describe '#gateway_customer' do
let!(:gateway_customer) { create(:gateway_customer, payment_method: payment_method, customer: user) }
diff --git a/spree/providers/stripe/app/services/spree_stripe/create_source.rb b/spree/providers/stripe/app/services/spree_stripe/create_source.rb
index 19eedaba74..314b197815 100644
--- a/spree/providers/stripe/app/services/spree_stripe/create_source.rb
+++ b/spree/providers/stripe/app/services/spree_stripe/create_source.rb
@@ -50,7 +50,8 @@ module SpreeStripe
def find_or_create_credit_card
if customer
- exact_source = customer.credit_cards.find_by(gateway_payment_profile_id: stripe_payment_method_id)
+ exact_source = customer.credit_cards.where(payment_method: gateway).
+ find_by(gateway_payment_profile_id: stripe_payment_method_id)
return exact_source if exact_source
matching_source = match_credit_card_by_fingerprint
diff --git a/spree/providers/stripe/spec/services/spree_stripe/create_source_spec.rb b/spree/providers/stripe/spec/services/spree_stripe/create_source_spec.rb
index fd36ed5076..1f5233aad2 100644
--- a/spree/providers/stripe/spec/services/spree_stripe/create_source_spec.rb
+++ b/spree/providers/stripe/spec/services/spree_stripe/create_source_spec.rb
@@ -61,11 +61,23 @@ RSpec.describe SpreeStripe::CreateSource do
end
context 'when the customer already has a card with the same profile id' do
- let!(:credit_card) { create(:credit_card, customer: customer, gateway_payment_profile_id: source_id) }
+ let!(:credit_card) do
+ create(:credit_card, customer: customer, payment_method: gateway, gateway_payment_profile_id: source_id)
+ end
it 'returns the already created card' do
expect(subject).to eq(credit_card)
end
+
+ context 'when that card belongs to another payment method' do
+ let!(:credit_card) { create(:credit_card, customer: customer, gateway_payment_profile_id: source_id) }
+ let!(:gateway_customer) { create(:gateway_customer, customer: customer, payment_method: gateway) }
+
+ it 'creates a new card for this payment method' do
+ expect(subject).not_to eq(credit_card)
+ expect(subject.payment_method).to eq(gateway)
+ end
+ end
end
context 'when the customer has no card yet' do