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
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.rbindex 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 Spreeenddef 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_removedenddef drop_payment_source(source)diff --git a/core/app/models/spree/credit_card.rb b/core/app/models/spree/credit_card.rbindex f58250eb86..4a368c6ccc 100644--- a/core/app/models/spree/credit_card.rb+++ b/core/app/models/spree/credit_card.rb@@ -39,6 +39,7 @@ module Spreeendscope :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.rbindex de8aa66e83..cfe3cc9d74 100644--- a/core/app/models/spree/gateway.rb+++ b/core/app/models/spree/gateway.rb@@ -67,7 +67,7 @@ module Spreedef 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).capturableenddef reusable_sources(order)@@ -75,7 +75,7 @@ module Spreesources_by_order orderelseif order.user_id- credit_cards.where(user_id: order.user_id).with_payment_profile+ credit_cards.where(user_id: order.user_id).capturableelse[]enddiff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rbindex db5db0373d..66c742f3b9 100644--- a/core/app/models/spree/payment.rb+++ b/core/app/models/spree/payment.rb@@ -32,7 +32,7 @@ module Spreehas_many :refunds, inverse_of: :paymentvalidates :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: :createbefore_validation :validate_source@@ -50,6 +50,7 @@ module Spreeafter_destroy :update_orderattr_accessor :source_attributes, :request_env, :capture_on_dispatch+ attribute :skip_source_requirement, :boolean, default: falseafter_initialize :build_source@@ -247,6 +248,12 @@ module Spreesource_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+privatedef set_amountdiff --git a/core/spec/models/spree/credit_card_spec.rb b/core/spec/models/spree/credit_card_spec.rbindex 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 dooutdated_credit_card_four)endend++ 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+ endendcontext '#display_brand' dodiff --git a/core/spec/models/spree/payment_spec.rb b/core/spec/models/spree/payment_spec.rbindex 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 dopayment.source = nilexpect(payment).not_to be_validend++ 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+ endendit 'returns useful error messages when source is invalid' do