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
Prepend after_transition methods, not events. Add Spree::Webhooks::Base.
Sebastian Palmac8c57b34b99a
diff --git a/api/app/models/spree/api/webhooks/order.rb b/api/app/models/spree/api/webhooks/order.rbindex 6265132cd1..1a7eaa7b01 100644--- a/api/app/models/spree/api/webhooks/order.rb+++ b/api/app/models/spree/api/webhooks/order.rb@@ -2,7 +2,7 @@ module Spreemodule Apimodule Webhooksmodule Order- def cancel+ def after_cancelsuperSpree::Webhooks::Endpoints::QueueRequests.call(event: 'order.cancel', payload: {})enddiff --git a/api/app/models/spree/api/webhooks/payment.rb b/api/app/models/spree/api/webhooks/payment.rbindex 6e3dda54b0..ade2261f19 100644--- a/api/app/models/spree/api/webhooks/payment.rb+++ b/api/app/models/spree/api/webhooks/payment.rb@@ -2,7 +2,7 @@ module Spreemodule Apimodule Webhooksmodule Payment- def void+ def after_voidsuperSpree::Webhooks::Endpoints::QueueRequests.call(event: 'payment.void', payload: {})enddiff --git a/api/app/models/spree/api/webhooks/shipment.rb b/api/app/models/spree/api/webhooks/shipment.rbindex 1594bcdca3..7f46977c5d 100644--- a/api/app/models/spree/api/webhooks/shipment.rb+++ b/api/app/models/spree/api/webhooks/shipment.rb@@ -2,7 +2,7 @@ module Spreemodule Apimodule Webhooksmodule Shipment- def ship+ def after_shipsuperSpree::Webhooks::Endpoints::QueueRequests.call(event: 'shipment.ship', payload: {})enddiff --git a/api/app/models/spree/webhooks/base.rb b/api/app/models/spree/webhooks/base.rbnew file mode 100644index 0000000000..8c42ffd3e4--- /dev/null+++ b/api/app/models/spree/webhooks/base.rb@@ -0,0 +1,11 @@+module Spree+ module Webhooks+ def self.table_name_prefix+ 'spree_webhooks_'+ end++ class Base < Spree::Base+ self.abstract_class = true+ end+ end+enddiff --git a/api/app/models/spree/webhooks/endpoint.rb b/api/app/models/spree/webhooks/endpoint.rbindex 9ec80b5f46..b047db4714 100644--- a/api/app/models/spree/webhooks/endpoint.rb+++ b/api/app/models/spree/webhooks/endpoint.rb@@ -1,7 +1,7 @@module Spreemodule Webhooks- class Endpoint < Spree::Base- self.table_name = 'spree_webhooks_endpoints'+ class Endpoint < Spree::Webhooks::Base+ validates :url, presence: trueendendenddiff --git a/api/app/services/spree/webhooks/endpoints/make_request.rb b/api/app/services/spree/webhooks/endpoints/make_request.rbindex 5101bcb4f8..bdf1fcc291 100644--- a/api/app/services/spree/webhooks/endpoints/make_request.rb+++ b/api/app/services/spree/webhooks/endpoints/make_request.rb@@ -2,29 +2,30 @@ module Spreemodule Webhooksmodule Endpointsclass MakeRequest- prepend Spree::ServiceModule::Base+ prepend Spree::ServiceModule::Base- def call(url:)- run :make_request- end+ def call(url:)+ run :make_request+ end- private+ private- HEADERS = { 'Content-Type' => 'application/json' }.freeze- private_constant :HEADERS+ HEADERS = { 'Content-Type' => 'application/json' }.freeze+ private_constant :HEADERS- def make_request(url:)- uri = URI(url)- if uri.path == "" && uri.host.nil? && uri.port.nil?- failure(false)- else- request = Net::HTTP::Post.new(uri.path, HEADERS)- request.body = {foo: :bar}.to_json- http = Net::HTTP.new(uri.host, uri.port)- http.use_ssl = true- success(http.request(request))+ def make_request(url:)+ uri = URI(url)+ if uri.path == "" && uri.host.nil? && uri.port.nil?+ failure(false)+ else+ request = Net::HTTP::Post.new(uri.path, HEADERS)+ request.body = {foo: :bar}.to_json+ http = Net::HTTP.new(uri.host, uri.port)+ http.use_ssl = true+ success(http.request(request))+ end+ endendendendend-diff --git a/api/spec/models/spree/api/webhooks/order_spec.rb b/api/spec/models/spree/api/webhooks/order_spec.rbindex df9c160181..18d8094a2e 100644--- a/api/spec/models/spree/api/webhooks/order_spec.rb+++ b/api/spec/models/spree/api/webhooks/order_spec.rb@@ -11,7 +11,7 @@ describe Spree::Order doallow(queue_requests).to receive(:call).with(any_args)end- context '#cancel' do+ context '#after_cancel' dolet(:order) { create(:completed_order_with_totals, store: store) }it 'executes QueueRequests.call with a order.cancel event and {} payload after invoking cancel' dodiff --git a/api/spec/models/spree/api/webhooks/shipment_spec.rb b/api/spec/models/spree/api/webhooks/shipment_spec.rbindex 46e0a3bb35..381892a9d1 100644--- a/api/spec/models/spree/api/webhooks/shipment_spec.rb+++ b/api/spec/models/spree/api/webhooks/shipment_spec.rb@@ -3,7 +3,7 @@ require 'spec_helper'describe Spree::Shipment dolet(:shipment) { create(:shipment) }- context '#ship' do+ context '#after_ship' dobefore doallow(Spree::Webhooks::Endpoints::QueueRequests).to receive(:new).and_return(queue_requests)allow(queue_requests).to receive(:call).with(any_args)diff --git a/api/spec/models/spree/webhooks/endpoint_spec.rb b/api/spec/models/spree/webhooks/endpoint_spec.rbnew file mode 100644index 0000000000..dbd68569c9--- /dev/null+++ b/api/spec/models/spree/webhooks/endpoint_spec.rb@@ -0,0 +1,15 @@+require 'spec_helper'++describe Spree::Store do+ describe 'validations' do+ context 'url presence' do+ it 'is valid with url' do+ expect(build(:endpoint, url: 'https://google.com/').valid?).to be(true)+ end++ it 'is invalid without url' do+ expect(build(:endpoint, url: nil).valid?).to be(false)+ end+ end+ end+enddiff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rbindex be52a2231a..85313d24b5 100644--- a/core/app/models/spree/payment.rb+++ b/core/app/models/spree/payment.rb@@ -97,6 +97,7 @@ module Spreeevent :void dotransition from: [:pending, :processing, :completed, :checkout], to: :voidend+ after_transition to: :void, do: :after_void# when the card brand isnt supportedevent :invalidate dotransition from: [:checkout], to: :invalid@@ -195,6 +196,10 @@ module Spreeprivate+ def after_void+ # this method is prepended in api/ to queue Webhooks requests+ end+def has_invalid_state?INVALID_STATES.include?(state)enddiff --git a/core/lib/spree/testing_support/factories/endpoint_factory.rb b/core/lib/spree/testing_support/factories/endpoint_factory.rbnew file mode 100644index 0000000000..31951814b8--- /dev/null+++ b/core/lib/spree/testing_support/factories/endpoint_factory.rb@@ -0,0 +1,4 @@+FactoryBot.define do+ factory :endpoint, class: Spree::Webhooks::Endpoint do+ end+end