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.

Add store resolution to webhook payments controller (#13910)

Damian Legawiec5963e3a157de
diff --git a/spree/api/app/controllers/spree/api/v3/webhooks/payments_controller.rb b/spree/api/app/controllers/spree/api/v3/webhooks/payments_controller.rb
index de2925b6b0..4f8ab52d0f 100644
--- a/spree/api/app/controllers/spree/api/v3/webhooks/payments_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/webhooks/payments_controller.rb
@@ -4,6 +4,7 @@ module Spree
module Webhooks
class PaymentsController < ActionController::API
include ActionController::RateLimiting
+ include Spree::Core::ControllerHelpers::Store
RATE_LIMIT_RESPONSE = -> {
[429, { 'Content-Type' => 'application/json', 'Retry-After' => '60' },
@@ -20,7 +21,7 @@ module Spree
# Verifies the webhook signature synchronously (returns 401 if invalid),
# then enqueues async processing and returns 200 immediately.
def create
- payment_method = Spree::PaymentMethod.find_by_prefix_id!(params[:payment_method_id])
+ payment_method = current_store.payment_methods.find_by_prefix_id!(params[:payment_method_id])
# Signature verification must be synchronous — invalid = 401
result = payment_method.parse_webhook_event(request.raw_post, request.headers)
diff --git a/spree/api/spec/controllers/spree/api/v3/webhooks/payments_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/webhooks/payments_controller_spec.rb
index 16f23436d4..a3460a92ee 100644
--- a/spree/api/spec/controllers/spree/api/v3/webhooks/payments_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/webhooks/payments_controller_spec.rb
@@ -7,6 +7,19 @@ RSpec.describe Spree::Api::V3::Webhooks::PaymentsController, type: :controller d
let(:payment_method) { create(:bogus_payment_method, stores: [store]) }
describe 'POST #create' do
+ context 'when resolving the current store' do
+ before do
+ allow_any_instance_of(Spree::PaymentMethod).to receive(:parse_webhook_event).and_return(nil)
+ request.host = store.url
+ end
+
+ it 'sets current_store from the request' do
+ post :create, params: { payment_method_id: payment_method.prefixed_id }
+
+ expect(controller.current_store).to eq(store)
+ end
+ end
+
context 'when webhook event is unsupported' do
before do
allow_any_instance_of(Spree::PaymentMethod).to receive(:parse_webhook_event).and_return(nil)
@@ -40,6 +53,17 @@ RSpec.describe Spree::Api::V3::Webhooks::PaymentsController, type: :controller d
end
end
+ context 'when payment method belongs to a different store' do
+ let(:other_store) { create(:store) }
+ let(:other_payment_method) { create(:bogus_payment_method, stores: [other_store]) }
+
+ it 'returns not found' do
+ post :create, params: { payment_method_id: other_payment_method.prefixed_id }
+
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
context 'when an unexpected error occurs' do
before do
allow_any_instance_of(Spree::PaymentMethod).to receive(:parse_webhook_event)