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.

Implement custom events for Product/Payment/Shipment

Damian Legawiec83247d855b1c
diff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rb
index f6c0e2e597..22a00068e6 100644
--- a/core/app/models/spree/payment.rb
+++ b/core/app/models/spree/payment.rb
@@ -15,6 +15,7 @@ module Spree
include Spree::Payment::Processing
include Spree::Payment::Webhooks
+ include Spree::Payment::CustomEvents
NON_RISKY_AVS_CODES = ['B', 'D', 'H', 'J', 'M', 'Q', 'T', 'V', 'X', 'Y'].freeze
RISKY_AVS_CODES = ['A', 'C', 'E', 'F', 'G', 'I', 'K', 'L', 'N', 'O', 'P', 'R', 'S', 'U', 'W', 'Z'].freeze
diff --git a/core/app/models/spree/payment/custom_events.rb b/core/app/models/spree/payment/custom_events.rb
new file mode 100644
index 0000000000..9aecf13097
--- /dev/null
+++ b/core/app/models/spree/payment/custom_events.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+module Spree
+ class Payment < Spree.base_class
+ # Publishes custom payment events beyond basic lifecycle events.
+ #
+ # Events:
+ # - payment.paid: Payment was completed
+ # - order.paid: Order is fully paid (no outstanding balance)
+ #
+ module CustomEvents
+ extend ActiveSupport::Concern
+
+ included do
+ after_commit :publish_payment_paid_event, on: :update, if: :should_publish_paid_event?
+ end
+
+ private
+
+ def should_publish_paid_event?
+ return false unless Spree::Events.enabled?
+ return false unless state_previously_changed?
+
+ state_previous_change&.last == 'completed'
+ end
+
+ def publish_payment_paid_event
+ publish_event('payment.paid')
+ publish_order_paid_event if order.paid?
+ end
+
+ def publish_order_paid_event
+ order.publish_event('order.paid')
+ end
+ end
+ end
+end
diff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rb
index b551a30f16..a48fc7e4b2 100644
--- a/core/app/models/spree/shipment.rb
+++ b/core/app/models/spree/shipment.rb
@@ -15,6 +15,7 @@ module Spree
end
include Spree::Shipment::Emails
include Spree::Shipment::Webhooks
+ include Spree::Shipment::CustomEvents
with_options inverse_of: :shipments do
belongs_to :address, class_name: 'Spree::Address'
@@ -455,17 +456,8 @@ module Spree
ShipmentHandler.factory(self).perform
end
- def publish_shipment_shipped_event
- publish_event('shipment.shipped')
- end
-
- def publish_shipment_canceled_event
- publish_event('shipment.canceled')
- end
-
- def publish_shipment_resumed_event
- publish_event('shipment.resumed')
- end
+ # publish_shipment_shipped_event, publish_shipment_canceled_event, and
+ # publish_shipment_resumed_event are defined in Spree::Shipment::CustomEvents
def can_get_rates?
return true unless order.requires_ship_address?
diff --git a/core/app/models/spree/shipment/custom_events.rb b/core/app/models/spree/shipment/custom_events.rb
new file mode 100644
index 0000000000..2ede330c7e
--- /dev/null
+++ b/core/app/models/spree/shipment/custom_events.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+module Spree
+ class Shipment < Spree.base_class
+ # Publishes custom shipment events beyond basic lifecycle events.
+ #
+ # Events:
+ # - shipment.shipped: Shipment was shipped
+ # - order.shipped: All order shipments are shipped
+ # - shipment.canceled: Shipment was canceled
+ # - shipment.resumed: Shipment was resumed from canceled state
+ #
+ # NOTE: These methods are called from the state machine's after_transition callbacks
+ # defined in the Shipment model, not via ActiveRecord callbacks.
+ #
+ module CustomEvents
+ extend ActiveSupport::Concern
+
+ def publish_shipment_shipped_event
+ return unless Spree::Events.enabled?
+
+ publish_event('shipment.shipped')
+ # Force reload of shipments association to see the new state
+ order.shipments.reset
+ publish_order_shipped_event if order.fully_shipped?
+ end
+
+ def publish_shipment_canceled_event
+ return unless Spree::Events.enabled?
+
+ publish_event('shipment.canceled')
+ end
+
+ def publish_shipment_resumed_event
+ return unless Spree::Events.enabled?
+
+ publish_event('shipment.resumed')
+ end
+
+ private
+
+ def publish_order_shipped_event
+ order.publish_event('order.shipped')
+ end
+ end
+ end
+end
diff --git a/core/app/models/spree/stock_movement.rb b/core/app/models/spree/stock_movement.rb
index f4ef6a59b2..dfd5b9bfe8 100644
--- a/core/app/models/spree/stock_movement.rb
+++ b/core/app/models/spree/stock_movement.rb
@@ -6,6 +6,7 @@ module Spree
}.freeze
include Spree::StockMovement::Webhooks
+ include Spree::StockMovement::CustomEvents
belongs_to :stock_item, class_name: 'Spree::StockItem', inverse_of: :stock_movements
belongs_to :originator, polymorphic: true
diff --git a/core/app/models/spree/stock_movement/custom_events.rb b/core/app/models/spree/stock_movement/custom_events.rb
new file mode 100644
index 0000000000..e7decfd267
--- /dev/null
+++ b/core/app/models/spree/stock_movement/custom_events.rb
@@ -0,0 +1,44 @@
+# frozen_string_literal: true
+
+module Spree
+ class StockMovement < Spree.base_class
+ # Publishes custom stock events when stock levels change.
+ #
+ # Events:
+ # - product.out_of_stock: Product has no stock left for any variant
+ # - product.back_in_stock: Product was out of stock and now has stock again
+ #
+ module CustomEvents
+ extend ActiveSupport::Concern
+
+ included do
+ around_save :track_stock_changes_for_events
+ end
+
+ private
+
+ def track_stock_changes_for_events
+ return yield unless Spree::Events.enabled?
+
+ the_product = product
+ product_was_in_stock = the_product.any_variant_in_stock_or_backorderable?
+
+ yield
+
+ # Reload to get fresh stock data
+ the_product.reload
+ product_now_in_stock = the_product.any_variant_in_stock_or_backorderable?
+
+ if product_was_in_stock && !product_now_in_stock
+ the_product.publish_event('product.out_of_stock')
+ elsif !product_was_in_stock && product_now_in_stock
+ the_product.publish_event('product.back_in_stock')
+ end
+ end
+
+ def product
+ stock_item.variant.product
+ end
+ end
+ end
+end
diff --git a/core/spec/models/spree/payment/custom_events_spec.rb b/core/spec/models/spree/payment/custom_events_spec.rb
new file mode 100644
index 0000000000..b4206a7aa1
--- /dev/null
+++ b/core/spec/models/spree/payment/custom_events_spec.rb
@@ -0,0 +1,57 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Spree::Payment::CustomEvents do
+ let(:order) { create(:order_with_line_items) }
+ let(:payment) { create(:payment, order: order, state: 'pending') }
+
+ before do
+ allow(Spree::Events).to receive(:enabled?).and_return(true)
+ allow(Spree::Events).to receive(:publish)
+ end
+
+ describe 'payment.paid event' do
+ it 'publishes payment.paid when payment state changes to completed' do
+ allow(order).to receive(:paid?).and_return(false)
+
+ payment.update!(state: 'completed')
+
+ expect(Spree::Events).to have_received(:publish).with('payment.paid', anything, anything)
+ end
+
+ it 'does not publish payment.paid when state changes to something other than completed' do
+ payment.update!(state: 'processing')
+
+ expect(Spree::Events).not_to have_received(:publish).with('payment.paid', anything, anything)
+ end
+
+ it 'does not publish when events are disabled' do
+ allow(Spree::Events).to receive(:enabled?).and_return(false)
+
+ payment.update!(state: 'completed')
+
+ expect(Spree::Events).not_to have_received(:publish).with('payment.paid', anything, anything)
+ end
+ end
+
+ describe 'order.paid event' do
+ it 'publishes order.paid when payment completes and order is fully paid' do
+ allow(order).to receive(:paid?).and_return(true)
+
+ payment.update!(state: 'completed')
+
+ expect(Spree::Events).to have_received(:publish).with('payment.paid', anything, anything)
+ expect(Spree::Events).to have_received(:publish).with('order.paid', anything, anything)
+ end
+
+ it 'does not publish order.paid when order still has outstanding balance' do
+ allow(order).to receive(:paid?).and_return(false)
+
+ payment.update!(state: 'completed')
+
+ expect(Spree::Events).to have_received(:publish).with('payment.paid', anything, anything)
+ expect(Spree::Events).not_to have_received(:publish).with('order.paid', anything, anything)
+ end
+ end
+end
diff --git a/core/spec/models/spree/shipment/custom_events_spec.rb b/core/spec/models/spree/shipment/custom_events_spec.rb
new file mode 100644
index 0000000000..51cad69b2b
--- /dev/null
+++ b/core/spec/models/spree/shipment/custom_events_spec.rb
@@ -0,0 +1,53 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Spree::Shipment::CustomEvents do
+ let(:order) { create(:order_ready_to_ship, line_items_count: 1) }
+ let(:shipment) { order.shipments.first }
+
+ before do
+ allow(Spree::Events).to receive(:enabled?).and_return(true)
+ allow(Spree::Events).to receive(:publish)
+ end
+
+ describe 'shipment.shipped event' do
+ it 'publishes shipment.shipped when shipment state changes to shipped' do
+ allow(order).to receive(:fully_shipped?).and_return(false)
+
+ shipment.ship!
+
+ expect(Spree::Events).to have_received(:publish).with('shipment.shipped', anything, anything)
+ end
+
+ it 'does not publish when events are disabled' do
+ allow(Spree::Events).to receive(:enabled?).and_return(false)
+
+ shipment.ship!
+
+ expect(Spree::Events).not_to have_received(:publish).with('shipment.shipped', anything, anything)
+ end
+ end
+
+ describe 'order.shipped event' do
+ it 'publishes order.shipped when shipment ships and order is fully shipped' do
+ # The order_ready_to_ship factory creates an order with a single shipment,
+ # so when that shipment ships, order.fully_shipped? returns true
+ shipment.ship!
+
+ expect(Spree::Events).to have_received(:publish).with('shipment.shipped', anything, anything)
+ expect(Spree::Events).to have_received(:publish).with('order.shipped', anything, anything)
+ end
+
+ it 'does not publish order.shipped when order has unshipped shipments' do
+ # Create a second shipment that won't be shipped
+ create(:shipment, order: order, state: 'pending')
+ order.reload
+
+ shipment.ship!
+
+ expect(Spree::Events).to have_received(:publish).with('shipment.shipped', anything, anything)
+ expect(Spree::Events).not_to have_received(:publish).with('order.shipped', anything, anything)
+ end
+ end
+end
diff --git a/core/spec/models/spree/stock_movement/custom_events_spec.rb b/core/spec/models/spree/stock_movement/custom_events_spec.rb
new file mode 100644
index 0000000000..867aca79ae
--- /dev/null
+++ b/core/spec/models/spree/stock_movement/custom_events_spec.rb
@@ -0,0 +1,86 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Spree::StockMovement::CustomEvents do
+ let(:stock_location) { create(:stock_location) }
+ let(:product) { create(:product) }
+ let(:variant) { product.master }
+ let!(:stock_item) do
+ item = variant.stock_items.first || create(:stock_item, variant: variant, stock_location: stock_location)
+ item.update!(backorderable: false)
+ item
+ end
+
+ before do
+ allow(Spree::Events).to receive(:enabled?).and_return(true)
+ allow(Spree::Events).to receive(:publish)
+ end
+
+ describe 'product.out_of_stock event' do
+ before do
+ stock_item.set_count_on_hand(10)
+ end
+
+ it 'publishes product.out_of_stock when product goes out of stock' do
+ stock_item.stock_movements.create!(quantity: -10)
+
+ expect(Spree::Events).to have_received(:publish).with('product.out_of_stock', anything, anything)
+ end
+
+ it 'does not publish when product still has stock' do
+ stock_item.stock_movements.create!(quantity: -5)
+
+ expect(Spree::Events).not_to have_received(:publish).with('product.out_of_stock', anything, anything)
+ end
+
+ it 'does not publish when product was already out of stock' do
+ # Start with product already out of stock
+ stock_item.set_count_on_hand(0)
+
+ # Reset the mock to clear any events from setup
+ RSpec::Mocks.space.proxy_for(Spree::Events).reset
+
+ allow(Spree::Events).to receive(:enabled?).and_return(true)
+ allow(Spree::Events).to receive(:publish)
+
+ # Add some stock (this triggers back_in_stock, not out_of_stock)
+ stock_item.stock_movements.create!(quantity: 1)
+
+ expect(Spree::Events).not_to have_received(:publish).with('product.out_of_stock', anything, anything)
+ end
+ end
+
+ describe 'product.back_in_stock event' do
+ before do
+ stock_item.set_count_on_hand(0)
+ end
+
+ it 'publishes product.back_in_stock when product comes back in stock' do
+ stock_item.stock_movements.create!(quantity: 10)
+
+ expect(Spree::Events).to have_received(:publish).with('product.back_in_stock', anything, anything)
+ end
+
+ it 'does not publish when product was already in stock' do
+ stock_item.set_count_on_hand(5)
+
+ stock_item.stock_movements.create!(quantity: 10)
+
+ expect(Spree::Events).not_to have_received(:publish).with('product.back_in_stock', anything, anything)
+ end
+ end
+
+ describe 'when events are disabled' do
+ before do
+ allow(Spree::Events).to receive(:enabled?).and_return(false)
+ stock_item.set_count_on_hand(10)
+ end
+
+ it 'does not publish any events' do
+ stock_item.stock_movements.create!(quantity: -10)
+
+ expect(Spree::Events).not_to have_received(:publish).with('product.out_of_stock', anything, anything)
+ end
+ end
+end