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
Added universal webhook interfaces to avoid using decorators (#12070)
Damian Legawiecd6f26f51e5c6
diff --git a/api/app/models/spree/api/webhooks/order_decorator.rb b/api/app/models/spree/api/webhooks/order_decorator.rbdeleted file mode 100644index 61de7584c2..0000000000--- a/api/app/models/spree/api/webhooks/order_decorator.rb+++ /dev/null@@ -1,43 +0,0 @@-module Spree- module Api- module Webhooks- module OrderDecorator- def self.prepended(base)- def base.custom_webhook_events- %w[order.canceled order.placed order.resumed order.shipped]- end-- base.after_update_commit :queue_webhooks_requests_for_order_resumed!- end-- def after_cancel- super- queue_webhooks_requests!('order.canceled')- end-- def finalize!- super- queue_webhooks_requests!('order.placed')- end-- def after_resume- super- queue_webhooks_requests!('order.resumed')- self.state_machine_resumed = false- end-- private-- def queue_webhooks_requests_for_order_resumed!- return if state_machine_resumed?- return unless state_previously_changed?- return unless state_previous_change&.last == 'resumed'-- queue_webhooks_requests!('order.resumed')- end- end- end- end-end--Spree::Order.prepend(Spree::Api::Webhooks::OrderDecorator)diff --git a/api/app/models/spree/api/webhooks/payment_decorator.rb b/api/app/models/spree/api/webhooks/payment_decorator.rbdeleted file mode 100644index 00b46bcf62..0000000000--- a/api/app/models/spree/api/webhooks/payment_decorator.rb+++ /dev/null@@ -1,26 +0,0 @@-module Spree- module Api- module Webhooks- module PaymentDecorator- def self.prepended(base)- def base.custom_webhook_events- %w[payment.paid payment.voided]- end- end-- def after_void- super- queue_webhooks_requests!('payment.voided')- end-- def after_completed- super- queue_webhooks_requests!('payment.paid')- order.queue_webhooks_requests!('order.paid') if order.paid?- end- end- end- end-end--Spree::Payment.prepend(Spree::Api::Webhooks::PaymentDecorator)diff --git a/api/app/models/spree/api/webhooks/product_decorator.rb b/api/app/models/spree/api/webhooks/product_decorator.rbdeleted file mode 100644index 1485d01ded..0000000000--- a/api/app/models/spree/api/webhooks/product_decorator.rb+++ /dev/null@@ -1,46 +0,0 @@-module Spree- module Api- module Webhooks- module ProductDecorator- def self.prepended(base)- def base.custom_webhook_events- %w[product.back_in_stock product.backorderable product.discontinued- product.out_of_stock product.activated product.archived product.drafted]- end-- def base.ignored_attributes_for_update_webhook_event- %w[status]- end-- base.after_update_commit :queue_webhooks_requests_for_product_discontinued!- end-- def after_activate- super- queue_webhooks_requests!('product.activated')- end-- def after_archive- super- queue_webhooks_requests!('product.archived')- end-- def after_draft- super- queue_webhooks_requests!('product.drafted')- end-- private-- def queue_webhooks_requests_for_product_discontinued!- return unless discontinue_on_previously_changed?- return if (change = discontinue_on_previous_change).blank? || change.last.blank?-- queue_webhooks_requests!('product.discontinued')- end- end- end- end-end--Spree::Product.prepend(Spree::Api::Webhooks::ProductDecorator)diff --git a/api/app/models/spree/api/webhooks/shipment_decorator.rb b/api/app/models/spree/api/webhooks/shipment_decorator.rbdeleted file mode 100644index ee2181cf8c..0000000000--- a/api/app/models/spree/api/webhooks/shipment_decorator.rb+++ /dev/null@@ -1,21 +0,0 @@-module Spree- module Api- module Webhooks- module ShipmentDecorator- def self.prepended(base)- def base.custom_webhook_events- %w[shipment.shipped]- end- end-- def after_ship- super- queue_webhooks_requests!('shipment.shipped')- order.queue_webhooks_requests!('order.shipped') if order.fully_shipped?- end- end- end- end-end--Spree::Shipment.prepend(Spree::Api::Webhooks::ShipmentDecorator)diff --git a/api/app/models/spree/api/webhooks/stock_item_decorator.rb b/api/app/models/spree/api/webhooks/stock_item_decorator.rbdeleted file mode 100644index f20a77161e..0000000000--- a/api/app/models/spree/api/webhooks/stock_item_decorator.rb+++ /dev/null@@ -1,43 +0,0 @@-module Spree- module Api- module Webhooks- module StockItemDecorator- def self.prepended(base)- base.around_save :queue_webhooks_requests_for_variant_backorderable!- base.around_save :queue_webhooks_requests_for_product_backorderable!- end-- private-- def queue_webhooks_requests_for_variant_backorderable!- was_out_of_stock = !variant.in_stock_or_backorderable?- was_not_backorderable = !variant_backorderable?- yield- if was_out_of_stock && was_not_backorderable && variant_backorderable?- reload- variant.queue_webhooks_requests!('variant.backorderable')- end- end-- def queue_webhooks_requests_for_product_backorderable!- product_was_out_of_stock = !product.any_variant_in_stock_or_backorderable?- product_was_not_backorderable = !product_backorderable?- yield- if product_was_out_of_stock && product_was_not_backorderable && product_backorderable?- variant.product.queue_webhooks_requests!('product.backorderable')- end- end-- def product_backorderable?- Spree::StockItem.exists?(backorderable: true, variant_id: variant.product.variants.ids)- end-- def variant_backorderable?- variant.stock_items.exists?(backorderable: true)- end- end- end- end-end--Spree::StockItem.prepend(Spree::Api::Webhooks::StockItemDecorator)diff --git a/api/app/models/spree/api/webhooks/stock_movement_decorator.rb b/api/app/models/spree/api/webhooks/stock_movement_decorator.rbdeleted file mode 100644index f0520c091d..0000000000--- a/api/app/models/spree/api/webhooks/stock_movement_decorator.rb+++ /dev/null@@ -1,52 +0,0 @@-module Spree- module Api- module Webhooks- module StockMovementDecorator- def self.prepended(base)- base.around_save :queue_webhooks_requests_for_variant_out_of_stock!- base.around_save :queue_webhooks_requests_for_variant_back_in_stock!- base.around_save :queue_webhooks_requests_for_product_out_of_stock!- base.around_save :queue_webhooks_requests_for_product_back_in_stock!- end-- private-- def queue_webhooks_requests_for_variant_out_of_stock!- variant_in_stock_before_update = variant.in_stock_or_backorderable?- yield- if variant_in_stock_before_update && !variant.in_stock_or_backorderable?- reload- stock_item.variant.queue_webhooks_requests!('variant.out_of_stock')- end- end-- def queue_webhooks_requests_for_variant_back_in_stock!- variant_was_out_of_stock = !variant.in_stock_or_backorderable?- yield- if variant_was_out_of_stock && variant.in_stock_or_backorderable?- reload- variant.queue_webhooks_requests!('variant.back_in_stock')- end- end-- def queue_webhooks_requests_for_product_back_in_stock!- product_was_out_of_stock = !product.any_variant_in_stock_or_backorderable?- yield- if product_was_out_of_stock && product.any_variant_in_stock_or_backorderable?- product.queue_webhooks_requests!('product.back_in_stock')- end- end-- def queue_webhooks_requests_for_product_out_of_stock!- product_was_in_stock = product.any_variant_in_stock_or_backorderable?- yield- if product_was_in_stock && !product.any_variant_in_stock_or_backorderable?- product.queue_webhooks_requests!('product.out_of_stock')- end- end- end- end- end-end--Spree::StockMovement.prepend(Spree::Api::Webhooks::StockMovementDecorator)diff --git a/api/app/models/spree/api/webhooks/variant_decorator.rb b/api/app/models/spree/api/webhooks/variant_decorator.rbdeleted file mode 100644index 39bd5e99bc..0000000000--- a/api/app/models/spree/api/webhooks/variant_decorator.rb+++ /dev/null@@ -1,26 +0,0 @@-module Spree- module Api- module Webhooks- module VariantDecorator- def self.prepended(base)- def base.custom_webhook_events- %w[variant.back_in_stock variant.backorderable variant.discontinued variant.out_of_stock]- end-- base.after_update_commit :queue_webhooks_requests_for_variant_discontinued!- end-- private-- def queue_webhooks_requests_for_variant_discontinued!- return unless discontinue_on_previously_changed?- return if (change = discontinue_on_previous_change).blank? || change.last.blank?-- queue_webhooks_requests!('variant.discontinued')- end- end- end- end-end--Spree::Variant.prepend(Spree::Api::Webhooks::VariantDecorator)diff --git a/api/app/models/spree/order/webhooks.rb b/api/app/models/spree/order/webhooks.rbnew file mode 100644index 0000000000..2037bb3da4--- /dev/null+++ b/api/app/models/spree/order/webhooks.rb@@ -0,0 +1,39 @@+module Spree+ class Order < Spree::Base+ module Webhooks+ extend ActiveSupport::Concern+ include Spree::Webhooks::HasWebhooks++ included do+ after_update_commit :queue_webhooks_requests_for_order_resumed!+ end++ class_methods do+ def custom_webhook_events+ %w[order.canceled order.placed order.resumed order.shipped]+ end+ end++ def send_order_canceled_webhook+ queue_webhooks_requests!('order.canceled')+ end++ def send_order_placed_webhook+ queue_webhooks_requests!('order.placed')+ end++ def send_order_resumed_webhook+ queue_webhooks_requests!('order.resumed')+ self.state_machine_resumed = false # to not fire the same webhook twice+ end++ def queue_webhooks_requests_for_order_resumed!+ return if state_machine_resumed?+ return unless state_previously_changed?+ return unless state_previous_change&.last == 'resumed'++ send_order_resumed_webhook+ end+ end+ end+enddiff --git a/api/app/models/spree/payment/webhooks.rb b/api/app/models/spree/payment/webhooks.rbnew file mode 100644index 0000000000..108538b75e--- /dev/null+++ b/api/app/models/spree/payment/webhooks.rb@@ -0,0 +1,23 @@+module Spree+ class Payment < Spree::Base+ module Webhooks+ extend ActiveSupport::Concern+ include Spree::Webhooks::HasWebhooks++ class_methods do+ def custom_webhook_events+ %w[payment.paid payment.voided]+ end+ end++ def send_payment_voided_webhook+ queue_webhooks_requests!('payment.voided')+ end++ def send_payment_completed_webhook+ queue_webhooks_requests!('payment.paid')+ order.queue_webhooks_requests!('order.paid') if order.paid?+ end+ end+ end+enddiff --git a/api/app/models/spree/product/webhooks.rb b/api/app/models/spree/product/webhooks.rbnew file mode 100644index 0000000000..162af7c5a3--- /dev/null+++ b/api/app/models/spree/product/webhooks.rb@@ -0,0 +1,42 @@+module Spree+ class Product < Spree::Base+ module Webhooks+ extend ActiveSupport::Concern+ include Spree::Webhooks::HasWebhooks++ included do+ after_update_commit :queue_webhooks_requests_for_product_discontinued!+ end++ class_methods do+ def custom_webhook_events+ %w[product.back_in_stock product.backorderable product.discontinued+ product.out_of_stock product.activated product.archived product.drafted]+ end++ def ignored_attributes_for_update_webhook_event+ %w[status]+ end+ end++ def send_product_activated_webhook+ queue_webhooks_requests!('product.activated')+ end++ def send_product_archived_webhook+ queue_webhooks_requests!('product.archived')+ end++ def send_product_drafted_webhook+ queue_webhooks_requests!('product.drafted')+ end++ def queue_webhooks_requests_for_product_discontinued!+ return unless discontinue_on_previously_changed?+ return if (change = discontinue_on_previous_change).blank? || change.last.blank?++ queue_webhooks_requests!('product.discontinued')+ end+ end+ end+enddiff --git a/api/app/models/spree/shipment/webhooks.rb b/api/app/models/spree/shipment/webhooks.rbnew file mode 100644index 0000000000..043ef65e74--- /dev/null+++ b/api/app/models/spree/shipment/webhooks.rb@@ -0,0 +1,19 @@+module Spree+ class Shipment < Spree::Base+ module Webhooks+ extend ActiveSupport::Concern+ include Spree::Webhooks::HasWebhooks++ class_methods do+ def custom_webhook_events+ %w[shipment.shipped]+ end+ end++ def send_shipment_shipped_webhook+ queue_webhooks_requests!('shipment.shipped')+ order.queue_webhooks_requests!('order.shipped') if order.fully_shipped?+ end+ end+ end+enddiff --git a/api/app/models/spree/stock_item/webhooks.rb b/api/app/models/spree/stock_item/webhooks.rbnew file mode 100644index 0000000000..1e97c24ce3--- /dev/null+++ b/api/app/models/spree/stock_item/webhooks.rb@@ -0,0 +1,40 @@+module Spree+ class StockItem < Spree::Base+ module Webhooks+ extend ActiveSupport::Concern+ include Spree::Webhooks::HasWebhooks++ included do+ around_save :queue_webhooks_requests_for_variant_backorderable!+ around_save :queue_webhooks_requests_for_product_backorderable!+ end++ def queue_webhooks_requests_for_variant_backorderable!+ was_out_of_stock = !variant.in_stock_or_backorderable?+ was_not_backorderable = !variant_backorderable?+ yield+ if was_out_of_stock && was_not_backorderable && variant_backorderable?+ reload+ variant.queue_webhooks_requests!('variant.backorderable')+ end+ end++ def queue_webhooks_requests_for_product_backorderable!+ product_was_out_of_stock = !product.any_variant_in_stock_or_backorderable?+ product_was_not_backorderable = !product_backorderable?+ yield+ if product_was_out_of_stock && product_was_not_backorderable && product_backorderable?+ variant.product.queue_webhooks_requests!('product.backorderable')+ end+ end++ def product_backorderable?+ Spree::StockItem.exists?(backorderable: true, variant_id: variant.product.variants.ids)+ end++ def variant_backorderable?+ variant.stock_items.exists?(backorderable: true)+ end+ end+ end+enddiff --git a/api/app/models/spree/stock_movement/webhooks.rb b/api/app/models/spree/stock_movement/webhooks.rbnew file mode 100644index 0000000000..016f270f1b--- /dev/null+++ b/api/app/models/spree/stock_movement/webhooks.rb@@ -0,0 +1,49 @@+module Spree+ class StockMovement < Spree::Base+ module Webhooks+ extend ActiveSupport::Concern+ include Spree::Webhooks::HasWebhooks++ included do+ around_save :queue_webhooks_requests_for_variant_out_of_stock!+ around_save :queue_webhooks_requests_for_variant_back_in_stock!+ around_save :queue_webhooks_requests_for_product_out_of_stock!+ around_save :queue_webhooks_requests_for_product_back_in_stock!+ end++ def queue_webhooks_requests_for_variant_out_of_stock!+ variant_in_stock_before_update = variant.in_stock_or_backorderable?+ yield+ if variant_in_stock_before_update && !variant.in_stock_or_backorderable?+ reload+ stock_item.variant.queue_webhooks_requests!('variant.out_of_stock')+ end+ end++ def queue_webhooks_requests_for_variant_back_in_stock!+ variant_was_out_of_stock = !variant.in_stock_or_backorderable?+ yield+ if variant_was_out_of_stock && variant.in_stock_or_backorderable?+ reload+ variant.queue_webhooks_requests!('variant.back_in_stock')+ end+ end++ def queue_webhooks_requests_for_product_back_in_stock!+ product_was_out_of_stock = !product.any_variant_in_stock_or_backorderable?+ yield+ if product_was_out_of_stock && product.any_variant_in_stock_or_backorderable?+ product.queue_webhooks_requests!('product.back_in_stock')+ end+ end++ def queue_webhooks_requests_for_product_out_of_stock!+ product_was_in_stock = product.any_variant_in_stock_or_backorderable?+ yield+ if product_was_in_stock && !product.any_variant_in_stock_or_backorderable?+ product.queue_webhooks_requests!('product.out_of_stock')+ end+ end+ end+ end+enddiff --git a/api/app/models/spree/variant/webhooks.rb b/api/app/models/spree/variant/webhooks.rbnew file mode 100644index 0000000000..0d4ed40119--- /dev/null+++ b/api/app/models/spree/variant/webhooks.rb@@ -0,0 +1,25 @@+module Spree+ class Variant < Spree::Base+ module Webhooks+ extend ActiveSupport::Concern+ include Spree::Webhooks::HasWebhooks++ included do+ after_update_commit :queue_webhooks_requests_for_variant_discontinued!+ end++ class_methods do+ def custom_webhook_events+ %w[variant.back_in_stock variant.backorderable variant.discontinued variant.out_of_stock]+ end+ end++ def queue_webhooks_requests_for_variant_discontinued!+ return unless discontinue_on_previously_changed?+ return if (change = discontinue_on_previous_change).blank? || change.last.blank?++ queue_webhooks_requests!('variant.discontinued')+ end+ end+ end+enddiff --git a/api/lib/spree/api/engine.rb b/api/lib/spree/api/engine.rbindex a0c9672d6e..0b48b38a49 100644--- a/api/lib/spree/api/engine.rb+++ b/api/lib/spree/api/engine.rb@@ -18,17 +18,9 @@ module SpreeMigrations.new(config, engine_name).checkend- def self.activate- Dir.glob(File.join(File.dirname(__FILE__), '../../../app/models/spree/api/webhooks/*_decorator*.rb')) do |c|- Rails.application.config.cache_classes ? require(c) : load(c)- end- end-def self.root@root ||= Pathname.new(File.expand_path('../../..', __dir__))end-- config.to_prepare &method(:activate).to_procendendenddiff --git a/api/spec/models/spree/api/webhooks/order_decorator_spec.rb b/api/spec/models/spree/order_webhooks_spec.rbsimilarity index 98%rename from api/spec/models/spree/api/webhooks/order_decorator_spec.rbrename to api/spec/models/spree/order_webhooks_spec.rbindex 3590b320b1..8fef11e0e7 100644--- a/api/spec/models/spree/api/webhooks/order_decorator_spec.rb+++ b/api/spec/models/spree/order_webhooks_spec.rb@@ -1,6 +1,6 @@require 'spec_helper'-describe Spree::Api::Webhooks::OrderDecorator do+describe Spree::Order::Webhooks dolet(:store) { create(:store, default: true) }let(:webhook_payload_body) doSpree::Api::V2::Platform::OrderSerializer.new(diff --git a/api/spec/models/spree/api/webhooks/payment_decorator_spec.rb b/api/spec/models/spree/payment_webhooks_spec.rbsimilarity index 98%rename from api/spec/models/spree/api/webhooks/payment_decorator_spec.rbrename to api/spec/models/spree/payment_webhooks_spec.rbindex 85ce2fd984..8717f88047 100644--- a/api/spec/models/spree/api/webhooks/payment_decorator_spec.rb+++ b/api/spec/models/spree/payment_webhooks_spec.rb@@ -1,6 +1,6 @@require 'spec_helper'-describe Spree::Api::Webhooks::PaymentDecorator do+describe Spree::Payment::Webhooks dolet(:webhook_payload_body) doSpree::Api::V2::Platform::PaymentSerializer.new(payment,diff --git a/api/spec/models/spree/api/webhooks/product_decorator_spec.rb b/api/spec/models/spree/product_webhooks_spec.rbsimilarity index 97%rename from api/spec/models/spree/api/webhooks/product_decorator_spec.rbrename to api/spec/models/spree/product_webhooks_spec.rbindex a3591d2052..0dd6d62a5a 100644--- a/api/spec/models/spree/api/webhooks/product_decorator_spec.rb+++ b/api/spec/models/spree/product_webhooks_spec.rb@@ -1,6 +1,6 @@require 'spec_helper'-describe Spree::Api::Webhooks::ProductDecorator do+describe Spree::Product::Webhooks dolet(:product) { create(:product) }let(:webhook_payload_body) doSpree::Api::V2::Platform::ProductSerializer.new(diff --git a/api/spec/models/spree/api/webhooks/shipment_decorator_spec.rb b/api/spec/models/spree/shipment_webhooks_spec.rbsimilarity index 98%rename from api/spec/models/spree/api/webhooks/shipment_decorator_spec.rbrename to api/spec/models/spree/shipment_webhooks_spec.rbindex bd0272a53e..8d10b5a405 100644--- a/api/spec/models/spree/api/webhooks/shipment_decorator_spec.rb+++ b/api/spec/models/spree/shipment_webhooks_spec.rb@@ -1,6 +1,6 @@require 'spec_helper'-describe Spree::Api::Webhooks::ShipmentDecorator do+describe Spree::Shipment::Webhooks dolet(:order) { create(:order) }let(:shipment) { create(:shipment) }diff --git a/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb b/api/spec/models/spree/stock_item_webhooks_spec.rbsimilarity index 98%rename from api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rbrename to api/spec/models/spree/stock_item_webhooks_spec.rbindex 0bbbfd76a3..6dabeb3344 100644--- a/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb+++ b/api/spec/models/spree/stock_item_webhooks_spec.rb@@ -1,6 +1,6 @@require 'spec_helper'-describe Spree::Api::Webhooks::StockItemDecorator do+describe Spree::StockItem::Webhooks dolet(:webhook_payload_body) doSpree::Api::V2::Platform::VariantSerializer.new(variant,diff --git a/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb b/api/spec/models/spree/stock_movement_webhooks_spec.rbsimilarity index 99%rename from api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rbrename to api/spec/models/spree/stock_movement_webhooks_spec.rbindex 3752b7347b..ef49e0ef0f 100644--- a/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb+++ b/api/spec/models/spree/stock_movement_webhooks_spec.rb@@ -1,6 +1,6 @@require 'spec_helper'-describe Spree::Api::Webhooks::StockMovementDecorator do+describe Spree::StockMovement::Webhooks dolet(:webhook_payload_body) doSpree::Api::V2::Platform::VariantSerializer.new(variant.reload,diff --git a/api/spec/models/spree/api/webhooks/variant_decorator_spec.rb b/api/spec/models/spree/variant_webhooks_spec.rbsimilarity index 96%rename from api/spec/models/spree/api/webhooks/variant_decorator_spec.rbrename to api/spec/models/spree/variant_webhooks_spec.rbindex 04161a80ff..8c2277a1cc 100644--- a/api/spec/models/spree/api/webhooks/variant_decorator_spec.rb+++ b/api/spec/models/spree/variant_webhooks_spec.rb@@ -1,6 +1,6 @@require 'spec_helper'-describe Spree::Api::Webhooks::VariantDecorator do+describe Spree::Variant::Webhooks dolet(:variant) { create(:variant) }context 'emitting variant.discontinued' dodiff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rbindex ed612c56b3..73d54d4d7f 100644--- a/core/app/models/spree/order.rb+++ b/core/app/models/spree/order.rb@@ -17,6 +17,7 @@ module Spreeinclude Spree::Order::StoreCreditinclude Spree::Order::AddressBookinclude Spree::Order::Emails+ include Spree::Order::Webhooksinclude Spree::Core::NumberGenerator.new(prefix: 'R')include Spree::Core::TokenGenerator@@ -25,9 +26,6 @@ module Spreeinclude Spree::SingleStoreResourceinclude Spree::MemoizedDatainclude Spree::Metadata- if defined?(Spree::Webhooks::HasWebhooks)- include Spree::Webhooks::HasWebhooks- endif defined?(Spree::Security::Orders)include Spree::Security::Ordersend@@ -401,6 +399,8 @@ module Spreedeliver_order_confirmation_email unless confirmation_delivered?deliver_store_owner_order_notification_email if deliver_store_owner_order_notification_email?+ send_order_placed_webhook+consider_riskend@@ -726,11 +726,13 @@ module Spreesend_cancel_emailupdate_with_updater!+ send_order_canceled_webhookenddef after_resumeshipments.each(&:resume!)consider_risk+ send_order_resumed_webhookenddef use_billing?diff --git a/core/app/models/spree/order/webhooks.rb b/core/app/models/spree/order/webhooks.rbnew file mode 100644index 0000000000..5bfee711eb--- /dev/null+++ b/core/app/models/spree/order/webhooks.rb@@ -0,0 +1,19 @@+module Spree+ class Order < Spree::Base+ module Webhooks+ extend ActiveSupport::Concern++ def send_order_canceled_webhook+ # Implement your logic of sending cancale webhooks+ end++ def send_order_placed_webhook+ # Implement your logic of sending order placed webhooks+ end++ def send_order_resumed_webhook+ # Implement your logic of sending after resume webhooks+ end+ end+ end+enddiff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rbindex c8c06ad134..d1a24a8035 100644--- a/core/app/models/spree/payment.rb+++ b/core/app/models/spree/payment.rb@@ -6,14 +6,12 @@ module Spreeinclude Spree::NumberIdentifierinclude Spree::NumberAsParaminclude Spree::Metadata- if defined?(Spree::Webhooks::HasWebhooks)- include Spree::Webhooks::HasWebhooks- endif defined?(Spree::Security::Payments)include Spree::Security::Paymentsendinclude Spree::Payment::Processing+ include Spree::Payment::WebhooksNON_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'].freeze@@ -105,11 +103,11 @@ module Spreeevent :complete dotransition from: [:processing, :pending, :checkout], to: :completedend- after_transition to: :completed, do: :after_completed+ after_transition to: :completed, do: [:after_completed, :send_payment_completed_webhook]event :void dotransition from: [:pending, :processing, :completed, :checkout], to: :voidend- after_transition to: :void, do: :after_void+ after_transition to: :void, do: [:after_void, :send_payment_voided_webhook]# when the card brand isn't supportedevent :invalidate dotransition from: [:checkout], to: :invalid@@ -209,11 +207,11 @@ module Spreeprivatedef after_void- # this method is prepended in api/ to queue Webhooks requests+ # Implement your logic hereenddef after_completed- # this method is prepended in api/ to queue Webhooks requests+ # Implement your logic hereenddef has_invalid_state?diff --git a/core/app/models/spree/payment/webhooks.rb b/core/app/models/spree/payment/webhooks.rbnew file mode 100644index 0000000000..ac512ef5a4--- /dev/null+++ b/core/app/models/spree/payment/webhooks.rb@@ -0,0 +1,15 @@+module Spree+ class Payment < Spree::Base+ module Webhooks+ extend ActiveSupport::Concern++ def send_payment_voided_webhook+ # Implement your logic here+ end++ def send_payment_completed_webhook+ # Implement your logic here+ end+ end+ end+enddiff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rbindex 168adde4d5..a3e3eb03e5 100644--- a/core/app/models/spree/product.rb+++ b/core/app/models/spree/product.rb@@ -27,9 +27,7 @@ module Spreeinclude Spree::TranslatableResourceSluginclude Spree::MemoizedDatainclude Spree::Metadata- if defined?(Spree::Webhooks::HasWebhooks)- include Spree::Webhooks::HasWebhooks- end+ include Spree::Product::Webhooksif defined?(Spree::VendorConcern)include Spree::VendorConcernend@@ -181,17 +179,17 @@ module Spreeevent :activate dotransition to: :activeend- after_transition to: :active, do: :after_activate+ after_transition to: :active, do: [:after_activate, :send_product_activated_webhook]event :archive dotransition to: :archivedend- after_transition to: :archived, do: :after_archive+ after_transition to: :archived, do: [:after_archive, :send_product_archived_webhook]event :draft dotransition to: :draftend- after_transition to: :draft, do: :after_draft+ after_transition to: :draft, do: [:after_draft, :send_product_drafted_webhook]end# Can't use short form block syntax due to https://github.com/Netflix/fast_jsonapi/issues/259@@ -584,15 +582,15 @@ module Spreeenddef after_activate- # this method is prepended in api/ to queue Webhooks requests+ # Implement your logic hereenddef after_archive- # this method is prepended in api/ to queue Webhooks requests+ # Implement your logic hereenddef after_draft- # this method is prepended in api/ to queue Webhooks requests+ # Implement your logic hereendendenddiff --git a/core/app/models/spree/product/webhooks.rb b/core/app/models/spree/product/webhooks.rbnew file mode 100644index 0000000000..bbf2124c28--- /dev/null+++ b/core/app/models/spree/product/webhooks.rb@@ -0,0 +1,17 @@+module Spree+ class Product < Spree::Base+ module Webhooks+ def send_product_activated_webhook+ # Implement your logic here+ end++ def send_product_archived_webhook+ # Implement your logic here+ end++ def send_product_drafted_webhook+ # Implement your logic here+ end+ end+ end+enddiff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rbindex 6a272c77f5..0104ab7fe1 100644--- a/core/app/models/spree/shipment.rb+++ b/core/app/models/spree/shipment.rb@@ -6,9 +6,6 @@ module Spreeinclude Spree::NumberIdentifierinclude Spree::NumberAsParaminclude Spree::Metadata- if defined?(Spree::Webhooks::HasWebhooks)- include Spree::Webhooks::HasWebhooks- endif defined?(Spree::Security::Shipments)include Spree::Security::Shipmentsend@@ -16,6 +13,7 @@ module Spreeinclude Spree::VendorConcernendinclude Spree::Shipment::Emails+ include Spree::Shipment::Webhookswith_options inverse_of: :shipments dobelongs_to :address, class_name: 'Spree::Address'@@ -81,7 +79,7 @@ module Spreeevent :ship dotransition from: %i(ready canceled), to: :shippedend- after_transition to: :shipped, do: :after_ship+ after_transition to: :shipped, do: [:after_ship, :send_shipment_shipped_webhook]event :cancel dotransition to: :canceled, from: %i(pending ready)diff --git a/core/app/models/spree/shipment/webhooks.rb b/core/app/models/spree/shipment/webhooks.rbnew file mode 100644index 0000000000..119aa84ea0--- /dev/null+++ b/core/app/models/spree/shipment/webhooks.rb@@ -0,0 +1,11 @@+module Spree+ class Shipment < Spree::Base+ module Webhooks+ extend ActiveSupport::Concern++ def send_shipment_shipped_webhook+ # Implement your logic here+ end+ end+ end+enddiff --git a/core/app/models/spree/stock_item.rb b/core/app/models/spree/stock_item.rbindex 45759b9a9f..eac0efa8e9 100644--- a/core/app/models/spree/stock_item.rb+++ b/core/app/models/spree/stock_item.rb@@ -3,9 +3,7 @@ module Spreeacts_as_paranoidinclude Spree::Metadata- if defined?(Spree::Webhooks::HasWebhooks)- include Spree::Webhooks::HasWebhooks- end+ include Spree::StockItem::Webhookswith_options inverse_of: :stock_items dobelongs_to :stock_location, class_name: 'Spree::StockLocation'diff --git a/core/app/models/spree/stock_item/webhooks.rb b/core/app/models/spree/stock_item/webhooks.rbnew file mode 100644index 0000000000..c02eebacde--- /dev/null+++ b/core/app/models/spree/stock_item/webhooks.rb@@ -0,0 +1,6 @@+module Spree+ class StockItem < Spree::Base+ module Webhooks+ end+ end+enddiff --git a/core/app/models/spree/stock_movement.rb b/core/app/models/spree/stock_movement.rbindex aaa5ebfa2a..b3835f0028 100644--- a/core/app/models/spree/stock_movement.rb+++ b/core/app/models/spree/stock_movement.rb@@ -5,9 +5,7 @@ module Spreemin: -2**31}.freeze- if defined?(Spree::Webhooks::HasWebhooks)- include Spree::Webhooks::HasWebhooks- end+ include Spree::StockMovement::Webhooksbelongs_to :stock_item, class_name: 'Spree::StockItem', inverse_of: :stock_movementsbelongs_to :originator, polymorphic: truediff --git a/core/app/models/spree/stock_movement/webhooks.rb b/core/app/models/spree/stock_movement/webhooks.rbnew file mode 100644index 0000000000..9464a82d11--- /dev/null+++ b/core/app/models/spree/stock_movement/webhooks.rb@@ -0,0 +1,6 @@+module Spree+ class StockMovement < Spree::Base+ module Webhooks+ end+ end+enddiff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex fb6a6ef0bc..594b57df82 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -5,9 +5,7 @@ module Spreeinclude Spree::MemoizedDatainclude Spree::Metadata- if defined?(Spree::Webhooks::HasWebhooks)- include Spree::Webhooks::HasWebhooks- end+ include Spree::Variant::WebhooksMEMOIZED_METHODS = %w(purchasable in_stock backorderable tax_category options_text compare_at_price)diff --git a/core/app/models/spree/variant/webhooks.rb b/core/app/models/spree/variant/webhooks.rbnew file mode 100644index 0000000000..69466d4599--- /dev/null+++ b/core/app/models/spree/variant/webhooks.rb@@ -0,0 +1,6 @@+module Spree+ class Variant < Spree::Base+ module Webhooks+ end+ end+end