diff --git a/app/controllers/split_checkout_controller.rb b/app/controllers/split_checkout_controller.rb index 952569c9e9..61276b4303 100644 --- a/app/controllers/split_checkout_controller.rb +++ b/app/controllers/split_checkout_controller.rb @@ -56,7 +56,7 @@ class SplitCheckoutController < ::BaseController end def update_order - return unless params[:order] + return if @order.errors.any? @order.update(order_params) && advance_order_state end @@ -79,37 +79,7 @@ class SplitCheckoutController < ::BaseController end def order_params - return @order_params unless @order_params.nil? - - @order_params = params.require(:order).permit( - :email, :shipping_method_id, :special_instructions, - bill_address_attributes: PermittedAttributes::Address.attributes, - ship_address_attributes: PermittedAttributes::Address.attributes, - payments_attributes: [:payment_method_id] - ) - - set_address_details - set_payment_amount - - @order_params - end - - def set_address_details - return unless @order_params[:ship_address_attributes] && @order_params[:bill_address_attributes] - - if params[:ship_address_same_as_billing] - @order_params[:ship_address_attributes] = @order_params[:bill_address_attributes] - else - @order_params[:ship_address_attributes][:firstname] = @order_params[:bill_address_attributes][:firstname] - @order_params[:ship_address_attributes][:lastname] = @order_params[:bill_address_attributes][:lastname] - @order_params[:ship_address_attributes][:phone] = @order_params[:bill_address_attributes][:phone] - end - end - - def set_payment_amount - return unless @order_params[:payments_attributes] - - @order_params[:payments_attributes].first[:amount] = @order.total + @order_params ||= Checkout::Params.new(@order, params).call end def redirect_to_step diff --git a/app/services/checkout/params.rb b/app/services/checkout/params.rb new file mode 100644 index 0000000000..51d0dcb780 --- /dev/null +++ b/app/services/checkout/params.rb @@ -0,0 +1,63 @@ +# frozen_string_literal: true + +module Checkout + class Params + def initialize(order, params) + @params = params + @order = order + end + + def call + return {} unless params[:order] + + apply_strong_parameters + set_address_details + set_payment_amount + + @order_params + end + + private + + attr_reader :params, :order + + def apply_strong_parameters + @order_params = params.require(:order).permit( + :email, :shipping_method_id, :special_instructions, + bill_address_attributes: ::PermittedAttributes::Address.attributes, + ship_address_attributes: ::PermittedAttributes::Address.attributes, + payments_attributes: [:payment_method_id] + ) + end + + def set_address_details + return unless addresses_present? + + if params[:ship_address_same_as_billing] + set_ship_address_from_bill_address + else + set_basic_details + end + end + + def set_payment_amount + return unless @order_params[:payments_attributes] + + @order_params[:payments_attributes].first[:amount] = order.total + end + + def addresses_present? + @order_params[:ship_address_attributes] && @order_params[:bill_address_attributes] + end + + def set_ship_address_from_bill_address + @order_params[:ship_address_attributes] = @order_params[:bill_address_attributes] + end + + def set_basic_details + [:firstname, :lastname, :phone].each do |attr| + @order_params[:ship_address_attributes][attr] = @order_params[:bill_address_attributes][attr] + end + end + end +end