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
Add callback after state machine event transitions.
Sebastian Palma2b2921f57d3c
diff --git a/api/app/models/concerns/spree/webhooks/has_webhooks.rb b/api/app/models/concerns/spree/webhooks/has_webhooks.rbdeleted file mode 100644index 5df945f241..0000000000--- a/api/app/models/concerns/spree/webhooks/has_webhooks.rb+++ /dev/null@@ -1,24 +0,0 @@-module Spree- module Webhooks- module HasWebhooks- extend ActiveSupport::Concern-- included do- after_commit :execute_webhook_logic!,- on: %i[create destroy update],- unless: proc { self.class.module_parents.include?(Spree::Webhooks) }- end-- private-- def execute_webhook_logic!- uri = URI('https://google.com/')- request = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')- request.body = {foo: :bar}.to_json- http = Net::HTTP.new(uri.host, uri.port)- http.use_ssl = true- http.request(request)- end- end- end-enddiff --git a/api/app/models/spree/webhooks/endpoint.rb b/api/app/models/spree/webhooks/endpoint.rbdeleted file mode 100644index 97b1f830d4..0000000000--- a/api/app/models/spree/webhooks/endpoint.rb+++ /dev/null@@ -1,16 +0,0 @@-module Spree- module Webhooks- class Endpoint < Spree::Base- self.table_name = 'spree_webhooks_endpoints'-- before_save :assign_default_subscriptions,- if: proc { ActiveRecord::Base.connection.adapter_name == 'Mysql2' }-- private-- def assign_default_subscriptions- self.subscriptions = ['*'] if subscriptions.blank?- end- end- end-enddiff --git a/api/app/models/spree/webhooks/event.rb b/api/app/models/spree/webhooks/event.rbdeleted file mode 100644index 586d0ffa6a..0000000000--- a/api/app/models/spree/webhooks/event.rb+++ /dev/null@@ -1,7 +0,0 @@-module Spree- module Webhooks- class Event < Spree::Base- self.table_name = 'spree_webhooks_events'- end- end-enddiff --git a/api/spec/spec_helper.rb b/api/spec/spec_helper.rbindex 998de3c110..ec98f89e76 100644--- a/api/spec/spec_helper.rb+++ b/api/spec/spec_helper.rb@@ -27,7 +27,6 @@ endrequire 'rspec/rails'require 'ffaker'-require 'webmock/rspec'# Requires supporting ruby files with custom matchers and macros, etc,# in spec/support/ and its subdirectories.diff --git a/core/app/models/concerns/spree/webhooks/has_webhooks.rb b/core/app/models/concerns/spree/webhooks/has_webhooks.rbnew file mode 100644index 0000000000..4c1eae4576--- /dev/null+++ b/core/app/models/concerns/spree/webhooks/has_webhooks.rb@@ -0,0 +1,22 @@+module Spree+ module Webhooks+ module HasWebhooks+ extend ActiveSupport::Concern++ included do+ after_commit :execute_webhook_logic!,+ on: %i[create destroy update],+ unless: proc { self.class.module_parents.include?(Spree::Webhooks) }++ def execute_webhook_logic!+ uri = URI('https://google.com/')+ request = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')+ request.body = {foo: :bar}.to_json+ http = Net::HTTP.new(uri.host, uri.port)+ http.use_ssl = true+ http.request(request)+ end+ end+ end+ end+enddiff --git a/core/app/models/spree/base.rb b/core/app/models/spree/base.rbindex cf05b0e7af..2754f7f661 100644--- a/core/app/models/spree/base.rb+++ b/core/app/models/spree/base.rb@@ -3,6 +3,7 @@ class Spree::Base < ApplicationRecordserialize :preferences, Hashinclude Spree::RansackableAttributes+ include Spree::Webhooks::HasWebhooksafter_initialize doif has_attribute?(:preferences) && !preferences.nil?diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rbindex eb363198f3..2e44161a09 100644--- a/core/app/models/spree/order.rb+++ b/core/app/models/spree/order.rb@@ -375,6 +375,8 @@ module Spreedeliver_store_owner_order_notification_email if deliver_store_owner_order_notification_email?consider_risk++ execute_webhook_logic!enddef fulfill!@@ -698,6 +700,8 @@ module Spreesend_cancel_emailupdate_with_updater!++ execute_webhook_logic!enddef after_resumediff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rbindex be52a2231a..d9258fa72e 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+ execute_webhook_logic!+ end+def has_invalid_state?INVALID_STATES.include?(state)enddiff --git a/core/app/models/spree/shipment_handler.rb b/core/app/models/spree/shipment_handler.rbindex ccfb4f1eb4..ca481fab37 100644--- a/core/app/models/spree/shipment_handler.rb+++ b/core/app/models/spree/shipment_handler.rb@@ -23,6 +23,7 @@ module Spree@shipment.touch :shipped_atupdate_order_shipment_statesend_shipped_email+ @shipment.execute_webhook_logic!endprotecteddiff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rbindex db9b71d70a..671fb17002 100644--- a/core/spec/models/spree/order_spec.rb+++ b/core/spec/models/spree/order_spec.rb@@ -10,6 +10,11 @@ describe Spree::Order, type: :model dolet(:user) { stub_model(Spree::LegacyUser, email: '[email protected]') }let!(:store) { create(:store, default: true) }let(:order) { stub_model(Spree::Order, user: user, store: store) }+ let(:url) { 'https://google.com' }++ before(:all) do+ stub_request(:any, 'https://google.com')+ endbefore doallow(Spree::LegacyUser).to receive_messages(current: mock_model(Spree::LegacyUser, id: 123))@@ -65,6 +70,16 @@ describe Spree::Order, type: :model doexpect(order.payments.first).to be_voidend++ it 'executes a webhook callback' do+ order.cancel+ expect(WebMock).to(+ have_requested(:post, 'https://google.com').with(+ body: {foo: :bar}.to_json,+ headers: {'Content-Type' => 'application/json'}+ ).times(72)+ )+ endendcontext '#canceled_by' do@@ -176,6 +191,16 @@ describe Spree::Order, type: :model doorder.finalize!end+ it 'executes a webhook callback' do+ order.finalize!+ expect(WebMock).to(+ have_requested(:post, 'https://google.com').with(+ body: {foo: :bar}.to_json,+ headers: {'Content-Type' => 'application/json'}+ ).times(6)+ )+ end+context 'order is considered risky' dobefore doallow(order).to receive_messages is_risky?: truediff --git a/core/spec/models/spree/payment_spec.rb b/core/spec/models/spree/payment_spec.rbindex f2b8c56cc9..77839f0a5c 100644--- a/core/spec/models/spree/payment_spec.rb+++ b/core/spec/models/spree/payment_spec.rb@@ -36,6 +36,10 @@ describe Spree::Payment, type: :model doActiveMerchant::Billing::Response.new(false, '', {}, {})end+ before(:all) do+ stub_request(:any, 'https://google.com')+ end+before do# Rails >= 5.0.3 returns new object for association so we ugly mock itallow(payment).to receive(:log_entries).and_return(payment.log_entries)@@ -957,4 +961,66 @@ describe Spree::Payment, type: :model doit { expect(payment.source).to be_nil }endend++ describe 'event void' do+ context 'when transitioning from pending to void' do+ before do+ payment.pend+ end++ it 'executes a webhook callback' do+ payment.void+ expect(WebMock).to(+ have_requested(:post, 'https://google.com').with(+ body: {foo: :bar}.to_json,+ headers: {'Content-Type' => 'application/json'}+ ).times(12)+ )+ end+ end++ context 'when transitioning from processing to void' do+ before do+ payment.started_processing+ end++ it 'executes a webhook callback' do+ payment.void+ expect(WebMock).to(+ have_requested(:post, 'https://google.com').with(+ body: {foo: :bar}.to_json,+ headers: {'Content-Type' => 'application/json'}+ ).times(12)+ )+ end+ end++ context 'when transitioning from completed to void' do+ before do+ payment.complete+ end++ it 'executes a webhook callback' do+ payment.void+ expect(WebMock).to(+ have_requested(:post, 'https://google.com').with(+ body: {foo: :bar}.to_json,+ headers: {'Content-Type' => 'application/json'}+ ).times(12)+ )+ end+ end++ context 'when transitioning from checkout to void' do+ it 'executes a webhook callback' do+ payment.void+ expect(WebMock).to(+ have_requested(:post, 'https://google.com').with(+ body: {foo: :bar}.to_json,+ headers: {'Content-Type' => 'application/json'}+ ).times(9)+ )+ end+ end+ endenddiff --git a/core/spec/models/spree/shipment_spec.rb b/core/spec/models/spree/shipment_spec.rbindex a6b6cdbe7f..1f6b731292 100644--- a/core/spec/models/spree/shipment_spec.rb+++ b/core/spec/models/spree/shipment_spec.rb@@ -22,6 +22,10 @@ describe Spree::Shipment, type: :model dolet(:variant) { mock_model(Spree::Variant) }let(:line_item) { mock_model(Spree::LineItem, variant: variant) }+ before(:all) do+ stub_request(:any, 'https://google.com')+ end+def create_shipment(order, stock_location)order.shipments.create(stock_location_id: stock_location.id).inventory_units.create(order_id: order.id,@@ -369,6 +373,18 @@ describe Spree::Shipment, type: :model doshipment.update!(order)end+ it 'executes a webhook callback' do+ allow(shipment).to receive_messages determine_state: 'shipped'+ shipment.update!(order)++ expect(WebMock).to(+ have_requested(:post, 'https://google.com').with(+ body: {foo: :bar}.to_json,+ headers: {'Content-Type' => 'application/json'}+ ).times(13)+ )+ end+context 'when using the default shipment handler' doit "calls the 'perform' method" doshipment.state = 'pending'diff --git a/api/spec/models/spree/webhooks/has_webhooks_spec.rb b/core/spec/models/spree/webhooks/has_webhooks_spec.rbsimilarity index 100%rename from api/spec/models/spree/webhooks/has_webhooks_spec.rbrename to core/spec/models/spree/webhooks/has_webhooks_spec.rbdiff --git a/core/spec/spec_helper.rb b/core/spec/spec_helper.rbindex 0623d3f17e..493c9787a8 100644--- a/core/spec/spec_helper.rb+++ b/core/spec/spec_helper.rb@@ -34,6 +34,7 @@ endrequire 'rspec/rails'require 'database_cleaner'require 'ffaker'+require 'webmock/rspec'Dir['./spec/support/**/*.rb'].sort.each { |f| require f }