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 capturable scope and ability to temporary skip payment.source validation (#12963)

Szymon Chorobski0be8793417bc
diff --git a/core/app/models/concerns/spree/user_payment_source.rb b/core/app/models/concerns/spree/user_payment_source.rb
index 20645f5e00..f17df8f80f 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.not_expired.where(payment_method: Spree::PaymentMethod.active).not_removed
+ credit_cards.capturable.not_expired.where(payment_method: Spree::PaymentMethod.active).not_removed
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 f58250eb86..4a368c6ccc 100644
--- a/core/app/models/spree/credit_card.rb
+++ b/core/app/models/spree/credit_card.rb
@@ -39,6 +39,7 @@ module Spree
end
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) }
scope :not_expired, lambda {
where('CAST(spree_credit_cards.year AS DECIMAL) > ?', Time.current.year).
diff --git a/core/app/models/spree/gateway.rb b/core/app/models/spree/gateway.rb
index de8aa66e83..cfe3cc9d74 100644
--- a/core/app/models/spree/gateway.rb
+++ b/core/app/models/spree/gateway.rb
@@ -67,7 +67,7 @@ module Spree
def sources_by_order(order)
source_ids = order.payments.where(source_type: payment_source_class.to_s, payment_method_id: id).pluck(:source_id).uniq
- payment_source_class.where(id: source_ids).with_payment_profile
+ payment_source_class.where(id: source_ids).capturable
end
def reusable_sources(order)
@@ -75,7 +75,7 @@ module Spree
sources_by_order order
else
if order.user_id
- credit_cards.where(user_id: order.user_id).with_payment_profile
+ credit_cards.where(user_id: order.user_id).capturable
else
[]
end
diff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rb
index db5db0373d..66c742f3b9 100644
--- a/core/app/models/spree/payment.rb
+++ b/core/app/models/spree/payment.rb
@@ -32,7 +32,7 @@ module Spree
has_many :refunds, inverse_of: :payment
validates :payment_method, presence: true
- validates :source, presence: true, if: -> { payment_method&.source_required? }
+ validates :source, presence: true, if: :source_required?
validate :payment_method_available_for_order, on: :create
before_validation :validate_source
@@ -50,6 +50,7 @@ module Spree
after_destroy :update_order
attr_accessor :source_attributes, :request_env, :capture_on_dispatch
+ attribute :skip_source_requirement, :boolean, default: false
after_initialize :build_source
@@ -247,6 +248,12 @@ module Spree
source_class.respond_to?(:display_name) ? source_class.display_name : source_class.name.demodulize.split(/(?=[A-Z])/).join(' ')
end
+ def source_required?
+ return false if skip_source_requirement
+
+ payment_method&.source_required?
+ end
+
private
def set_amount
diff --git a/core/spec/models/spree/credit_card_spec.rb b/core/spec/models/spree/credit_card_spec.rb
index 643e2efa3a..0be83f105b 100644
--- a/core/spec/models/spree/credit_card_spec.rb
+++ b/core/spec/models/spree/credit_card_spec.rb
@@ -406,6 +406,20 @@ describe Spree::CreditCard, type: :model do
outdated_credit_card_four)
end
end
+
+ describe '#capturable' do
+ let!(:credit_card_with_profile_id) { create(:credit_card, gateway_customer_profile_id: '123') }
+ let!(:credit_card_with_payment_id) { create(:credit_card, gateway_payment_profile_id: '123') }
+ let!(:credit_card_without_profile_or_payment_id) { create(:credit_card, gateway_customer_profile_id: nil, gateway_payment_profile_id: nil) }
+
+ it 'includes only capturable credit cards' do
+ expect(described_class.capturable).to include(credit_card_with_profile_id, credit_card_with_payment_id)
+ end
+
+ it 'does not include credit cards without profile or payment id' do
+ expect(described_class.capturable).not_to include(credit_card_without_profile_or_payment_id)
+ end
+ end
end
context '#display_brand' do
diff --git a/core/spec/models/spree/payment_spec.rb b/core/spec/models/spree/payment_spec.rb
index 43543524eb..58af8d7ef7 100644
--- a/core/spec/models/spree/payment_spec.rb
+++ b/core/spec/models/spree/payment_spec.rb
@@ -165,6 +165,13 @@ describe Spree::Payment, type: :model do
payment.source = nil
expect(payment).not_to be_valid
end
+
+ context 'when skip_source_requirement is set to true' do
+ it 'does not validate source presence' do
+ payment.skip_source_requirement = true
+ expect(payment).to be_valid
+ end
+ end
end
it 'returns useful error messages when source is invalid' do