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
Fix: Required the refunds permission to pay customers back from returns, claims, exchanges and cancellations
Damian Legawiec1c7e4c044e97
diff --git a/docs/api-reference/admin-api/endpoints.mdx b/docs/api-reference/admin-api/endpoints.mdxindex e6b1ee1625..8bf321dce0 100644--- a/docs/api-reference/admin-api/endpoints.mdx+++ b/docs/api-reference/admin-api/endpoints.mdx@@ -389,7 +389,7 @@ Endpoints marked — are exempt from scope checks (authentication and session en| `PATCH` | `/orders/{id}` | `write_orders` | Update an order || `DELETE` | `/orders/{id}` | `write_orders` | Delete a draft order || `PATCH` | `/orders/{id}/complete` | `write_orders` | Complete an order |-| `PATCH` | `/orders/{id}/cancel` | `write_orders` | Cancel an order |+| `PATCH` | `/orders/{id}/cancel` | `write_orders` (`write_refunds` with `refund_payments`) | Cancel an order || `PATCH` | `/orders/{id}/approve` | `write_orders` | Approve an order || `POST` | `/orders/{id}/resend_confirmation` | `write_orders` | Resend confirmation email || `GET` | `/orders/{order_id}/payments` | `read_payments` | List payments |diff --git a/spree/api/app/controllers/concerns/spree/api/v3/admin/refund_authorization.rb b/spree/api/app/controllers/concerns/spree/api/v3/admin/refund_authorization.rbnew file mode 100644index 0000000000..cfe64eaaba--- /dev/null+++ b/spree/api/app/controllers/concerns/spree/api/v3/admin/refund_authorization.rb@@ -0,0 +1,26 @@+module Spree+ module Api+ module V3+ module Admin+ # Handing money back is the `refunds` key's own authority, as it is on+ # the nested refunds endpoint: a role that settles returns, claims and+ # exchanges, or cancels orders, under `orders` does not thereby decide+ # to refund them. The store back office's gate only — a seller refunds+ # their own orders under the seller panel's `orders` key.+ module RefundAuthorization+ extend ActiveSupport::Concern++ REFUND_PERMISSION = 'write_refunds'.freeze++ private++ def require_refund_permission!+ return if holds_permission?(REFUND_PERMISSION)++ render_missing_permission(REFUND_PERMISSION)+ end+ end+ end+ end+ end+enddiff --git a/spree/api/app/controllers/spree/api/v3/admin/orders/claims_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders/claims_controller.rbindex 24b8d01641..1b3e96aaf2 100644--- a/spree/api/app/controllers/spree/api/v3/admin/orders/claims_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/orders/claims_controller.rb@@ -6,6 +6,7 @@ module Spree# Claims on a completed order.class ClaimsController < BaseControllerinclude Spree::Api::V3::Orders::ClaimActions+ include Spree::Api::V3::Admin::RefundAuthorization# Claims are a subject of the `orders` catalog resource, so# `read_orders`/`write_orders` gate these endpoints. `:claims`@@ -51,6 +52,7 @@ module Spreeendbefore_action :set_resource, only: [:show, :update, :approve, :resolve, :deny, :cancel]+ before_action :require_refund_permission!, only: :resolve, if: -> { params[:resolution].to_s.include?('refund') }# PATCH /api/v3/admin/orders/:order_id/claims/:id#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.rbindex 7b16e3ab13..0a97965f23 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@@ -7,6 +7,7 @@ module Spree# member action, because each carries different arguments.class ExchangesController < BaseControllerinclude Spree::Api::V3::Orders::ExchangeActions+ include Spree::Api::V3::Admin::RefundAuthorization# Exchanges are a subject of the `orders` catalog resource, so# `read_orders`/`write_orders` gate these endpoints. `:exchanges`@@ -49,6 +50,9 @@ module Spreeendbefore_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? }# PATCH /api/v3/admin/orders/:order_id/exchanges/:id#diff --git a/spree/api/app/controllers/spree/api/v3/admin/orders/returns_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders/returns_controller.rbindex b60325c2fb..cf0d6e068d 100644--- a/spree/api/app/controllers/spree/api/v3/admin/orders/returns_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/orders/returns_controller.rb@@ -10,6 +10,7 @@ module Spree# an amount.class ReturnsController < BaseControllerinclude Spree::Api::V3::Orders::ReturnActions+ include Spree::Api::V3::Admin::RefundAuthorization# Returns are a subject of the `orders` catalog resource (see# Spree::PermissionConfiguration), so `read_orders`/`write_orders`@@ -57,6 +58,7 @@ module Spreeendbefore_action :set_resource, only: [:show, :update, :approve, :receive, :refund, :cancel]+ before_action :require_refund_permission!, only: :refund# PATCH /api/v3/admin/orders/:order_id/returns/:id#diff --git a/spree/api/app/controllers/spree/api/v3/admin/orders_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders_controller.rbindex 8d7124b7cf..60e7a2449f 100644--- a/spree/api/app/controllers/spree/api/v3/admin/orders_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/orders_controller.rb@@ -4,6 +4,7 @@ module Spreemodule Adminclass OrdersController < ResourceControllerinclude Spree::Api::V3::OrderLock+ include Spree::Api::V3::Admin::RefundAuthorizationscoped_resource :orders@@ -86,6 +87,7 @@ module Spreeskip_before_action :set_resource, only: [:index, :create]before_action :set_resource, only: [:show, :update, :destroy, :complete, :cancel, :approve, :resend_confirmation, :resend_digital_links, :po_document]+ before_action :require_refund_permission!, only: :cancel, if: -> { params[:refund_payments].to_b }# POST /api/v3/admin/ordersdef creatediff --git a/spree/api/spec/controllers/spree/api/v3/admin/orders/claims_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/orders/claims_controller_spec.rbindex 8b9ef618e3..8491b732b4 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/orders/claims_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/orders/claims_controller_spec.rb@@ -88,6 +88,18 @@ RSpec.describe Spree::Api::V3::Admin::Orders::ClaimsController, type: :controlleexpect(Spree::StoreCredit.find_by(originator: claim)).to be_presentend+ 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 a refund resolution' do+ patch :resolve, params: { order_id: order.prefixed_id, id: claim.prefixed_id, resolution: 'refund', refund_method: 'store_credit' }, as: :json++ expect(response).to have_http_status(:forbidden)+ expect(json_response['error']['details']['required_permission']).to eq('write_refunds')+ end+ end+# A claim opened without per-item amounts has nothing to refund; the# dashboard collects them at creation, and the API says so plainly.it 'refuses a refund when no amount was recorded on the claim' dodiff --git a/spree/api/spec/controllers/spree/api/v3/admin/orders/returns_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/orders/returns_controller_spec.rbindex 0ef8968c11..a1ca49e637 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/orders/returns_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/orders/returns_controller_spec.rb@@ -247,6 +247,25 @@ RSpec.describe Spree::Api::V3::Admin::Orders::ReturnsController, type: :controllexpect(Spree::StoreCredit.find_by(originator: return_record)).to be_presentend+ # Settling a return is `orders` work; paying the customer back is the+ # `refunds` key's, as on the nested refunds endpoint.+ 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 refund' do+ patch :refund, params: {+ order_id: order.prefixed_id,+ id: return_record.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(Spree::StoreCredit.find_by(originator: return_record)).to be_nil+ end+ end+it 'rejects an unknown refund method' dopatch :refund, params: {order_id: order.prefixed_id,diff --git a/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rbindex af71fb18ee..a0b85abbc2 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb@@ -1170,6 +1170,26 @@ RSpec.describe Spree::Api::V3::Admin::OrdersController, type: :controller dolet!(:order) { create(:completed_order_with_totals, store: store) }let(:params) { { id: order.prefixed_id, refund_payments: 'true' } }+ 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 cancel with a refund' do+ subject++ expect(response).to have_http_status(:forbidden)+ expect(json_response['error']['details']['required_permission']).to eq('write_refunds')+ expect(order.reload).not_to be_canceled+ end++ it 'still cancels without one' do+ patch :cancel, params: { id: order.prefixed_id }, as: :json++ expect(response).to have_http_status(:ok)+ expect(order.reload).to be_canceled+ end+ end+it 'asks the workflow to refund what the order has been paid' doexpect(Spree.order_cancel_workflow).to receive(:call).with(hash_including(refund_payments: true, refund_amount: nil)).and_call_originaldiff --git a/spree/api/spec/integration/spree/api/v3/admin/claims_spec.rb b/spree/api/spec/integration/spree/api/v3/admin/claims_spec.rbindex 93c6f40814..2d562de7d1 100644--- a/spree/api/spec/integration/spree/api/v3/admin/claims_spec.rb+++ b/spree/api/spec/integration/spree/api/v3/admin/claims_spec.rb@@ -139,7 +139,7 @@ RSpec.describe 'Admin Claims API', type: :request, swagger_doc: 'api-reference/aproduces 'application/json'security [api_key: [], bearer_auth: []]description 'Settles an approved claim with money back, a replacement shipment, or both.'- admin_scope :write, :orders+ admin_scope_note('`write_orders`, plus `write_refunds` for a resolution that refunds')admin_sdk_example 'order-claims/resolve'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.rbindex d0c97dd3e6..2aaa4562cd 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-referencproduces 'application/json'security [api_key: [], bearer_auth: []]description 'Ships the replacements for a received exchange and settles any price difference.'- admin_scope :write, :orders+ admin_scope_note('`write_orders`, plus `write_refunds` when the customer is owed a difference')admin_sdk_example 'order-exchanges/fulfill'diff --git a/spree/api/spec/integration/spree/api/v3/admin/orders_spec.rb b/spree/api/spec/integration/spree/api/v3/admin/orders_spec.rbindex 1b3a58ce41..4deb72d3aa 100644--- a/spree/api/spec/integration/spree/api/v3/admin/orders_spec.rb+++ b/spree/api/spec/integration/spree/api/v3/admin/orders_spec.rb@@ -269,7 +269,7 @@ RSpec.describe 'Admin Orders API', type: :request, swagger_doc: 'api-reference/aSet `notify_customer: true` to send the order cancellation email.DESC- admin_scope :write, :orders+ admin_scope_note('`write_orders`, plus `write_refunds` with `refund_payments`')admin_sdk_example 'orders/cancel'diff --git a/spree/api/spec/integration/spree/api/v3/admin/returns_spec.rb b/spree/api/spec/integration/spree/api/v3/admin/returns_spec.rbindex b57947e6cc..58372fe191 100644--- a/spree/api/spec/integration/spree/api/v3/admin/returns_spec.rb+++ b/spree/api/spec/integration/spree/api/v3/admin/returns_spec.rb@@ -178,7 +178,7 @@ RSpec.describe 'Admin Returns API', type: :request, swagger_doc: 'api-reference/produces 'application/json'security [api_key: [], bearer_auth: []]description 'Pays the customer back for a received return, to store credit or through the original payment.'- admin_scope :write, :orders+ admin_scope_note('`write_orders` and `write_refunds`')admin_sdk_example 'order-returns/refund'