Skip to content

armgit / spree

Public Imported from GitHub
0

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.

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.rb
index 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 Spree
class MakeRequestJob < Spree::BaseJob
queue_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)
end
end
end
diff --git a/api/app/models/concerns/spree/webhooks/has_webhooks.rb b/api/app/models/concerns/spree/webhooks/has_webhooks.rb
index 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 Spree
after_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)
end
end
@@ -31,7 +29,7 @@ module Spree
end
end
- def payload
+ def body
resource_serializer.new(self).serializable_hash.to_json
rescue NameError
''
diff --git a/api/app/services/spree/webhooks/endpoints/make_request.rb b/api/app/services/spree/webhooks/endpoints/make_request.rb
index 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 Spree
request = Net::HTTP::Post.new(uri.path, HEADERS)
request.body = body
http = 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_type
success(code_type == Net::HTTPOK)
end
diff --git a/api/app/services/spree/webhooks/endpoints/queue_requests.rb b/api/app/services/spree/webhooks/endpoints/queue_requests.rb
index 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 Spree
class QueueRequests
prepend 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)
end
end
diff --git a/api/lib/spree/api/engine.rb b/api/lib/spree/api/engine.rb
index e9e5703cc1..6b7a1637b7 100644
--- a/api/lib/spree/api/engine.rb
+++ b/api/lib/spree/api/engine.rb
@@ -25,10 +25,18 @@ module Spree
initializer 'Spree::Webhooks' do
ActiveSupport.on_load(:spree_base) do
include 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::Shipment
end
end
diff --git a/api/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rb b/api/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rb
index 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 do
let(: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')
)
end
diff --git a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
index 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 do
context 'without a Spree::Webhooks descendant class' do
context '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' do
it '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).once
end
end
context 'after_destroy_commit' do
it 'queues a request through QueueRequests for product.destroy' do
product.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).once
end
end
context 'after_update_commit' do
it 'queues a request through QueueRequests for product.update' do
product.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).once
end
end
context 'with a class name with multiple words' do
let!(:cms_page) { create(:cms_homepage, store: store, locale: 'en') }
- let(:payload) do
+ let(:body) do
Spree::Api::V2::Platform::CmsPageSerializer.new(cms_page).serializable_hash.to_json
end
let(: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).once
end
end
end
- context 'without a resource serializer - blank payload' do
+ context 'without a resource serializer - blank body' do
let(:product) { Spree::TestProduct.new(name: 'test') }
context 'after_create_commit' do
diff --git a/api/spec/models/spree/api/webhooks/order_spec.rb b/api/spec/models/spree/api/webhooks/order_spec.rb
index 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 do
describe 'sending webhooks after transitioning from states' do
let(:queue_requests) { instance_double(Spree::Webhooks::Endpoints::QueueRequests) }
let(:store) { create(:store, default: true) }
- let(:payload) do
+ let(:body) do
Spree::Api::V2::Platform::OrderSerializer.new(order).serializable_hash.to_json
end
@@ -19,10 +19,10 @@ describe Spree::Order do
describe '#after_cancel' do
let(: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' do
Timecop.freeze(Time.local(1990)) do
order.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).once
end
end
end
@@ -30,9 +30,9 @@ describe Spree::Order do
describe '#finalize!' do
let(: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!' do
order.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).once
end
end
end
diff --git a/api/spec/models/spree/api/webhooks/payment_spec.rb b/api/spec/models/spree/api/webhooks/payment_spec.rb
index 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 do
let(: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 do
ENV['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 do
after { 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' do
payment.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).once
end
end
diff --git a/api/spec/models/spree/api/webhooks/shipment_spec.rb b/api/spec/models/spree/api/webhooks/shipment_spec.rb
index 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 do
let(: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' do
before do
ENV['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 do
let(: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' do
shipment.cancel # previous state that allows the object be shipped
shipment.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).once
end
end
end
diff --git a/api/spec/services/spree/webhooks/endpoints/make_request_spec.rb b/api/spec/services/spree/webhooks/endpoints/make_request_spec.rb
index 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 do
describe '#call' do
subject { 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 do
context 'when request code_type is not Net::HTTPOK' do
before do
http_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.rb
similarity index 83%
rename from api/spec/services/spree/webhooks/endpoints/queue_request_spec.rb
rename to api/spec/services/spree/webhooks/endpoints/queue_requests_spec.rb
index 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 do
describe '#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 do
it 'queues a job to make a request' do
expect { 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)
)
end
end
@@ -49,7 +49,7 @@ describe Spree::Webhooks::Endpoints::QueueRequests, :job, :spree_webhooks do
it 'queues a job to make a request' do
expect { 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)
)
end
end
@@ -65,7 +65,7 @@ describe Spree::Webhooks::Endpoints::QueueRequests, :job, :spree_webhooks do
it 'does not queue a job to make a request' do
expect { 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)
)
end
end
@@ -81,7 +81,7 @@ describe Spree::Webhooks::Endpoints::QueueRequests, :job, :spree_webhooks do
it 'does not queue a job to make a request' do
expect { 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)
)
end
end
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index 52ee1b9219..9b72f3d58b 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -63,7 +63,7 @@ module Spree
end
self.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_code
attr_accessor :temporary_address, :temporary_credit_card
@@ -728,3 +728,5 @@ module Spree
end
end
end
+
+ActiveSupport.run_load_hooks(:spree_order, Spree::Order)
diff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rb
index c4bc63a08d..61568aa139 100644
--- a/core/app/models/spree/payment.rb
+++ b/core/app/models/spree/payment.rb
@@ -302,3 +302,5 @@ module Spree
end
end
end
+
+ActiveSupport.run_load_hooks(:spree_payment, Spree::Payment)
diff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rb
index 97cd0312f6..a2a763c9fe 100644
--- a/core/app/models/spree/shipment.rb
+++ b/core/app/models/spree/shipment.rb
@@ -401,3 +401,5 @@ module Spree
end
end
end
+
+ActiveSupport.run_load_hooks(:spree_shipment, Spree::Shipment)