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
Implement custom events for Product/Payment/Shipment
Damian Legawiec83247d855b1c
diff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rbindex f6c0e2e597..22a00068e6 100644--- a/core/app/models/spree/payment.rb+++ b/core/app/models/spree/payment.rb@@ -15,6 +15,7 @@ module Spreeinclude Spree::Payment::Processinginclude Spree::Payment::Webhooks+ include Spree::Payment::CustomEventsNON_RISKY_AVS_CODES = ['B', 'D', 'H', 'J', 'M', 'Q', 'T', 'V', 'X', 'Y'].freezeRISKY_AVS_CODES = ['A', 'C', 'E', 'F', 'G', 'I', 'K', 'L', 'N', 'O', 'P', 'R', 'S', 'U', 'W', 'Z'].freezediff --git a/core/app/models/spree/payment/custom_events.rb b/core/app/models/spree/payment/custom_events.rbnew file mode 100644index 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+enddiff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rbindex b551a30f16..a48fc7e4b2 100644--- a/core/app/models/spree/shipment.rb+++ b/core/app/models/spree/shipment.rb@@ -15,6 +15,7 @@ module Spreeendinclude Spree::Shipment::Emailsinclude Spree::Shipment::Webhooks+ include Spree::Shipment::CustomEventswith_options inverse_of: :shipments dobelongs_to :address, class_name: 'Spree::Address'@@ -455,17 +456,8 @@ module SpreeShipmentHandler.factory(self).performend- 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::CustomEventsdef 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.rbnew file mode 100644index 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+enddiff --git a/core/app/models/spree/stock_movement.rb b/core/app/models/spree/stock_movement.rbindex 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}.freezeinclude Spree::StockMovement::Webhooks+ include Spree::StockMovement::CustomEventsbelongs_to :stock_item, class_name: 'Spree::StockItem', inverse_of: :stock_movementsbelongs_to :originator, polymorphic: truediff --git a/core/app/models/spree/stock_movement/custom_events.rb b/core/app/models/spree/stock_movement/custom_events.rbnew file mode 100644index 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+enddiff --git a/core/spec/models/spree/payment/custom_events_spec.rb b/core/spec/models/spree/payment/custom_events_spec.rbnew file mode 100644index 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+enddiff --git a/core/spec/models/spree/shipment/custom_events_spec.rb b/core/spec/models/spree/shipment/custom_events_spec.rbnew file mode 100644index 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+enddiff --git a/core/spec/models/spree/stock_movement/custom_events_spec.rb b/core/spec/models/spree/stock_movement/custom_events_spec.rbnew file mode 100644index 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