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 some scopes to payments and store credits (#12077)

Łukasz Adamczykcf3da93eb4ad
diff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rb
index d1a24a8035..57ca0e1944 100644
--- a/core/app/models/spree/payment.rb
+++ b/core/app/models/spree/payment.rb
@@ -65,6 +65,8 @@ module Spree
scope :processing, -> { with_state('processing') }
scope :failed, -> { with_state('failed') }
+ scope :incomplete, -> { where.not(state: 'completed') }
+
scope :risky, -> { where("avs_response IN (?) OR (cvv_response_code IS NOT NULL and cvv_response_code != 'M') OR state = 'failed'", RISKY_AVS_CODES) }
scope :valid, -> { where.not(state: INVALID_STATES) }
diff --git a/core/app/models/spree/store_credit.rb b/core/app/models/spree/store_credit.rb
index 7cbec93ce3..a1f272ba06 100644
--- a/core/app/models/spree/store_credit.rb
+++ b/core/app/models/spree/store_credit.rb
@@ -23,7 +23,10 @@ module Spree
belongs_to :created_by, class_name: "::#{Spree.admin_user_class}", foreign_key: 'created_by_id', optional: true
belongs_to :credit_type, class_name: 'Spree::StoreCreditType', foreign_key: 'type_id', optional: true
belongs_to :store, class_name: 'Spree::Store'
+
has_many :store_credit_events, class_name: 'Spree::StoreCreditEvent'
+ has_many :payments, as: :source, class_name: 'Spree::Payment'
+ has_many :orders, through: :payments, class_name: 'Spree::Order'
validates :currency, :store, presence: true
validates :amount, numericality: { greater_than: 0 }
@@ -36,6 +39,10 @@ module Spree
scope :order_by_priority, -> { includes(:credit_type).order('spree_store_credit_types.priority ASC') }
+ scope :not_authorized, -> { where(amount_authorized: 0) }
+ scope :not_used, -> { where(amount_authorized: 0) }
+ scope :available, -> { not_authorized.not_used }
+
after_save :store_event
before_destroy :validate_no_amount_used