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.

Fix: Required the refunds permission for every exchange fulfillment

Damian Legawiec40c1c464d214
diff --git a/spree/api/app/controllers/spree/api/v3/admin/orders/exchanges_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders/exchanges_controller.rb
index 0a97965f23..4d0d3ce0dc 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/orders/exchanges_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/orders/exchanges_controller.rb
@@ -50,9 +50,14 @@ module Spree
end
before_action :set_resource, only: [:show, :update, :approve, :receive, :fulfill, :cancel]
- # Fulfilling settles the price difference, which is a refund when
- # the replacements cost less than what came back.
- before_action :require_refund_permission!, only: :fulfill, if: -> { @resource.price_difference.negative? }
+ # Fulfilling settles the difference on the units that actually came
+ # back, a credit to the customer when the replacements cost less.
+ # That figure is only known once the tax rows are rewritten for
+ # those units, inside the fulfillment itself, so the gate cannot be
+ # narrowed on the announced difference: a line received at zero
+ # units would offset a credit still paid on another. Every
+ # fulfillment settles money, so all of them take the refund authority.
+ before_action :require_refund_permission!, only: :fulfill
# PATCH /api/v3/admin/orders/:order_id/exchanges/:id
#
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/orders/exchanges_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/orders/exchanges_controller_spec.rb
index 1c3413eb37..f36cb0df07 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/orders/exchanges_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/orders/exchanges_controller_spec.rb
@@ -96,6 +96,25 @@ RSpec.describe Spree::Api::V3::Admin::Orders::ExchangesController, type: :contro
end
end
+ describe 'PATCH #fulfill' do
+ let(:exchange) { create(:received_exchange, store: store, order: order) }
+
+ # Fulfilling settles money on the units that came back, so it takes the
+ # refund authority whatever the announced difference says.
+ context 'with a key that may handle orders but not refund them' do
+ let(:secret_api_key) { create(:api_key, :secret, store: store, scopes: %w[write_orders]) }
+ let(:headers) { api_key_headers }
+
+ it 'refuses to fulfill' do
+ patch :fulfill, params: { order_id: order.prefixed_id, id: exchange.prefixed_id, refund_method: 'store_credit' }, as: :json
+
+ expect(response).to have_http_status(:forbidden)
+ expect(json_response['error']['details']['required_permission']).to eq('write_refunds')
+ expect(exchange.reload).not_to be_fulfilled
+ end
+ end
+ end
+
describe 'PATCH #cancel' do
it 'refuses an exchange already received' do
exchange = create(:received_exchange, store: store, order: order)
diff --git a/spree/api/spec/integration/spree/api/v3/admin/exchanges_spec.rb b/spree/api/spec/integration/spree/api/v3/admin/exchanges_spec.rb
index 2aaa4562cd..0ad19f4d02 100644
--- a/spree/api/spec/integration/spree/api/v3/admin/exchanges_spec.rb
+++ b/spree/api/spec/integration/spree/api/v3/admin/exchanges_spec.rb
@@ -174,7 +174,7 @@ RSpec.describe 'Admin Exchanges API', type: :request, swagger_doc: 'api-referenc
produces 'application/json'
security [api_key: [], bearer_auth: []]
description 'Ships the replacements for a received exchange and settles any price difference.'
- admin_scope_note('`write_orders`, plus `write_refunds` when the customer is owed a difference')
+ admin_scope_note('`write_orders` and `write_refunds`')
admin_sdk_example 'order-exchanges/fulfill'