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.

Introducing new `Checkout::Update` (#13769)

Damian Legawiec8ef4f5f398f6
diff --git a/spree/api/app/controllers/spree/api/v3/store/cart_controller.rb b/spree/api/app/controllers/spree/api/v3/store/cart_controller.rb
index 99767bc9ec..d8e3038940 100644
--- a/spree/api/app/controllers/spree/api/v3/store/cart_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/cart_controller.rb
@@ -4,6 +4,7 @@ module Spree
module Store
class CartController < Store::BaseController
include Spree::Api::V3::CartResolvable
+ include Spree::Api::V3::OrderLock
before_action :require_authentication!, only: [:associate]
@@ -33,9 +34,17 @@ module Spree
# Returns 404 if no cart exists - use POST /cart to create one
# Authorize via cart_token param or JWT Bearer token
# Uses find_cart (without bang) intentionally — read-only access, no authorize! needed
+ #
+ # Auto-advances the checkout state machine so that shipments and
+ # payment requirements are up-to-date (temporary until Spree 6 removes
+ # the state machine).
def show
@cart = find_cart
+ if @cart && @cart.ship_address_id.present? && @cart.shipments.empty?
+ with_order_lock { Spree::Checkout::Advance.call(order: @cart) }
+ end
+
render_cart
end
diff --git a/spree/api/app/controllers/spree/api/v3/store/checkout/shipments_controller.rb b/spree/api/app/controllers/spree/api/v3/store/checkout/shipments_controller.rb
index 2e4ddd9068..22c0273f16 100644
--- a/spree/api/app/controllers/spree/api/v3/store/checkout/shipments_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/checkout/shipments_controller.rb
@@ -30,6 +30,10 @@ module Spree
shipment.save!
end
+ # Auto-advance (e.g. delivery → payment) after rate selection.
+ # Temporary — Spree 6 removes the checkout state machine.
+ try_advance
+
render_cart
end
end
@@ -39,6 +43,20 @@ module Spree
def permitted_params
params.permit(:selected_shipping_rate_id)
end
+
+ # Temporary — Spree 6 removes the checkout state machine.
+ def try_advance
+ return if @cart.complete? || @cart.canceled?
+
+ loop do
+ break unless @cart.next
+ break if @cart.confirm? || @cart.complete?
+ end
+ rescue StandardError => e
+ Rails.error.report(e, context: { order_id: @cart.id, state: @cart.state }, source: 'spree.checkout')
+ ensure
+ @cart.reload
+ end
end
end
end
diff --git a/spree/api/app/controllers/spree/api/v3/store/checkout_controller.rb b/spree/api/app/controllers/spree/api/v3/store/checkout_controller.rb
index 35beeb278c..fce088bb03 100644
--- a/spree/api/app/controllers/spree/api/v3/store/checkout_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/checkout_controller.rb
@@ -10,10 +10,10 @@ module Spree
# PATCH /api/v3/store/checkout
# Update checkout info (email, addresses, special instructions).
- # Does NOT auto-advance — the frontend controls navigation between steps.
+ # Auto-advances to the next checkout step when possible.
def update
with_order_lock do
- result = Spree.order_update_service.call(
+ result = Spree.checkout_update_service.call(
order: @cart,
params: checkout_params
)
@@ -46,6 +46,10 @@ module Spree
params.permit(
:email,
:special_instructions,
+ :currency,
+ :locale,
+ :ship_address_id,
+ :bill_address_id,
ship_address: address_params,
bill_address: address_params,
metadata: {}
diff --git a/spree/api/lib/spree/api/dependencies.rb b/spree/api/lib/spree/api/dependencies.rb
index 71f37693d9..b6983811e4 100644
--- a/spree/api/lib/spree/api/dependencies.rb
+++ b/spree/api/lib/spree/api/dependencies.rb
@@ -26,7 +26,7 @@ module Spree
# checkout services
storefront_checkout_next_service: -> { Spree::Dependencies.checkout_next_service },
storefront_checkout_advance_service: -> { Spree::Dependencies.checkout_advance_service },
- storefront_checkout_update_service: -> { Spree::Dependencies.checkout_update_service },
+ storefront_checkout_update_service: 'Spree::Checkout::V2Update',
storefront_checkout_complete_service: -> { Spree::Dependencies.checkout_complete_service },
storefront_checkout_add_store_credit_service: -> { Spree::Dependencies.checkout_add_store_credit_service },
storefront_checkout_remove_store_credit_service: -> { Spree::Dependencies.checkout_remove_store_credit_service },
@@ -288,7 +288,7 @@ module Spree
# order services
platform_order_recalculate_service: -> { Spree::Dependencies.cart_recalculate_service },
- platform_order_update_service: -> { Spree::Dependencies.checkout_update_service },
+ platform_order_update_service: 'Spree::Checkout::V2Update',
platform_order_empty_service: -> { Spree::Dependencies.cart_empty_service },
platform_order_destroy_service: -> { Spree::Dependencies.cart_destroy_service },
platform_order_next_service: -> { Spree::Dependencies.checkout_next_service },
diff --git a/spree/api/spec/controllers/spree/api/v3/store/cart_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/cart_controller_spec.rb
index b610b03a04..77b13004d0 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/cart_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/cart_controller_spec.rb
@@ -306,6 +306,45 @@ RSpec.describe Spree::Api::V3::Store::CartController, type: :controller do
)
end
end
+
+ context 'auto-advance' do
+ let(:user) { create(:user_with_addresses) }
+ let(:cart) { create(:order_with_line_items, store: store, user: user) }
+ let(:country) { Spree::Country.find_by(iso: 'US') || create(:country, iso: 'US') }
+ let!(:us_state) { country.states.find_by(abbr: 'NY') || create(:state, country: country, abbr: 'NY', name: 'New York') }
+ let!(:zone) { create(:zone, zone_members: [Spree::ZoneMember.new(zoneable: country)]) }
+ let!(:shipping_method) { create(:shipping_method, zones: [zone]) }
+
+ before do
+ request.headers['Authorization'] = "Bearer #{jwt_token}"
+ end
+
+ it 'generates shipments when address is present but shipments are empty' do
+ address = create(:address, user: user, country: country, state: us_state)
+ cart.update!(email: '[email protected]', ship_address: address)
+ cart.shipments.delete_all
+ cart.update_column(:state, 'address')
+ cart.reload
+
+ expect(cart.shipments).to be_empty
+
+ get :show
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['shipments']).to be_present
+ end
+
+ it 'does not advance when no address is set' do
+ cart.update!(ship_address: nil)
+ cart.shipments.delete_all
+ cart.reload
+
+ get :show
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['shipments']).to be_empty
+ end
+ end
end
describe 'PATCH #associate' do
diff --git a/spree/api/spec/controllers/spree/api/v3/store/checkout/shipments_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/checkout/shipments_controller_spec.rb
index 158467458f..f07691a054 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/checkout/shipments_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/checkout/shipments_controller_spec.rb
@@ -84,6 +84,36 @@ RSpec.describe Spree::Api::V3::Store::Checkout::ShipmentsController, type: :cont
end
end
+ context 'auto-advance after rate selection' do
+ it 'advances order from delivery to payment' do
+ rate = shipment.shipping_rates.first
+
+ expect(order.state).to eq('delivery')
+
+ patch :update, params: {
+ id: shipment.to_param,
+ selected_shipping_rate_id: rate.to_param
+ }
+
+ expect(response).to have_http_status(:ok)
+ expect(order.reload.state).to eq('payment')
+ expect(json_response['current_step']).to eq('payment')
+ end
+
+ it 'does not fail if advancement is not possible' do
+ # Put order in a state where next won't advance (e.g. address)
+ order.update_column(:state, 'address')
+ rate = shipment.shipping_rates.first
+
+ patch :update, params: {
+ id: shipment.to_param,
+ selected_shipping_rate_id: rate.to_param
+ }
+
+ expect(response).to have_http_status(:ok)
+ end
+ end
+
context 'error handling' do
it 'returns not found for non-existent shipping rate' do
patch :update, params: {
diff --git a/spree/api/spec/controllers/spree/api/v3/store/checkout_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/checkout_controller_spec.rb
new file mode 100644
index 0000000000..a990f7d0e9
--- /dev/null
+++ b/spree/api/spec/controllers/spree/api/v3/store/checkout_controller_spec.rb
@@ -0,0 +1,50 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Store::CheckoutController, type: :controller do
+ render_views
+
+ include_context 'API v3 Store'
+
+ let(:user) { create(:user_with_addresses) }
+ let!(:order) { create(:order_with_line_items, store: store, user: user) }
+ let(:country) { Spree::Country.find_by(iso: 'US') || create(:country, iso: 'US') }
+ let!(:us_state) { country.states.find_by(abbr: 'NY') || create(:state, country: country, abbr: 'NY', name: 'New York') }
+ let!(:zone) { create(:zone, zone_members: [Spree::ZoneMember.new(zoneable: country)]) }
+ let!(:shipping_method) { create(:shipping_method, zones: [zone]) }
+
+ before do
+ request.headers['X-Spree-Api-Key'] = api_key.token
+ request.headers['Authorization'] = "Bearer #{jwt_token}"
+ end
+
+ describe 'PATCH #update' do
+ it 'accepts ship_address_id to use an existing address' do
+ order.update!(email: '[email protected]')
+ existing_address = user.addresses.first || create(:address, user: user, country: country, state: us_state)
+
+ patch :update, params: { ship_address_id: existing_address.prefixed_id }
+
+ expect(response).to have_http_status(:ok)
+ expect(order.reload.ship_address_id).to eq(existing_address.id)
+ end
+
+ it 'auto-advances to payment after address submission' do
+ order.update!(email: '[email protected]')
+ order.next # cart -> address
+ order.reload
+ expect(order.state).to eq('address')
+
+ patch :update, params: {
+ ship_address: {
+ firstname: 'John', lastname: 'Doe',
+ address1: '123 Main St', city: 'New York',
+ zipcode: '10001', country_iso: 'US', state_abbr: 'NY',
+ phone: '555-1234'
+ }
+ }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['current_step']).to eq('payment')
+ end
+ end
+end
diff --git a/spree/api/spec/integration/spree/api/v3/store/checkout_spec.rb b/spree/api/spec/integration/spree/api/v3/store/checkout_spec.rb
index 70f496a10e..14bcd23f33 100644
--- a/spree/api/spec/integration/spree/api/v3/store/checkout_spec.rb
+++ b/spree/api/spec/integration/spree/api/v3/store/checkout_spec.rb
@@ -44,6 +44,8 @@ RSpec.describe 'Checkout API', type: :request, swagger_doc: 'api-reference/store
email: { type: :string, format: 'email', example: '[email protected]' },
special_instructions: { type: :string, example: 'Leave at door' },
metadata: { type: :object, additionalProperties: true },
+ ship_address_id: { type: :string, description: 'Existing address ID to use as shipping address', example: 'addr_abc123' },
+ bill_address_id: { type: :string, description: 'Existing address ID to use as billing address', example: 'addr_def456' },
bill_address: {
type: :object,
properties: {
diff --git a/spree/core/app/services/spree/checkout/update.rb b/spree/core/app/services/spree/checkout/update.rb
index 93f5eff155..d8ecb90631 100644
--- a/spree/core/app/services/spree/checkout/update.rb
+++ b/spree/core/app/services/spree/checkout/update.rb
@@ -2,70 +2,110 @@ module Spree
module Checkout
class Update
prepend Spree::ServiceModule::Base
- include Spree::Addresses::Helper
-
- def call(order:, params:, permitted_attributes:, request_env:)
- # Validate address ownership to prevent IDOR attacks
- address_ownership_error = validate_address_ownership(order, params)
- return failure(order, address_ownership_error) if address_ownership_error
-
- ship_changed = address_with_country_iso_present?(params, 'ship')
- bill_changed = address_with_country_iso_present?(params, 'bill')
- params[:order][:ship_address_attributes] = replace_country_iso_with_id(params[:order][:ship_address_attributes]) if ship_changed
- params[:order][:bill_address_attributes] = replace_country_iso_with_id(params[:order][:bill_address_attributes]) if bill_changed
-
- # for quick checkouts we cannot revert to previous states
- # we already have the address and delivery steps completed
- # however we need to update the shipping address with missing data
- # as previously we didn't have access to first/last name and street
- unless params[:do_not_change_state]
- order.state = 'address' if (ship_changed || bill_changed || quick_checkout_cancelled?(params)) && order.has_checkout_step?('address')
- order.state = 'delivery' if selected_shipping_rate_present?(params) && order.has_checkout_step?('delivery')
+
+ def call(order:, params:)
+ @order = order
+ @params = params.to_h.deep_symbolize_keys
+
+ ApplicationRecord.transaction do
+ assign_order_attributes
+ assign_address(:ship_address)
+ assign_address(:bill_address)
+
+ order.save!
+
+ process_line_items
end
- return success(order) if order.update_from_params(params, permitted_attributes, request_env)
+ try_advance
- failure(order)
+ success(order)
+ rescue ActiveRecord::RecordNotFound
+ raise
+ rescue ActiveRecord::RecordInvalid => e
+ failure(order, e.record.errors.full_messages.to_sentence)
+ rescue StandardError => e
+ failure(order, e.message)
end
private
- def validate_address_ownership(order, params)
- return nil unless params[:order]
+ attr_reader :order, :params
+
+ def assign_order_attributes
+ order.email = params[:email] if params[:email].present?
+ order.special_instructions = params[:special_instructions] if params.key?(:special_instructions)
+ order.currency = params[:currency].upcase if params[:currency].present?
+ order.locale = params[:locale] if params[:locale].present?
+ order.metadata = order.metadata.merge(params[:metadata].to_h) if params[:metadata].present?
+ end
- %w[bill ship].each do |address_kind|
- address_id = params[:order].dig("#{address_kind}_address_attributes".to_sym, :id)
- next unless address_id
+ def assign_address(address_type)
+ address_id_param = params[:"#{address_type}_id"]
+ address_params = params[address_type]
- address = Spree::Address.find_by(id: address_id)
- next unless address
+ if address_id_param.present?
+ address_id = resolve_address_id(address_id_param)
+ order.public_send(:"#{address_type}_id=", address_id) if address_id
+ return
+ end
- # Allow if address has no user (guest address) or belongs to the order's user
- next if address.user_id.nil?
- next if order.user_id.present? && address.user_id == order.user_id
+ return unless address_params.is_a?(Hash)
- return Spree.t(:address_not_owned_by_user)
+ if address_params[:id].present?
+ address_id = resolve_address_id(address_params[:id])
+ order.public_send(:"#{address_type}_id=", address_id) if address_id
+ else
+ revert_to_address_state if order.has_checkout_step?('address')
+ order.public_send(:"#{address_type}_attributes=", address_params)
end
+ end
+
+ def process_line_items
+ return unless params[:line_items].is_a?(Array)
- nil
+ result = Spree.cart_upsert_items_service.call(
+ order: order,
+ line_items: params[:line_items]
+ )
+
+ raise StandardError, result.error.to_s if result.failure?
end
- def address_with_country_iso_present?(params, address_kind = 'ship')
- return false unless params.dig(:order, "#{address_kind}_address_attributes".to_sym, :country_iso)
- return false if params.dig(:order, "#{address_kind}_address_attributes".to_sym, :country_id)
+ def resolve_address_id(prefixed_id)
+ return unless order.user
- true
+ decoded = Spree::Address.decode_prefixed_id(prefixed_id)
+ decoded ? order.user.addresses.find_by(id: decoded)&.id : nil
end
- def selected_shipping_rate_present?(params)
- shipments_attributes = params.dig(:order, :shipments_attributes)
- return false unless shipments_attributes
+ def revert_to_address_state
+ return if ['cart', 'address'].include?(order.state)
- shipments_attributes.any? { |s| s.dig(:selected_shipping_rate_id) }
+ order.state = 'address'
end
- def quick_checkout_cancelled?(params)
- params.dig(:order, :ship_address_id) == 'CLEAR'
+ # Auto-advance as far as the checkout state machine allows.
+ # Loops order.next until the order can't progress further (e.g. missing
+ # payment) or reaches confirm/complete. Stops at the first step whose
+ # before_transition guard fails — the `requirements` array in the
+ # serialized response tells the frontend what's still missing.
+ # Failure is swallowed — the update itself already succeeded.
+ def try_advance
+ return if order.complete? || order.canceled?
+
+ loop do
+ break unless order.next
+ break if order.confirm? || order.complete?
+ end
+ rescue StandardError => e
+ Rails.error.report(e, context: { order_id: order.id, state: order.state }, source: 'spree.checkout')
+ ensure
+ begin
+ order.reload
+ rescue StandardError # rubocop:disable Lint/SuppressedException
+ # reload failure must not mask the original result
+ end
end
end
end
diff --git a/spree/core/app/services/spree/checkout/v2_update.rb b/spree/core/app/services/spree/checkout/v2_update.rb
new file mode 100644
index 0000000000..7833f89421
--- /dev/null
+++ b/spree/core/app/services/spree/checkout/v2_update.rb
@@ -0,0 +1,72 @@
+module Spree
+ module Checkout
+ class V2Update
+ prepend Spree::ServiceModule::Base
+ include Spree::Addresses::Helper
+
+ def call(order:, params:, permitted_attributes:, request_env:)
+ # Validate address ownership to prevent IDOR attacks
+ address_ownership_error = validate_address_ownership(order, params)
+ return failure(order, address_ownership_error) if address_ownership_error
+
+ ship_changed = address_with_country_iso_present?(params, 'ship')
+ bill_changed = address_with_country_iso_present?(params, 'bill')
+ params[:order][:ship_address_attributes] = replace_country_iso_with_id(params[:order][:ship_address_attributes]) if ship_changed
+ params[:order][:bill_address_attributes] = replace_country_iso_with_id(params[:order][:bill_address_attributes]) if bill_changed
+
+ # for quick checkouts we cannot revert to previous states
+ # we already have the address and delivery steps completed
+ # however we need to update the shipping address with missing data
+ # as previously we didn't have access to first/last name and street
+ unless params[:do_not_change_state]
+ order.state = 'address' if (ship_changed || bill_changed || quick_checkout_cancelled?(params)) && order.has_checkout_step?('address')
+ order.state = 'delivery' if selected_shipping_rate_present?(params) && order.has_checkout_step?('delivery')
+ end
+
+ return success(order) if order.update_from_params(params, permitted_attributes, request_env)
+
+ failure(order)
+ end
+
+ private
+
+ def validate_address_ownership(order, params)
+ return nil unless params[:order]
+
+ %w[bill ship].each do |address_kind|
+ address_id = params[:order].dig("#{address_kind}_address_attributes".to_sym, :id)
+ next unless address_id
+
+ address = Spree::Address.find_by(id: address_id)
+ next unless address
+
+ # Allow if address has no user (guest address) or belongs to the order's user
+ next if address.user_id.nil?
+ next if order.user_id.present? && address.user_id == order.user_id
+
+ return Spree.t(:address_not_owned_by_user)
+ end
+
+ nil
+ end
+
+ def address_with_country_iso_present?(params, address_kind = 'ship')
+ return false unless params.dig(:order, "#{address_kind}_address_attributes".to_sym, :country_iso)
+ return false if params.dig(:order, "#{address_kind}_address_attributes".to_sym, :country_id)
+
+ true
+ end
+
+ def selected_shipping_rate_present?(params)
+ shipments_attributes = params.dig(:order, :shipments_attributes)
+ return false unless shipments_attributes
+
+ shipments_attributes.any? { |s| s.dig(:selected_shipping_rate_id) }
+ end
+
+ def quick_checkout_cancelled?(params)
+ params.dig(:order, :ship_address_id) == 'CLEAR'
+ end
+ end
+ end
+end
diff --git a/spree/core/app/services/spree/orders/update.rb b/spree/core/app/services/spree/orders/update.rb
deleted file mode 100644
index ab05531c07..0000000000
--- a/spree/core/app/services/spree/orders/update.rb
+++ /dev/null
@@ -1,121 +0,0 @@
-module Spree
- module Orders
- # Core order update service with modern conventions:
- # - Flat parameter structure (no wrapping in "order" key)
- # - snake_case field names without "_attributes" suffix
- # - Automatic state management based on what's being updated
- # - Support for line_items with prefixed variant IDs
- #
- # @example Update order with line items
- # Spree::Orders::Update.call(
- # order: order,
- # params: {
- # email: "[email protected]",
- # line_items: [
- # { variant_id: "variant_123", quantity: 2 },
- # { variant_id: "variant_456", quantity: 1 }
- # ],
- # ship_address: {
- # firstname: "John",
- # lastname: "Doe",
- # address1: "123 Main St",
- # city: "New York",
- # zipcode: "10001",
- # country_iso: "US",
- # state_abbr: "NY"
- # }
- # }
- # )
- #
- class Update
- prepend Spree::ServiceModule::Base
-
- def call(order:, params:)
- @order = order
- @params = params.to_h.deep_symbolize_keys
-
- ApplicationRecord.transaction do
- assign_order_attributes
- assign_address(:ship_address)
- assign_address(:bill_address)
-
- order.save!
-
- process_line_items
- end
-
- success(order.reload)
- rescue ActiveRecord::RecordNotFound
- raise
- rescue ActiveRecord::RecordInvalid => e
- failure(order, e.record.errors.full_messages.to_sentence)
- rescue StandardError => e
- failure(order, e.message)
- end
-
- private
-
- attr_reader :order, :params
-
- def assign_order_attributes
- order.email = params[:email] if params[:email].present?
- order.special_instructions = params[:special_instructions] if params.key?(:special_instructions)
- order.currency = params[:currency].upcase if params[:currency].present?
- order.locale = params[:locale] if params[:locale].present?
- order.metadata = order.metadata.merge(params[:metadata].to_h) if params[:metadata].present?
- end
-
- def assign_address(address_type)
- address_id_param = params[:"#{address_type}_id"]
- address_params = params[address_type]
-
- # Priority 1: Direct address ID reference (ship_address_id / bill_address_id)
- if address_id_param.present?
- address_id = resolve_address_id(address_id_param)
- order.public_send(:"#{address_type}_id=", address_id) if address_id
- return
- end
-
- # Priority 2: Nested address params (ship_address / bill_address)
- return unless address_params.is_a?(Hash)
-
- if address_params[:id].present?
- # Using existing address by ID within nested params
- address_id = resolve_address_id(address_params[:id])
- order.public_send(:"#{address_type}_id=", address_id) if address_id
- else
- # Creating/updating address with provided attributes
- revert_to_address_state if order.has_checkout_step?('address')
- order.public_send(:"#{address_type}_attributes=", address_params)
- end
- end
-
- def process_line_items
- return unless params[:line_items].is_a?(Array)
-
- result = Spree.cart_upsert_items_service.call(
- order: order,
- line_items: params[:line_items]
- )
-
- raise StandardError, result.error.to_s if result.failure?
- end
-
- # Translate prefixed ID to internal id
- def resolve_address_id(prefixed_id)
- return unless order.user
-
- decoded = Spree::Address.decode_prefixed_id(prefixed_id)
- decoded ? order.user.addresses.find_by(id: decoded)&.id : nil
- end
-
- # Revert order state to 'address' when address changes
- # This ensures shipments are recreated when transitioning back to delivery
- def revert_to_address_state
- return if ['cart', 'address'].include?(order.state)
-
- order.state = 'address'
- end
- end
- end
-end
diff --git a/spree/core/lib/spree/core/dependencies.rb b/spree/core/lib/spree/core/dependencies.rb
index fcc0df72c0..383879de5b 100644
--- a/spree/core/lib/spree/core/dependencies.rb
+++ b/spree/core/lib/spree/core/dependencies.rb
@@ -42,7 +42,7 @@ module Spree
# order
order_approve_service: 'Spree::Orders::Approve',
order_cancel_service: 'Spree::Orders::Cancel',
- order_update_service: 'Spree::Orders::Update',
+ order_update_service: 'Spree::Checkout::Update',
order_updater: 'Spree::OrderUpdater',
# shipment
diff --git a/spree/core/spec/services/spree/checkout/update_spec.rb b/spree/core/spec/services/spree/checkout/update_spec.rb
index 52be391475..95a3883856 100644
--- a/spree/core/spec/services/spree/checkout/update_spec.rb
+++ b/spree/core/spec/services/spree/checkout/update_spec.rb
@@ -1,347 +1,354 @@
require 'spec_helper'
-describe Spree::Checkout::Update, type: :service do
- describe '#transform_address_params' do
- let!(:state) { create(:state) }
- let!(:country) { state.country }
- let!(:replace_country) { described_class.new }
- let!(:address) do
- {
- firstname: 'John',
- lastname: 'Doe',
- address1: '7735 Old Georgetown Road',
- city: 'Bethesda',
- phone: '3014445002',
- zipcode: '20814',
- state_id: state.id,
- country_iso: country.iso
- }
- end
+module Spree
+ RSpec.describe Checkout::Update do
+ let(:store) { create(:store, supported_currencies: 'USD,EUR,GBP') }
+ let(:user) { create(:user) }
+ let(:order) { create(:order_with_line_items, user: user, store: store, currency: 'USD') }
- context 'with ship_address order params' do
- let(:order_params) do
- ActionController::Parameters.new(
- order: {
- ship_address_attributes: address
- }
- )
- end
- let(:result) { replace_country.send(:replace_country_iso_with_id, order_params[:order][:ship_address_attributes]) }
+ describe '#call' do
+ subject { described_class.call(order: order, params: params) }
- before { result }
+ describe 'updating email' do
+ let(:params) { { email: '[email protected]' } }
- it 'will return hash contain country_id' do
- expect(result[:country_id]).to eq country.id
+ it 'updates the order email' do
+ expect(subject).to be_success
+ expect(order.reload.email).to eq('[email protected]')
+ end
end
- it 'will return hash without country_iso' do
- expect(result).not_to include(:country_iso)
- end
- end
+ describe 'updating special_instructions' do
+ let(:params) { { special_instructions: 'Leave at the door' } }
- context 'with bill_address order params' do
- let(:order_params) do
- ActionController::Parameters.new(
- order: {
- bill_address_attributes: address
- }
- )
- end
- let(:result) { replace_country.send(:replace_country_iso_with_id, order_params[:order][:bill_address_attributes]) }
+ it 'updates the special instructions' do
+ expect(subject).to be_success
+ expect(order.reload.special_instructions).to eq('Leave at the door')
+ end
- before { result }
+ context 'when clearing special_instructions' do
+ let(:order) { create(:order_with_line_items, user: user, store: store, special_instructions: 'Existing instructions') }
- it 'will return hash contain country_id' do
- expect(result[:country_id]).to eq country.id
- end
+ it 'clears with empty string' do
+ result = described_class.call(order: order, params: { special_instructions: '' })
+ expect(result).to be_success
+ expect(order.reload.special_instructions).to eq('')
+ end
- it 'will return hash without country_iso' do
- expect(result).not_to include(:country_iso)
+ it 'clears with nil' do
+ result = described_class.call(order: order, params: { special_instructions: nil })
+ expect(result).to be_success
+ expect(order.reload.special_instructions).to be_nil
+ end
+ end
end
- end
- end
-
- describe 'update address' do
- let(:user) { create(:user_with_addresses) }
- let(:order) { create(:order_with_line_items, user: user, bill_address: user.bill_address, ship_address: user.ship_address, state: order_state) }
- let(:state) { create(:state) }
- let(:country) { state.country }
- let(:update_service) { described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil) }
- let(:order_params) { ActionController::Parameters.new(order: {
- ship_address_id: ship_address_id,
- bill_address_id: bill_address_id,
- ship_address_attributes: address_attributes
- }) }
- let(:permitted_attributes) do
- Spree::PermittedAttributes.checkout_attributes + [
- bill_address_attributes: Spree::PermittedAttributes.address_attributes,
- ship_address_attributes: Spree::PermittedAttributes.address_attributes
- ]
- end
- context 'at cart state' do
- let(:order_state) { 'cart' }
- let(:ship_address_id) { nil }
- let(:bill_address_id) { nil }
- let(:address_attributes) do
- {
- firstname: 'John',
- lastname: 'Doe',
- address1: '7735 Old Georgetown Road',
- city: 'Bethesda',
- phone: '3014445002',
- zipcode: '20814',
- state_id: state.id,
- country_iso: country.iso
- }
- end
+ describe 'updating currency' do
+ context 'with supported currency' do
+ let(:params) { { currency: 'EUR' } }
- it 'should set order back to address state' do
- expect(order.state).not_to eq 'address'
- expect(order.ship_address.state.id).not_to eq state.id
+ it 'updates the currency' do
+ expect(subject).to be_success
+ expect(order.reload.currency).to eq('EUR')
+ end
- update_service
+ it 'is case-insensitive' do
+ result = described_class.call(order: order, params: { currency: 'eur' })
+ expect(result).to be_success
+ expect(order.reload.currency).to eq('EUR')
+ end
+ end
- expect(order.state).to eq 'address'
- expect(order.ship_address.state.id).to eq state.id
- end
+ context 'with unsupported currency' do
+ let(:params) { { currency: 'JPY' } }
- it 'should not set order back to address state if do_not_change_state is true' do
- expect(order.state).not_to eq 'address'
- order_params[:do_not_change_state] = true
- update_service
+ it 'returns failure' do
+ expect(subject).to be_failure
+ end
- expect(order.state).not_to eq 'address'
+ it 'does not change the currency' do
+ subject
+ expect(order.reload.currency).to eq('USD')
+ end
+ end
end
- it 'should set order back to address state if quick checkout cancelled' do
- expect(order.state).not_to eq 'address'
- order_params[:order][:ship_address_id] = 'CLEAR'
- update_service
+ describe 'updating addresses' do
+ let(:country) { Spree::Country.find_by(iso: 'US') || create(:country, iso: 'US') }
+ let!(:state) { country.states.find_by(abbr: 'NY') || create(:state, country: country, abbr: 'NY', name: 'New York') }
+
+ shared_examples 'address update' do |address_type|
+ let(:address_key) { address_type }
+ let(:address_id_key) { :"#{address_type}_id" }
+
+ context 'with new address attributes' do
+ let(:params) do
+ {
+ address_key => {
+ firstname: 'John',
+ lastname: 'Doe',
+ address1: '123 Main St',
+ city: 'New York',
+ zipcode: '10001',
+ country_iso: 'US',
+ state_abbr: 'NY',
+ phone: '555-1234'
+ }
+ }
+ end
+
+ it 'creates a new address' do
+ expect(subject).to be_success
+ address = order.reload.public_send(address_key)
+ expect(address.firstname).to eq('John')
+ expect(address.lastname).to eq('Doe')
+ expect(address.address1).to eq('123 Main St')
+ expect(address.city).to eq('New York')
+ expect(address.zipcode).to eq('10001')
+ expect(address.country.iso).to eq('US')
+ expect(address.state.abbr).to eq('NY')
+ end
+
+ context 'when order has address checkout step and is past address state' do
+ let(:order) { create(:order_with_line_items, user: user, store: store, state: 'delivery') }
+
+ it 'reverts to address then auto-advances to payment' do
+ expect(subject).to be_success
+ expect(order.reload.state).to eq('payment')
+ end
+ end
+
+ context 'when order is in cart state' do
+ let(:order) { create(:order_with_line_items, user: user, store: store, state: 'cart') }
+
+ it 'auto-advances to payment' do
+ expect(subject).to be_success
+ expect(order.reload.state).to eq('payment')
+ end
+ end
+ end
+
+ context 'with existing address by nested id' do
+ let(:existing_address) { create(:address, user: user) }
+ let(:params) { { address_key => { id: existing_address.prefixed_id } } }
+
+ it 'uses the existing address' do
+ expect(subject).to be_success
+ expect(order.reload.public_send(address_id_key)).to eq(existing_address.id)
+ end
+ end
+
+ context 'with top-level address_id parameter' do
+ let(:existing_address) { create(:address, user: user) }
+ let(:params) { { address_id_key => existing_address.prefixed_id } }
+
+ it 'uses the existing address' do
+ expect(subject).to be_success
+ expect(order.reload.public_send(address_id_key)).to eq(existing_address.id)
+ end
+ end
+ end
- expect(order.state).to eq 'address'
- end
- end
+ describe 'ship_address' do
+ include_examples 'address update', :ship_address
+ end
- context 'at address state' do
- let(:order_state) { 'address' }
- let(:ship_address_id) { order.ship_address_id }
- let(:bill_address_id) { order.bill_address_id }
- let(:address_attributes) { nil }
- let(:address) { create(:address, user: user) }
-
- shared_examples 'user default addresses did not change' do
- it 'does not change user default addresses' do
- update_service
-
- expect(user.reload.bill_address_id).not_to eq order.reload.bill_address
- expect(user.reload.bill_address_id).not_to eq order.reload.ship_address
- expect(user.reload.ship_address_id).not_to eq order.reload.bill_address
- expect(user.reload.ship_address_id).not_to eq order.reload.ship_address
+ describe 'bill_address' do
+ include_examples 'address update', :bill_address
end
end
- shared_examples 'checkout is in address step' do
- it 'keeps checkout in address step' do
- update_service
-
- expect(order.reload.state).to eq 'address'
+ describe 'address ownership' do
+ let(:other_user) { create(:user) }
+ let(:other_users_address) { create(:address, user: other_user) }
+
+ shared_examples 'ignores other users address' do |address_type|
+ context "when using another user's address for #{address_type}" do
+ let(:params) { { address_type => { id: other_users_address.prefixed_id } } }
+
+ it 'ignores the address and keeps original' do
+ original_address_id = order.public_send(:"#{address_type}_id")
+ expect(subject).to be_success
+ expect(order.reload.public_send(:"#{address_type}_id")).to eq(original_address_id)
+ expect(order.public_send(:"#{address_type}_id")).not_to eq(other_users_address.id)
+ end
+ end
+
+ context "when using another user's address via #{address_type}_id" do
+ let(:params) { { :"#{address_type}_id" => other_users_address.prefixed_id } }
+
+ it 'ignores the address and keeps original' do
+ original_address_id = order.public_send(:"#{address_type}_id")
+ expect(subject).to be_success
+ expect(order.reload.public_send(:"#{address_type}_id")).to eq(original_address_id)
+ expect(order.public_send(:"#{address_type}_id")).not_to eq(other_users_address.id)
+ end
+ end
end
- end
- context 'when address did not change' do
- it_behaves_like 'user default addresses did not change'
- it_behaves_like 'checkout is in address step'
+ include_examples 'ignores other users address', :ship_address
+ include_examples 'ignores other users address', :bill_address
- it 'does not change order addresses' do
- expect(update_service).to be_success
+ context 'when order has no user (guest order)' do
+ let(:order) { create(:order_with_line_items, user: nil, store: store) }
+ let(:params) { { ship_address_id: other_users_address.prefixed_id } }
- expect(order.reload.bill_address_id).to eq user.bill_address_id
- expect(order.reload.ship_address_id).to eq user.ship_address_id
+ it 'ignores address_id params and keeps existing address' do
+ original_address_id = order.ship_address_id
+ expect(subject).to be_success
+ expect(order.reload.ship_address_id).to eq(original_address_id)
+ end
end
end
- context 'when ship address changed' do
- let(:ship_address_id) { address.id }
+ describe 'updating metadata' do
+ let(:params) { { metadata: { 'erp_id' => '12345', 'source' => 'mobile' } } }
- it_behaves_like 'user default addresses did not change'
- it_behaves_like 'checkout is in address step'
-
- it 'should update order ship address' do
- expect(update_service).to be_success
- expect(order.reload.bill_address_id).to eq user.bill_address_id
- expect(order.reload.ship_address_id).to eq address.id
+ it 'merges metadata into the order' do
+ expect(subject).to be_success
+ expect(order.reload.metadata).to include('erp_id' => '12345', 'source' => 'mobile')
end
- end
- context 'when bill address changed' do
- let(:bill_address_id) { address.id }
+ context 'with existing metadata' do
+ before { order.update!(metadata: { 'existing_key' => 'existing_value' }) }
- it_behaves_like 'user default addresses did not change'
- it_behaves_like 'checkout is in address step'
+ let(:params) { { metadata: { 'new_key' => 'new_value' } } }
- it 'should update order bill address' do
- expect(update_service).to be_success
- expect(order.reload.bill_address_id).to eq address.id
- expect(order.reload.ship_address_id).to eq user.ship_address_id
+ it 'merges without removing existing keys' do
+ expect(subject).to be_success
+ expect(order.reload.metadata).to include('existing_key' => 'existing_value', 'new_key' => 'new_value')
+ end
end
end
- context 'when ship and bill address changed' do
- let(:ship_address_id) { address.id }
- let(:bill_address_id) { address.id }
+ describe 'updating multiple fields' do
+ let(:country) { Spree::Country.find_by(iso: 'US') || create(:country, iso: 'US') }
+ let!(:state) { country.states.find_by(abbr: 'NY') || create(:state, country: country, abbr: 'NY', name: 'New York') }
- it_behaves_like 'user default addresses did not change'
- it_behaves_like 'checkout is in address step'
+ let(:params) do
+ {
+ email: '[email protected]',
+ special_instructions: 'Handle with care',
+ ship_address: {
+ firstname: 'John',
+ lastname: 'Doe',
+ address1: '123 Main St',
+ city: 'New York',
+ zipcode: '10001',
+ country_iso: 'US',
+ state_abbr: 'NY'
+ }
+ }
+ end
- it 'should update both order addresses' do
- expect(update_service).to be_success
- expect(order.reload.bill_address_id).to eq address.id
- expect(order.reload.ship_address_id).to eq address.id
+ it 'updates all fields in a single transaction' do
+ expect(subject).to be_success
+ order.reload
+ expect(order.email).to eq('[email protected]')
+ expect(order.special_instructions).to eq('Handle with care')
+ expect(order.ship_address.firstname).to eq('John')
end
end
- end
- end
- describe 'address ownership validation' do
- let(:user) { create(:user_with_addresses) }
- let(:other_user) { create(:user_with_addresses) }
- let(:order) { create(:order_with_line_items, user: user, state: 'address') }
- let(:permitted_attributes) do
- Spree::PermittedAttributes.checkout_attributes + [
- bill_address_attributes: Spree::PermittedAttributes.address_attributes,
- ship_address_attributes: Spree::PermittedAttributes.address_attributes
- ]
- end
+ describe 'setting line items' do
+ let(:variant) { create(:variant) }
- context 'when bill_address_attributes contains id of another user address' do
- let(:other_user_address) { other_user.bill_address }
- let(:order_params) do
- ActionController::Parameters.new(
- order: {
- bill_address_attributes: { id: other_user_address.id }
- }
- )
- end
+ before do
+ variant.stock_items.first.update!(count_on_hand: 10)
+ store.products << variant.product unless store.products.include?(variant.product)
+ end
- it 'returns failure' do
- result = described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
- expect(result).to be_failure
- end
+ context 'with new line_items' do
+ let(:params) do
+ {
+ line_items: [
+ { variant_id: variant.prefixed_id, quantity: 2 }
+ ]
+ }
+ end
+
+ it 'adds line items to the order' do
+ expect(subject).to be_success
+ order.reload
+ line_item = order.line_items.find_by(variant: variant)
+ expect(line_item).to be_present
+ expect(line_item.quantity).to eq(2)
+ end
+ end
- it 'does not associate the other user address with the order' do
- described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
- expect(order.reload.bill_address_id).not_to eq other_user_address.id
- end
- end
+ context 'with existing line item (upsert)' do
+ let!(:existing_line_item) { order.line_items.first }
+ let(:existing_variant) { existing_line_item.variant }
- context 'when ship_address_attributes contains id of another user address' do
- let(:other_user_address) { other_user.ship_address }
- let(:order_params) do
- ActionController::Parameters.new(
- order: {
- ship_address_attributes: { id: other_user_address.id }
- }
- )
- end
+ before do
+ store.products << existing_variant.product unless store.products.include?(existing_variant.product)
+ end
- it 'returns failure' do
- result = described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
- expect(result).to be_failure
- end
+ let(:params) do
+ {
+ line_items: [
+ { variant_id: existing_variant.prefixed_id, quantity: 7 }
+ ]
+ }
+ end
- it 'does not associate the other user address with the order' do
- described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
- expect(order.reload.ship_address_id).not_to eq other_user_address.id
- end
- end
+ it 'sets quantity instead of incrementing' do
+ expect(subject).to be_success
+ expect(existing_line_item.reload.quantity).to eq(7)
+ end
+ end
- context 'when address_attributes contains id of the same user address' do
- let(:user_address) { create(:address, user: user) }
- let(:order_params) do
- ActionController::Parameters.new(
- order: {
- bill_address_attributes: { id: user_address.id }
- }
- )
+ context 'with invalid variant_id' do
+ let(:params) do
+ {
+ line_items: [
+ { variant_id: 'variant_invalid999', quantity: 1 }
+ ]
+ }
+ end
+
+ it 'raises RecordNotFound with variant details' do
+ expect { subject }.to raise_error(ActiveRecord::RecordNotFound) do |error|
+ expect(error.model).to eq('Spree::Variant')
+ expect(error.message).to include('variant_invalid999')
+ end
+ end
+ end
end
- it 'returns success' do
- result = described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
- expect(result).to be_success
- end
- end
+ describe 'error handling' do
+ context 'with invalid address prefix_id' do
+ let(:params) { { ship_address_id: 'addr_invalid123' } }
- context 'when address_attributes contains id of address with no user' do
- let(:guest_address) { create(:address, user: nil) }
- let(:order_params) do
- ActionController::Parameters.new(
- order: {
- bill_address_attributes: { id: guest_address.id }
- }
- )
- end
+ it 'succeeds but does not change the address' do
+ original_address_id = order.ship_address_id
+ expect(subject).to be_success
+ expect(order.reload.ship_address_id).to eq(original_address_id)
+ end
+ end
- it 'returns success' do
- result = described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
- expect(result).to be_success
- end
- end
+ context 'when order save fails' do
+ let(:params) { { email: '[email protected]' } }
- context 'when address_attributes does not contain id' do
- let(:state) { create(:state) }
- let(:country) { state.country }
- let(:order_params) do
- ActionController::Parameters.new(
- order: {
- bill_address_attributes: {
- firstname: 'John',
- lastname: 'Doe',
- address1: '123 Main St',
- city: 'Anytown',
- zipcode: '12345',
- country_iso: country.iso,
- state_id: state.id,
- phone: '555-1234'
- }
- }
- )
- end
+ before do
+ allow(order).to receive(:save!).and_raise(ActiveRecord::RecordInvalid.new(order))
+ end
- it 'returns success' do
- result = described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
- expect(result).to be_success
+ it 'returns failure with error message' do
+ expect(subject).to be_failure
+ end
+ end
end
- end
- end
-
- describe 'update selected shipping rate' do
- let(:update_service) { described_class.new }
- let(:order) { create(:order_with_line_items) }
- let(:order_params) do
- ActionController::Parameters.new(
- order: {
- shipments_attributes: [
- {
- id: order.shipments.first.id,
- selected_shipping_rate_id: order.shipments.first.shipping_rates.first.id
- }
- ]
- }
- )
- end
- let(:permitted_attributes) do
- Spree::PermittedAttributes.checkout_attributes + [
- shipments_attributes: Spree::PermittedAttributes.shipment_attributes
- ]
- end
- it 'should set order back to delivery state' do
- expect(order.state).not_to eq 'delivery'
+ describe 'parameter normalization' do
+ let(:params) { { 'email' => '[email protected]' } }
- update_service.send(:call, order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
+ it 'handles string keys' do
+ expect(subject).to be_success
+ expect(order.reload.email).to eq('[email protected]')
+ end
+ end
- expect(order.state).to eq 'delivery'
end
end
end
diff --git a/spree/core/spec/services/spree/checkout/v2_update_spec.rb b/spree/core/spec/services/spree/checkout/v2_update_spec.rb
new file mode 100644
index 0000000000..2827ba73da
--- /dev/null
+++ b/spree/core/spec/services/spree/checkout/v2_update_spec.rb
@@ -0,0 +1,347 @@
+require 'spec_helper'
+
+describe Spree::Checkout::V2Update, type: :service do
+ describe '#transform_address_params' do
+ let!(:state) { create(:state) }
+ let!(:country) { state.country }
+ let!(:replace_country) { described_class.new }
+ let!(:address) do
+ {
+ firstname: 'John',
+ lastname: 'Doe',
+ address1: '7735 Old Georgetown Road',
+ city: 'Bethesda',
+ phone: '3014445002',
+ zipcode: '20814',
+ state_id: state.id,
+ country_iso: country.iso
+ }
+ end
+
+ context 'with ship_address order params' do
+ let(:order_params) do
+ ActionController::Parameters.new(
+ order: {
+ ship_address_attributes: address
+ }
+ )
+ end
+ let(:result) { replace_country.send(:replace_country_iso_with_id, order_params[:order][:ship_address_attributes]) }
+
+ before { result }
+
+ it 'will return hash contain country_id' do
+ expect(result[:country_id]).to eq country.id
+ end
+
+ it 'will return hash without country_iso' do
+ expect(result).not_to include(:country_iso)
+ end
+ end
+
+ context 'with bill_address order params' do
+ let(:order_params) do
+ ActionController::Parameters.new(
+ order: {
+ bill_address_attributes: address
+ }
+ )
+ end
+ let(:result) { replace_country.send(:replace_country_iso_with_id, order_params[:order][:bill_address_attributes]) }
+
+ before { result }
+
+ it 'will return hash contain country_id' do
+ expect(result[:country_id]).to eq country.id
+ end
+
+ it 'will return hash without country_iso' do
+ expect(result).not_to include(:country_iso)
+ end
+ end
+ end
+
+ describe 'update address' do
+ let(:user) { create(:user_with_addresses) }
+ let(:order) { create(:order_with_line_items, user: user, bill_address: user.bill_address, ship_address: user.ship_address, state: order_state) }
+ let(:state) { create(:state) }
+ let(:country) { state.country }
+ let(:update_service) { described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil) }
+ let(:order_params) { ActionController::Parameters.new(order: {
+ ship_address_id: ship_address_id,
+ bill_address_id: bill_address_id,
+ ship_address_attributes: address_attributes
+ }) }
+ let(:permitted_attributes) do
+ Spree::PermittedAttributes.checkout_attributes + [
+ bill_address_attributes: Spree::PermittedAttributes.address_attributes,
+ ship_address_attributes: Spree::PermittedAttributes.address_attributes
+ ]
+ end
+
+ context 'at cart state' do
+ let(:order_state) { 'cart' }
+ let(:ship_address_id) { nil }
+ let(:bill_address_id) { nil }
+ let(:address_attributes) do
+ {
+ firstname: 'John',
+ lastname: 'Doe',
+ address1: '7735 Old Georgetown Road',
+ city: 'Bethesda',
+ phone: '3014445002',
+ zipcode: '20814',
+ state_id: state.id,
+ country_iso: country.iso
+ }
+ end
+
+ it 'should set order back to address state' do
+ expect(order.state).not_to eq 'address'
+ expect(order.ship_address.state.id).not_to eq state.id
+
+ update_service
+
+ expect(order.state).to eq 'address'
+ expect(order.ship_address.state.id).to eq state.id
+ end
+
+ it 'should not set order back to address state if do_not_change_state is true' do
+ expect(order.state).not_to eq 'address'
+ order_params[:do_not_change_state] = true
+ update_service
+
+ expect(order.state).not_to eq 'address'
+ end
+
+ it 'should set order back to address state if quick checkout cancelled' do
+ expect(order.state).not_to eq 'address'
+ order_params[:order][:ship_address_id] = 'CLEAR'
+ update_service
+
+ expect(order.state).to eq 'address'
+ end
+ end
+
+ context 'at address state' do
+ let(:order_state) { 'address' }
+ let(:ship_address_id) { order.ship_address_id }
+ let(:bill_address_id) { order.bill_address_id }
+ let(:address_attributes) { nil }
+ let(:address) { create(:address, user: user) }
+
+ shared_examples 'user default addresses did not change' do
+ it 'does not change user default addresses' do
+ update_service
+
+ expect(user.reload.bill_address_id).not_to eq order.reload.bill_address
+ expect(user.reload.bill_address_id).not_to eq order.reload.ship_address
+ expect(user.reload.ship_address_id).not_to eq order.reload.bill_address
+ expect(user.reload.ship_address_id).not_to eq order.reload.ship_address
+ end
+ end
+
+ shared_examples 'checkout is in address step' do
+ it 'keeps checkout in address step' do
+ update_service
+
+ expect(order.reload.state).to eq 'address'
+ end
+ end
+
+ context 'when address did not change' do
+ it_behaves_like 'user default addresses did not change'
+ it_behaves_like 'checkout is in address step'
+
+ it 'does not change order addresses' do
+ expect(update_service).to be_success
+
+ expect(order.reload.bill_address_id).to eq user.bill_address_id
+ expect(order.reload.ship_address_id).to eq user.ship_address_id
+ end
+ end
+
+ context 'when ship address changed' do
+ let(:ship_address_id) { address.id }
+
+ it_behaves_like 'user default addresses did not change'
+ it_behaves_like 'checkout is in address step'
+
+ it 'should update order ship address' do
+ expect(update_service).to be_success
+ expect(order.reload.bill_address_id).to eq user.bill_address_id
+ expect(order.reload.ship_address_id).to eq address.id
+ end
+ end
+
+ context 'when bill address changed' do
+ let(:bill_address_id) { address.id }
+
+ it_behaves_like 'user default addresses did not change'
+ it_behaves_like 'checkout is in address step'
+
+ it 'should update order bill address' do
+ expect(update_service).to be_success
+ expect(order.reload.bill_address_id).to eq address.id
+ expect(order.reload.ship_address_id).to eq user.ship_address_id
+ end
+ end
+
+ context 'when ship and bill address changed' do
+ let(:ship_address_id) { address.id }
+ let(:bill_address_id) { address.id }
+
+ it_behaves_like 'user default addresses did not change'
+ it_behaves_like 'checkout is in address step'
+
+ it 'should update both order addresses' do
+ expect(update_service).to be_success
+ expect(order.reload.bill_address_id).to eq address.id
+ expect(order.reload.ship_address_id).to eq address.id
+ end
+ end
+ end
+ end
+
+ describe 'address ownership validation' do
+ let(:user) { create(:user_with_addresses) }
+ let(:other_user) { create(:user_with_addresses) }
+ let(:order) { create(:order_with_line_items, user: user, state: 'address') }
+ let(:permitted_attributes) do
+ Spree::PermittedAttributes.checkout_attributes + [
+ bill_address_attributes: Spree::PermittedAttributes.address_attributes,
+ ship_address_attributes: Spree::PermittedAttributes.address_attributes
+ ]
+ end
+
+ context 'when bill_address_attributes contains id of another user address' do
+ let(:other_user_address) { other_user.bill_address }
+ let(:order_params) do
+ ActionController::Parameters.new(
+ order: {
+ bill_address_attributes: { id: other_user_address.id }
+ }
+ )
+ end
+
+ it 'returns failure' do
+ result = described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
+ expect(result).to be_failure
+ end
+
+ it 'does not associate the other user address with the order' do
+ described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
+ expect(order.reload.bill_address_id).not_to eq other_user_address.id
+ end
+ end
+
+ context 'when ship_address_attributes contains id of another user address' do
+ let(:other_user_address) { other_user.ship_address }
+ let(:order_params) do
+ ActionController::Parameters.new(
+ order: {
+ ship_address_attributes: { id: other_user_address.id }
+ }
+ )
+ end
+
+ it 'returns failure' do
+ result = described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
+ expect(result).to be_failure
+ end
+
+ it 'does not associate the other user address with the order' do
+ described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
+ expect(order.reload.ship_address_id).not_to eq other_user_address.id
+ end
+ end
+
+ context 'when address_attributes contains id of the same user address' do
+ let(:user_address) { create(:address, user: user) }
+ let(:order_params) do
+ ActionController::Parameters.new(
+ order: {
+ bill_address_attributes: { id: user_address.id }
+ }
+ )
+ end
+
+ it 'returns success' do
+ result = described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
+ expect(result).to be_success
+ end
+ end
+
+ context 'when address_attributes contains id of address with no user' do
+ let(:guest_address) { create(:address, user: nil) }
+ let(:order_params) do
+ ActionController::Parameters.new(
+ order: {
+ bill_address_attributes: { id: guest_address.id }
+ }
+ )
+ end
+
+ it 'returns success' do
+ result = described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
+ expect(result).to be_success
+ end
+ end
+
+ context 'when address_attributes does not contain id' do
+ let(:state) { create(:state) }
+ let(:country) { state.country }
+ let(:order_params) do
+ ActionController::Parameters.new(
+ order: {
+ bill_address_attributes: {
+ firstname: 'John',
+ lastname: 'Doe',
+ address1: '123 Main St',
+ city: 'Anytown',
+ zipcode: '12345',
+ country_iso: country.iso,
+ state_id: state.id,
+ phone: '555-1234'
+ }
+ }
+ )
+ end
+
+ it 'returns success' do
+ result = described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
+ expect(result).to be_success
+ end
+ end
+ end
+
+ describe 'update selected shipping rate' do
+ let(:update_service) { described_class.new }
+ let(:order) { create(:order_with_line_items) }
+ let(:order_params) do
+ ActionController::Parameters.new(
+ order: {
+ shipments_attributes: [
+ {
+ id: order.shipments.first.id,
+ selected_shipping_rate_id: order.shipments.first.shipping_rates.first.id
+ }
+ ]
+ }
+ )
+ end
+ let(:permitted_attributes) do
+ Spree::PermittedAttributes.checkout_attributes + [
+ shipments_attributes: Spree::PermittedAttributes.shipment_attributes
+ ]
+ end
+
+ it 'should set order back to delivery state' do
+ expect(order.state).not_to eq 'delivery'
+
+ update_service.send(:call, order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
+
+ expect(order.state).to eq 'delivery'
+ end
+ end
+end
diff --git a/spree/core/spec/services/spree/orders/update_spec.rb b/spree/core/spec/services/spree/orders/update_spec.rb
deleted file mode 100644
index d242b7c4a0..0000000000
--- a/spree/core/spec/services/spree/orders/update_spec.rb
+++ /dev/null
@@ -1,353 +0,0 @@
-require 'spec_helper'
-
-module Spree
- RSpec.describe Orders::Update do
- let(:store) { create(:store, supported_currencies: 'USD,EUR,GBP') }
- let(:user) { create(:user) }
- let(:order) { create(:order_with_line_items, user: user, store: store, currency: 'USD') }
-
- describe '#call' do
- subject { described_class.call(order: order, params: params) }
-
- describe 'updating email' do
- let(:params) { { email: '[email protected]' } }
-
- it 'updates the order email' do
- expect(subject).to be_success
- expect(order.reload.email).to eq('[email protected]')
- end
- end
-
- describe 'updating special_instructions' do
- let(:params) { { special_instructions: 'Leave at the door' } }
-
- it 'updates the special instructions' do
- expect(subject).to be_success
- expect(order.reload.special_instructions).to eq('Leave at the door')
- end
-
- context 'when clearing special_instructions' do
- let(:order) { create(:order_with_line_items, user: user, store: store, special_instructions: 'Existing instructions') }
-
- it 'clears with empty string' do
- result = described_class.call(order: order, params: { special_instructions: '' })
- expect(result).to be_success
- expect(order.reload.special_instructions).to eq('')
- end
-
- it 'clears with nil' do
- result = described_class.call(order: order, params: { special_instructions: nil })
- expect(result).to be_success
- expect(order.reload.special_instructions).to be_nil
- end
- end
- end
-
- describe 'updating currency' do
- context 'with supported currency' do
- let(:params) { { currency: 'EUR' } }
-
- it 'updates the currency' do
- expect(subject).to be_success
- expect(order.reload.currency).to eq('EUR')
- end
-
- it 'is case-insensitive' do
- result = described_class.call(order: order, params: { currency: 'eur' })
- expect(result).to be_success
- expect(order.reload.currency).to eq('EUR')
- end
- end
-
- context 'with unsupported currency' do
- let(:params) { { currency: 'JPY' } }
-
- it 'returns failure' do
- expect(subject).to be_failure
- end
-
- it 'does not change the currency' do
- subject
- expect(order.reload.currency).to eq('USD')
- end
- end
- end
-
- describe 'updating addresses' do
- let(:country) { Spree::Country.find_by(iso: 'US') || create(:country, iso: 'US') }
- let!(:state) { country.states.find_by(abbr: 'NY') || create(:state, country: country, abbr: 'NY', name: 'New York') }
-
- shared_examples 'address update' do |address_type|
- let(:address_key) { address_type }
- let(:address_id_key) { :"#{address_type}_id" }
-
- context 'with new address attributes' do
- let(:params) do
- {
- address_key => {
- firstname: 'John',
- lastname: 'Doe',
- address1: '123 Main St',
- city: 'New York',
- zipcode: '10001',
- country_iso: 'US',
- state_abbr: 'NY',
- phone: '555-1234'
- }
- }
- end
-
- it 'creates a new address' do
- expect(subject).to be_success
- address = order.reload.public_send(address_key)
- expect(address.firstname).to eq('John')
- expect(address.lastname).to eq('Doe')
- expect(address.address1).to eq('123 Main St')
- expect(address.city).to eq('New York')
- expect(address.zipcode).to eq('10001')
- expect(address.country.iso).to eq('US')
- expect(address.state.abbr).to eq('NY')
- end
-
- context 'when order has address checkout step and is past address state' do
- let(:order) { create(:order_with_line_items, user: user, store: store, state: 'delivery') }
-
- it 'reverts order to address state' do
- expect(subject).to be_success
- expect(order.reload.state).to eq('address')
- end
- end
-
- context 'when order is in cart state' do
- let(:order) { create(:order_with_line_items, user: user, store: store, state: 'cart') }
-
- it 'does not change order state' do
- expect(subject).to be_success
- expect(order.reload.state).to eq('cart')
- end
- end
- end
-
- context 'with existing address by nested id' do
- let(:existing_address) { create(:address, user: user) }
- let(:params) { { address_key => { id: existing_address.prefixed_id } } }
-
- it 'uses the existing address' do
- expect(subject).to be_success
- expect(order.reload.public_send(address_id_key)).to eq(existing_address.id)
- end
- end
-
- context 'with top-level address_id parameter' do
- let(:existing_address) { create(:address, user: user) }
- let(:params) { { address_id_key => existing_address.prefixed_id } }
-
- it 'uses the existing address' do
- expect(subject).to be_success
- expect(order.reload.public_send(address_id_key)).to eq(existing_address.id)
- end
- end
- end
-
- describe 'ship_address' do
- include_examples 'address update', :ship_address
- end
-
- describe 'bill_address' do
- include_examples 'address update', :bill_address
- end
- end
-
- describe 'address ownership' do
- let(:other_user) { create(:user) }
- let(:other_users_address) { create(:address, user: other_user) }
-
- shared_examples 'ignores other users address' do |address_type|
- context "when using another user's address for #{address_type}" do
- let(:params) { { address_type => { id: other_users_address.prefixed_id } } }
-
- it 'ignores the address and keeps original' do
- original_address_id = order.public_send(:"#{address_type}_id")
- expect(subject).to be_success
- expect(order.reload.public_send(:"#{address_type}_id")).to eq(original_address_id)
- expect(order.public_send(:"#{address_type}_id")).not_to eq(other_users_address.id)
- end
- end
-
- context "when using another user's address via #{address_type}_id" do
- let(:params) { { :"#{address_type}_id" => other_users_address.prefixed_id } }
-
- it 'ignores the address and keeps original' do
- original_address_id = order.public_send(:"#{address_type}_id")
- expect(subject).to be_success
- expect(order.reload.public_send(:"#{address_type}_id")).to eq(original_address_id)
- expect(order.public_send(:"#{address_type}_id")).not_to eq(other_users_address.id)
- end
- end
- end
-
- include_examples 'ignores other users address', :ship_address
- include_examples 'ignores other users address', :bill_address
-
- context 'when order has no user (guest order)' do
- let(:order) { create(:order_with_line_items, user: nil, store: store) }
- let(:params) { { ship_address_id: other_users_address.prefixed_id } }
-
- it 'ignores address_id params and keeps existing address' do
- original_address_id = order.ship_address_id
- expect(subject).to be_success
- expect(order.reload.ship_address_id).to eq(original_address_id)
- end
- end
- end
-
- describe 'updating metadata' do
- let(:params) { { metadata: { 'erp_id' => '12345', 'source' => 'mobile' } } }
-
- it 'merges metadata into the order' do
- expect(subject).to be_success
- expect(order.reload.metadata).to include('erp_id' => '12345', 'source' => 'mobile')
- end
-
- context 'with existing metadata' do
- before { order.update!(metadata: { 'existing_key' => 'existing_value' }) }
-
- let(:params) { { metadata: { 'new_key' => 'new_value' } } }
-
- it 'merges without removing existing keys' do
- expect(subject).to be_success
- expect(order.reload.metadata).to include('existing_key' => 'existing_value', 'new_key' => 'new_value')
- end
- end
- end
-
- describe 'updating multiple fields' do
- let(:country) { Spree::Country.find_by(iso: 'US') || create(:country, iso: 'US') }
- let!(:state) { country.states.find_by(abbr: 'NY') || create(:state, country: country, abbr: 'NY', name: 'New York') }
-
- let(:params) do
- {
- email: '[email protected]',
- special_instructions: 'Handle with care',
- ship_address: {
- firstname: 'John',
- lastname: 'Doe',
- address1: '123 Main St',
- city: 'New York',
- zipcode: '10001',
- country_iso: 'US',
- state_abbr: 'NY'
- }
- }
- end
-
- it 'updates all fields in a single transaction' do
- expect(subject).to be_success
- order.reload
- expect(order.email).to eq('[email protected]')
- expect(order.special_instructions).to eq('Handle with care')
- expect(order.ship_address.firstname).to eq('John')
- end
- end
-
- describe 'setting line items' do
- let(:variant) { create(:variant) }
-
- before do
- variant.stock_items.first.update!(count_on_hand: 10)
- store.products << variant.product unless store.products.include?(variant.product)
- end
-
- context 'with new line_items' do
- let(:params) do
- {
- line_items: [
- { variant_id: variant.prefixed_id, quantity: 2 }
- ]
- }
- end
-
- it 'adds line items to the order' do
- expect(subject).to be_success
- order.reload
- line_item = order.line_items.find_by(variant: variant)
- expect(line_item).to be_present
- expect(line_item.quantity).to eq(2)
- end
- end
-
- context 'with existing line item (upsert)' do
- let!(:existing_line_item) { order.line_items.first }
- let(:existing_variant) { existing_line_item.variant }
-
- before do
- store.products << existing_variant.product unless store.products.include?(existing_variant.product)
- end
-
- let(:params) do
- {
- line_items: [
- { variant_id: existing_variant.prefixed_id, quantity: 7 }
- ]
- }
- end
-
- it 'sets quantity instead of incrementing' do
- expect(subject).to be_success
- expect(existing_line_item.reload.quantity).to eq(7)
- end
- end
-
- context 'with invalid variant_id' do
- let(:params) do
- {
- line_items: [
- { variant_id: 'variant_invalid999', quantity: 1 }
- ]
- }
- end
-
- it 'raises RecordNotFound with variant details' do
- expect { subject }.to raise_error(ActiveRecord::RecordNotFound) do |error|
- expect(error.model).to eq('Spree::Variant')
- expect(error.message).to include('variant_invalid999')
- end
- end
- end
- end
-
- describe 'error handling' do
- context 'with invalid address prefix_id' do
- let(:params) { { ship_address_id: 'addr_invalid123' } }
-
- it 'succeeds but does not change the address' do
- original_address_id = order.ship_address_id
- expect(subject).to be_success
- expect(order.reload.ship_address_id).to eq(original_address_id)
- end
- end
-
- context 'when order save fails' do
- let(:params) { { email: '[email protected]' } }
-
- before do
- allow(order).to receive(:save!).and_raise(ActiveRecord::RecordInvalid.new(order))
- end
-
- it 'returns failure with error message' do
- expect(subject).to be_failure
- end
- end
- end
-
- describe 'parameter normalization' do
- let(:params) { { 'email' => '[email protected]' } }
-
- it 'handles string keys' do
- expect(subject).to be_success
- expect(order.reload.email).to eq('[email protected]')
- end
- end
- end
- end
-end