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.

Remove dead Checkout::Update and Checkout::GetShippingRates

Damian Legawiecab0cb3e9e452
diff --git a/spree/core/app/services/spree/checkout/get_shipping_rates.rb b/spree/core/app/services/spree/checkout/get_shipping_rates.rb
deleted file mode 100644
index 84fe7eb0f4..0000000000
--- a/spree/core/app/services/spree/checkout/get_shipping_rates.rb
+++ /dev/null
@@ -1,52 +0,0 @@
-module Spree
- module Checkout
- class GetShippingRates
- prepend Spree::ServiceModule::Base
-
- def call(order:)
- run :reload_order
- run :ensure_shipping_address
- run :ensure_line_items_present
- run :generate_or_return_shipping_rates
- end
-
- private
-
- # we need to reload order to fetch the most up-to-date version of it
- def reload_order(order:)
- success(order: order.reload)
- end
-
- def ensure_shipping_address(order:)
- return failure([], Spree.t('errors.services.get_shipping_rates.no_shipping_address')) if order.ship_address.blank?
-
- success(order: order)
- end
-
- def ensure_line_items_present(order:)
- return failure([], Spree.t('errors.services.get_shipping_rates.no_line_items')) if order.line_items.empty?
-
- success(order: order)
- end
-
- def generate_or_return_shipping_rates(order:)
- generate_shipping_rates(order: order) if order.fulfillments.empty?
- return_shipments(order: order)
- end
-
- def generate_shipping_rates(order:)
- ApplicationRecord.transaction do
- order.create_proposed_fulfillments
- order.create_shipment_tax_charge!
- order.set_shipments_cost
- order.apply_free_shipping_promotions
- end
- success(order: order.reload)
- end
-
- def return_shipments(order:)
- success(order.reload.shipments.includes([shipping_rates: :shipping_method]))
- end
- end
- end
-end
diff --git a/spree/core/app/services/spree/checkout/update.rb b/spree/core/app/services/spree/checkout/update.rb
deleted file mode 100644
index 5574c625d0..0000000000
--- a/spree/core/app/services/spree/checkout/update.rb
+++ /dev/null
@@ -1,109 +0,0 @@
-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
-
- # An address change invalidates the delivery proposals — rebuild them
- # (recalculation-on-write; there are no states to rewind).
- address_changed = ship_changed || bill_changed || quick_checkout_cancelled?(params)
-
- return success(order) if update_from_params(order, params, permitted_attributes, request_env, address_changed)
-
- failure(order)
- end
-
- private
-
- def update_from_params(order, params, permitted_attributes, request_env, address_changed)
- massage_payment_params!(order, params)
-
- attributes = params[:order] ? params[:order].permit(permitted_attributes).delete_if { |_k, v| v.nil? } : {}
-
- if (existing_card_id = params[:order]&.delete(:existing_card)).present?
- credit_card = Spree::CreditCard.find(existing_card_id)
- if credit_card.user_id != order.user_id || credit_card.user_id.blank?
- raise Spree::Core::GatewayError, Spree.t(:invalid_credit_card)
- end
-
- credit_card.verification_value = params[:cvc_confirm] if params[:cvc_confirm].present?
-
- attributes[:payments_attributes].first[:source] = credit_card
- attributes[:payments_attributes].first[:payment_method_id] = credit_card.payment_method_id
- attributes[:payments_attributes].first.delete(:source_attributes)
- end
-
- attributes[:payments_attributes].first[:request_env] = request_env if attributes[:payments_attributes]
-
- result = order.update(attributes)
- if result
- order.recalculate_for_address_change! if address_changed && order.respond_to?(:recalculate_for_address_change!)
- order.set_shipments_cost if order.respond_to?(:set_shipments_cost) && order.fulfillments.any?
- end
- result
- end
-
- # For the payment step, filter parameters to the nested attributes of
- # the selected payment method and set the chargeable amount.
- def massage_payment_params!(order, params)
- if params[:payment_source].present? && params.dig(:order, :payments_attributes)
- source_params = params.delete(:payment_source)[params[:order][:payments_attributes].first[:payment_method_id].to_s]
- params[:order][:payments_attributes].first[:source_attributes] = source_params if source_params
- end
-
- if params[:order] && (params[:order][:payments_attributes] || params[:order][:existing_card])
- params[:order][:payments_attributes] ||= [{}]
- params[:order][:payments_attributes].first[:amount] = order.order_total_after_store_credit
- end
- end
-
- 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/lib/spree/core/dependencies.rb b/spree/core/lib/spree/core/dependencies.rb
index 3cabb16cbe..b60c119650 100644
--- a/spree/core/lib/spree/core/dependencies.rb
+++ b/spree/core/lib/spree/core/dependencies.rb
@@ -33,11 +33,9 @@ module Spree
# checkout
checkout_next_service: 'Spree::Checkout::Next',
checkout_advance_service: 'Spree::Checkout::Advance',
- checkout_update_service: 'Spree::Checkout::Update',
checkout_complete_service: 'Spree::Checkout::Complete',
checkout_add_store_credit_service: 'Spree::Checkout::AddStoreCredit',
checkout_remove_store_credit_service: 'Spree::Checkout::RemoveStoreCredit',
- checkout_get_shipping_rates_service: 'Spree::Checkout::GetShippingRates',
checkout_select_shipping_method_service: 'Spree::Checkout::SelectShippingMethod',
# gift cards
diff --git a/spree/core/spec/services/spree/checkout/get_shipping_rates_spec.rb b/spree/core/spec/services/spree/checkout/get_shipping_rates_spec.rb
deleted file mode 100644
index 07bfb640a3..0000000000
--- a/spree/core/spec/services/spree/checkout/get_shipping_rates_spec.rb
+++ /dev/null
@@ -1,124 +0,0 @@
-require 'spec_helper'
-
-module Spree
- describe Checkout::GetShippingRates do
- subject { described_class }
-
- let(:order) { create(:order) }
- let(:line_item) { create(:line_item, order: order) }
- let(:shipment) { order.reload.shipments.first }
-
- let(:execute) { subject.call(order: order) }
- let(:value) { execute.value }
- let(:error) { execute.error.to_s }
-
- let!(:country) { create(:country) }
- let!(:shipping_method) do
- create(:shipping_method).tap do |shipping_method|
- shipping_method.calculator.preferred_amount = 10
- shipping_method.calculator.save
- end
- end
- let!(:zone) { create(:zone) }
- let!(:zone_member) { create(:zone_member, zone: zone, zoneable: country) }
- let(:address) { create(:address, country: country) }
-
- let(:free_shipping_promotion) { create(:free_shipping_promotion, kind: :automatic) }
-
- shared_examples 'generates shipping rates' do
- it 'returns shipping rates' do
- expect(execute.success?).to eq(true)
- expect(value).not_to be_empty
- expect(value).to eq(order.reload.shipments)
- expect(shipment.shipping_method).to eq(shipping_method)
- end
-
- it "doesn't update checkout state" do
- expect { execute }.not_to change {
- order.completed_at
- }
- end
- end
-
- shared_examples 'applies standard shipping costs' do
- before { execute }
-
- it 'for shipment' do
- expect(shipment.final_price).to eq(10.0)
- end
-
- it 'updates shipment total' do
- expect(order.reload.shipment_total).to eq(10.0)
- end
- end
-
- shared_examples 'failure' do
- it 'returns error' do
- expect(execute.success?).to eq(false)
- expect(value).to be_empty
- expect(error).to eq(error_message)
- end
-
- it "doesn't generate shipping rates" do
- expect { execute }.not_to change {
- Spree::ShippingRate.count
- order.shipments
- order.completed_at
- }
- end
- end
-
- context 'without shipping address' do
- let(:error_message) { 'To generate Shipping Rates Order needs to have a Shipping Address' }
-
- it_behaves_like 'failure'
- end
-
- context 'without line items' do
- let(:error_message) { 'To generate Shipping Rates you need to add some Line Items to Order' }
-
- before do
- order.ship_address = address
- order.save!
- end
-
- it_behaves_like 'failure'
- end
-
- context 'with line items and shipping address' do
- before do
- line_item
- order.ship_address = address
- order.save!
- end
-
- context 'without shipments' do
- before { order.shipments.delete_all }
-
- it_behaves_like 'generates shipping rates'
- it_behaves_like 'applies standard shipping costs'
- end
-
- context 'with already present shipments' do
- it_behaves_like 'generates shipping rates'
- it_behaves_like 'applies standard shipping costs'
-
- it 'replaces current shipments with new ones' do
- expect { execute }.to change { order.shipments.pluck(:number) }
- end
- end
-
- context 'with free shipping promotion' do
- before do
- free_shipping_promotion
- execute
- end
-
- it 'applies promotion' do
- expect(order.promotions).to include(free_shipping_promotion)
- expect(shipment.final_price).to eq(0.0)
- end
- end
- end
- end
-end
diff --git a/spree/core/spec/services/spree/checkout/select_shipping_method_spec.rb b/spree/core/spec/services/spree/checkout/select_shipping_method_spec.rb
index cabbdba768..8d224faea0 100644
--- a/spree/core/spec/services/spree/checkout/select_shipping_method_spec.rb
+++ b/spree/core/spec/services/spree/checkout/select_shipping_method_spec.rb
@@ -49,7 +49,7 @@ module Spree
context 'one shipment' do
before do
- Spree::Checkout::GetShippingRates.call(order: order)
+ order.create_proposed_fulfillments
order.reload
end
@@ -86,7 +86,7 @@ module Spree
let(:shipment_2) { order.shipments.last }
before do
- Spree::Checkout::GetShippingRates.call(order: order)
+ order.create_proposed_fulfillments
order.reload
end
diff --git a/spree/core/spec/services/spree/checkout/update_spec.rb b/spree/core/spec/services/spree/checkout/update_spec.rb
deleted file mode 100644
index b99a22f862..0000000000
--- a/spree/core/spec/services/spree/checkout/update_spec.rb
+++ /dev/null
@@ -1,347 +0,0 @@
-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
-
- 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.reload.completed_at).to be_nil
- expect(order.ship_address.state.id).not_to eq state.id
-
- update_service
-
- expect(order.reload.completed_at).to be_nil
- 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.reload.completed_at).to be_nil
- order_params[:do_not_change_state] = true
- update_service
-
- expect(order.reload.completed_at).to be_nil
- end
-
- it 'should set order back to address state if quick checkout cancelled' do
- expect(order.reload.completed_at).to be_nil
- order_params[:order][:ship_address_id] = 'CLEAR'
- update_service
-
- expect(order.reload.completed_at).to be_nil
- 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.completed_at).to be_nil
- 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.reload.fulfillments).to be_present
-
- update_service.send(:call, order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
-
- expect(order.reload.fulfillments).to be_present
- end
- end
-end