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 api Order/Payment/Shipment after load hook.
Sebastian Palmac7f94dcd8825
diff --git a/api/app/jobs/spree/webhooks/endpoints/make_request_job.rb b/api/app/jobs/spree/webhooks/endpoints/make_request_job.rbindex d37ba060f1..900a6ead4d 100644--- a/api/app/jobs/spree/webhooks/endpoints/make_request_job.rb+++ b/api/app/jobs/spree/webhooks/endpoints/make_request_job.rb@@ -4,8 +4,8 @@ module Spreeclass MakeRequestJob < Spree::BaseJobqueue_as :spree_webhooks- def perform(payload, url)- Spree::Webhooks::Endpoints::MakeRequest.call(payload: payload, url: url)+ def perform(body, url)+ Spree::Webhooks::Endpoints::MakeRequest.call(body: body, url: url)endendenddiff --git a/api/app/models/concerns/spree/webhooks/has_webhooks.rb b/api/app/models/concerns/spree/webhooks/has_webhooks.rbindex df74e623a7..f19fa3e9aa 100644--- a/api/app/models/concerns/spree/webhooks/has_webhooks.rb+++ b/api/app/models/concerns/spree/webhooks/has_webhooks.rb@@ -9,11 +9,9 @@ module Spreeafter_update_commit(proc { queue_webhooks_requests!(event_name(:update)) })def queue_webhooks_requests!(event)- return if disable_spree_webhooks?- return if webhooks_descendant?- return if payload.blank?+ return if disable_spree_webhooks? || webhooks_descendant? || body.blank?- Spree::Webhooks::Endpoints::QueueRequests.call(event: event, payload: payload)+ Spree::Webhooks::Endpoints::QueueRequests.call(event: event, body: body)endend@@ -31,7 +29,7 @@ module Spreeendend- def payload+ def bodyresource_serializer.new(self).serializable_hash.to_jsonrescue NameError''diff --git a/api/app/services/spree/webhooks/endpoints/make_request.rb b/api/app/services/spree/webhooks/endpoints/make_request.rbindex 54681d8d10..e7fac1130c 100644--- a/api/app/services/spree/webhooks/endpoints/make_request.rb+++ b/api/app/services/spree/webhooks/endpoints/make_request.rb@@ -25,7 +25,7 @@ module Spreerequest = Net::HTTP::Post.new(uri.path, HEADERS)request.body = bodyhttp = Net::HTTP.new(uri.host, uri.port)- http.use_ssl = true+ http.use_ssl = true unless Rails.env.development? || Rails.env.test?code_type = http.request(request).code_typesuccess(code_type == Net::HTTPOK)enddiff --git a/api/app/services/spree/webhooks/endpoints/queue_requests.rb b/api/app/services/spree/webhooks/endpoints/queue_requests.rbindex f8b1e68b1b..269f1617d0 100644--- a/api/app/services/spree/webhooks/endpoints/queue_requests.rb+++ b/api/app/services/spree/webhooks/endpoints/queue_requests.rb@@ -4,9 +4,9 @@ module Spreeclass QueueRequestsprepend Spree::ServiceModule::Base- def call(event:, payload:)+ def call(body:, event:)urls_subscribed_to(event).each do |url|- Spree::Webhooks::Endpoints::MakeRequestJob.perform_later(payload, url)+ Spree::Webhooks::Endpoints::MakeRequestJob.perform_later(body, url)endenddiff --git a/api/lib/spree/api/engine.rb b/api/lib/spree/api/engine.rbindex e9e5703cc1..6b7a1637b7 100644--- a/api/lib/spree/api/engine.rb+++ b/api/lib/spree/api/engine.rb@@ -25,10 +25,18 @@ module Spreeinitializer 'Spree::Webhooks' doActiveSupport.on_load(:spree_base) doinclude Spree::Webhooks::HasWebhooks+ end++ ActiveSupport.on_load(:spree_order) do+ prepend Spree::Api::Webhooks::Order+ end++ ActiveSupport.on_load(:spree_payment) do+ prepend Spree::Api::Webhooks::Payment+ end- Spree::Order.prepend(Spree::Api::Webhooks::Order)- Spree::Payment.prepend(Spree::Api::Webhooks::Payment)- Spree::Shipment.prepend(Spree::Api::Webhooks::Shipment)+ ActiveSupport.on_load(:spree_shipment) do+ prepend Spree::Api::Webhooks::Shipmentendenddiff --git a/api/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rb b/api/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rbindex cc4e4661cf..dd1f6741c9 100644--- a/api/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rb+++ b/api/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rb@@ -2,10 +2,10 @@ require 'spec_helper'describe Spree::Webhooks::Endpoints::MakeRequestJob, :job dolet(:url) { 'http://url.com/' }- let(:payload) { {} }+ let(:body) { {} }- it 'it enqueues a HTTP request using Spree::Webhooks::Endpoints::MakeRequest' do- expect { described_class.perform_later(payload, url) }.to(+ it 'enqueues a HTTP request using Spree::Webhooks::Endpoints::MakeRequest' do+ expect { described_class.perform_later(body, url) }.to(have_enqueued_job.on_queue('spree_webhooks'))enddiff --git a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rbindex 06c7b93a22..d3326cf21c 100644--- a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb+++ b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb@@ -95,42 +95,42 @@ describe Spree::Webhooks::HasWebhooks docontext 'without a Spree::Webhooks descendant class' docontext 'with a resource serializer' do- let(:payload) { Spree::Api::V2::Platform::ProductSerializer.new(product).serializable_hash.to_json }+ let(:body) { Spree::Api::V2::Platform::ProductSerializer.new(product).serializable_hash.to_json }context 'after_create_commit' doit 'queues a request through QueueRequests for product.create' do- expect(queue_requests).to have_received(:call).with(event: 'product.create', payload: payload).once+ expect(queue_requests).to have_received(:call).with(event: 'product.create', body: body).onceendendcontext 'after_destroy_commit' doit 'queues a request through QueueRequests for product.destroy' doproduct.destroy- expect(queue_requests).to have_received(:call).with(event: 'product.destroy', payload: payload).once+ expect(queue_requests).to have_received(:call).with(event: 'product.destroy', body: body).onceendendcontext 'after_update_commit' doit 'queues a request through QueueRequests for product.update' doproduct.update(name: 'updated')- expect(queue_requests).to have_received(:call).with(event: 'product.update', payload: payload).once+ expect(queue_requests).to have_received(:call).with(event: 'product.update', body: body).onceendendcontext 'with a class name with multiple words' dolet!(:cms_page) { create(:cms_homepage, store: store, locale: 'en') }- let(:payload) do+ let(:body) doSpree::Api::V2::Platform::CmsPageSerializer.new(cms_page).serializable_hash.to_jsonendlet(:underscore_event) { 'cms_page.create' }it 'underscorize the event name' do- expect(queue_requests).to have_received(:call).with(event: underscore_event, payload: payload).once+ expect(queue_requests).to have_received(:call).with(event: underscore_event, body: body).onceendendend- context 'without a resource serializer - blank payload' do+ context 'without a resource serializer - blank body' dolet(:product) { Spree::TestProduct.new(name: 'test') }context 'after_create_commit' dodiff --git a/api/spec/models/spree/api/webhooks/order_spec.rb b/api/spec/models/spree/api/webhooks/order_spec.rbindex 179ed2f4d2..682d274603 100644--- a/api/spec/models/spree/api/webhooks/order_spec.rb+++ b/api/spec/models/spree/api/webhooks/order_spec.rb@@ -4,7 +4,7 @@ describe Spree::Order dodescribe 'sending webhooks after transitioning from states' dolet(:queue_requests) { instance_double(Spree::Webhooks::Endpoints::QueueRequests) }let(:store) { create(:store, default: true) }- let(:payload) do+ let(:body) doSpree::Api::V2::Platform::OrderSerializer.new(order).serializable_hash.to_jsonend@@ -19,10 +19,10 @@ describe Spree::Order dodescribe '#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' do+ it 'executes QueueRequests.call with a order.cancel event and {} body after invoking cancel' doTimecop.freeze(Time.local(1990)) doorder.cancel- expect(queue_requests).to have_received(:call).with(event: 'order.cancel', payload: payload).once+ expect(queue_requests).to have_received(:call).with(event: 'order.cancel', body: body).onceendendend@@ -30,9 +30,9 @@ describe Spree::Order dodescribe '#finalize!' dolet(:order) { described_class.create(email: '[email protected]', store: store) }- it 'executes QueueRequests.call with a order.complete event and {} payload after invoking finalize!' do+ it 'executes QueueRequests.call with a order.complete event and {} body after invoking finalize!' doorder.finalize!- expect(queue_requests).to have_received(:call).with(event: 'order.complete', payload: payload).once+ expect(queue_requests).to have_received(:call).with(event: 'order.complete', body: body).onceendendenddiff --git a/api/spec/models/spree/api/webhooks/payment_spec.rb b/api/spec/models/spree/api/webhooks/payment_spec.rbindex b9dbc61354..1ae8351fba 100644--- a/api/spec/models/spree/api/webhooks/payment_spec.rb+++ b/api/spec/models/spree/api/webhooks/payment_spec.rb@@ -4,7 +4,7 @@ describe Spree::Payment dolet(:payment) { create(:payment) }describe '#void' do- let(:payload) { Spree::Api::V2::Platform::PaymentSerializer.new(payment).serializable_hash.to_json }+ let(:body) { Spree::Api::V2::Platform::PaymentSerializer.new(payment).serializable_hash.to_json }let(:queue_requests) { instance_double(Spree::Webhooks::Endpoints::QueueRequests) }shared_examples 'queues a webhook request' do@@ -12,7 +12,7 @@ describe Spree::Payment doENV['DISABLE_SPREE_WEBHOOKS'] = nil# Avoid creating a new state change after transitioning as is defined in the model# because after_void queues the HTTP request before finishing the transition, hence- # the total state changes that are sent in the payload is one less.+ # the total state changes that are sent in the body is one less.allow(payment).to receive_message_chain(:state_changes, :create!)allow(Spree::Webhooks::Endpoints::QueueRequests).to receive(:new).and_return(queue_requests)allow(queue_requests).to receive(:call).with(any_args)@@ -20,9 +20,9 @@ describe Spree::Payment doafter { ENV['DISABLE_SPREE_WEBHOOKS'] = 'true' }- it 'executes QueueRequests.call with a payment.void event and {} payload after invoking void' do+ it 'executes QueueRequests.call with a payment.void event and {} body after invoking void' dopayment.void- expect(queue_requests).to have_received(:call).with(event: 'payment.void', payload: payload).once+ expect(queue_requests).to have_received(:call).with(event: 'payment.void', body: body).onceendenddiff --git a/api/spec/models/spree/api/webhooks/shipment_spec.rb b/api/spec/models/spree/api/webhooks/shipment_spec.rbindex 7a664cbb4a..0e9a7e4c29 100644--- a/api/spec/models/spree/api/webhooks/shipment_spec.rb+++ b/api/spec/models/spree/api/webhooks/shipment_spec.rb@@ -2,14 +2,14 @@ require 'spec_helper'describe Spree::Shipment dolet(:shipment) { create(:shipment) }- let(:payload) { Spree::Api::V2::Platform::ShipmentSerializer.new(shipment).serializable_hash.to_json }+ let(:body) { Spree::Api::V2::Platform::ShipmentSerializer.new(shipment).serializable_hash.to_json }describe '#after_ship' dobefore doENV['DISABLE_SPREE_WEBHOOKS'] = nil# Avoid creating a new state change after transitioning as is defined in the model- # because after_void queues the HTTP request before finishing the transition, hence- # the total state changes that are sent in the payload is one less.+ # because after_ship queues the HTTP request before finishing the transition, hence+ # the total state changes that are sent in the body is one less.allow(shipment).to receive_message_chain(:state_changes, :create!)allow(Spree::Webhooks::Endpoints::QueueRequests).to receive(:new).and_return(queue_requests)allow(queue_requests).to receive(:call).with(any_args)@@ -19,10 +19,10 @@ describe Spree::Shipment dolet(:queue_requests) { instance_double(Spree::Webhooks::Endpoints::QueueRequests) }- it 'executes QueueRequests.call with a shipment.ship event and {} payload after invoking ship' do+ it 'executes QueueRequests.call with a shipment.ship event and {} body after invoking ship' doshipment.cancel # previous state that allows the object be shippedshipment.ship- expect(queue_requests).to have_received(:call).with(event: 'shipment.ship', payload: payload).once+ expect(queue_requests).to have_received(:call).with(event: 'shipment.ship', body: body).onceendendenddiff --git a/api/spec/services/spree/webhooks/endpoints/make_request_spec.rb b/api/spec/services/spree/webhooks/endpoints/make_request_spec.rbindex c04c33ec00..2419d7ae2c 100644--- a/api/spec/services/spree/webhooks/endpoints/make_request_spec.rb+++ b/api/spec/services/spree/webhooks/endpoints/make_request_spec.rb@@ -4,7 +4,7 @@ describe Spree::Webhooks::Endpoints::MakeRequest dodescribe '#call' dosubject { described_class.call(body: body, url: url) }- let(:url) { 'https://google.com/' }+ let(:url) { 'http://google.com/' }let(:body) { { foo: :bar }.to_json }shared_examples 'returns a failure without making a request' do@@ -56,12 +56,11 @@ describe Spree::Webhooks::Endpoints::MakeRequest docontext 'when request code_type is not Net::HTTPOK' dobefore dohttp_double = instance_double(Net::HTTP)- expect(Net::HTTP).to receive(:new).and_return(http_double)- expect(http_double).to receive(:use_ssl=)- expect(http_double).to(+ allow(Net::HTTP).to receive(:new).and_return(http_double)+ allow(http_double).to(receive(:request).and_return(double(:request).tap do |request|- expect(request).to receive(:code_type).and_return(Net::HTTPClientError)+ allow(request).to receive(:code_type).and_return(Net::HTTPClientError)end))diff --git a/api/spec/services/spree/webhooks/endpoints/queue_request_spec.rb b/api/spec/services/spree/webhooks/endpoints/queue_requests_spec.rbsimilarity index 83%rename from api/spec/services/spree/webhooks/endpoints/queue_request_spec.rbrename to api/spec/services/spree/webhooks/endpoints/queue_requests_spec.rbindex 4113406b03..76b3df84ae 100644--- a/api/spec/services/spree/webhooks/endpoints/queue_request_spec.rb+++ b/api/spec/services/spree/webhooks/endpoints/queue_requests_spec.rb@@ -2,10 +2,10 @@ require 'spec_helper'describe Spree::Webhooks::Endpoints::QueueRequests, :job, :spree_webhooks dodescribe '#call' do- subject { described_class.call(event: event, payload: payload) }+ subject { described_class.call(event: event, body: body) }let(:event) { 'order.finalize' }- let(:payload) { {} }+ let(:body) { {} }let(:queue) { 'spree_webhooks' }let(:make_request_job) { Spree::Webhooks::Endpoints::MakeRequestJob }@@ -31,7 +31,7 @@ describe Spree::Webhooks::Endpoints::QueueRequests, :job, :spree_webhooks doit 'queues a job to make a request' doexpect { subject }.to(- have_enqueued_job(make_request_job).with(payload, endpoint.url).on_queue(queue)+ have_enqueued_job(make_request_job).with(body, endpoint.url).on_queue(queue))endend@@ -49,7 +49,7 @@ describe Spree::Webhooks::Endpoints::QueueRequests, :job, :spree_webhooks doit 'queues a job to make a request' doexpect { subject }.to(- have_enqueued_job(make_request_job).with(payload, endpoint.url).on_queue(queue)+ have_enqueued_job(make_request_job).with(body, endpoint.url).on_queue(queue))endend@@ -65,7 +65,7 @@ describe Spree::Webhooks::Endpoints::QueueRequests, :job, :spree_webhooks doit 'does not queue a job to make a request' doexpect { subject }.not_to(- have_enqueued_job(make_request_job).with(payload, endpoint.url).on_queue(queue)+ have_enqueued_job(make_request_job).with(body, endpoint.url).on_queue(queue))endend@@ -81,7 +81,7 @@ describe Spree::Webhooks::Endpoints::QueueRequests, :job, :spree_webhooks doit 'does not queue a job to make a request' doexpect { subject }.not_to(- have_enqueued_job(make_request_job).with(payload, endpoint.url).on_queue(queue)+ have_enqueued_job(make_request_job).with(body, endpoint.url).on_queue(queue))endenddiff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rbindex 52ee1b9219..9b72f3d58b 100644--- a/core/app/models/spree/order.rb+++ b/core/app/models/spree/order.rb@@ -63,7 +63,7 @@ module Spreeendself.whitelisted_ransackable_associations = %w[shipments user created_by approver canceler promotions bill_address ship_address line_items store]- self.whitelisted_ransackable_attributes = %w[completed_at email number state payment_state shipment_state total item_total considered_risky channel]+ self.whitelisted_ransackable_attributes = %w[completed_at email number state payment_state shipment_state total item_total considered_risky channel]attr_reader :coupon_codeattr_accessor :temporary_address, :temporary_credit_card@@ -728,3 +728,5 @@ module Spreeendendend++ActiveSupport.run_load_hooks(:spree_order, Spree::Order)diff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rbindex c4bc63a08d..61568aa139 100644--- a/core/app/models/spree/payment.rb+++ b/core/app/models/spree/payment.rb@@ -302,3 +302,5 @@ module Spreeendendend++ActiveSupport.run_load_hooks(:spree_payment, Spree::Payment)diff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rbindex 97cd0312f6..a2a763c9fe 100644--- a/core/app/models/spree/shipment.rb+++ b/core/app/models/spree/shipment.rb@@ -401,3 +401,5 @@ module Spreeendendend++ActiveSupport.run_load_hooks(:spree_shipment, Spree::Shipment)