Files
openfoodnetwork/app/controllers/concerns/checkout_steps.rb
Gaetan Craig-Riou b09054a76a Update order state when moving back through checkout step
Some important logic happens after the order transition from one state
to another. In particular, voucher are recalculated. To fix any
inconsistency, we make sure the order is the state matching the checkout
step we are on.
2023-10-18 09:45:05 +11:00

62 lines
1.3 KiB
Ruby

# frozen_string_literal: true
module CheckoutSteps
extend ActiveSupport::Concern
private
def summary_step?
params[:step] == "summary"
end
def payment_step?
params[:step] == "payment"
end
def details_step?
params[:step] == "details"
end
def redirect_to_step_based_on_order
case @order.state
when "cart", "address", "delivery"
redirect_to checkout_step_path(:details)
when "payment"
redirect_to checkout_step_path(:payment)
when "confirmation"
redirect_to checkout_step_path(:summary)
else
redirect_to order_path(@order, order_token: @order.token)
end
end
def redirect_to_step
case params[:step]
when "details"
return redirect_to checkout_step_path(:payment)
when "payment"
return redirect_to checkout_step_path(:summary)
end
redirect_to_step_based_on_order
end
def check_step
case @order.state
when "cart", "address", "delivery"
redirect_to checkout_step_path(:details) unless details_step?
when "payment"
redirect_to checkout_step_path(:payment) if summary_step?
end
end
def update_order_state
if @order.state == "confirmation" && payment_step?
@order.back_to_payment
end
return unless @order.state.in?(["payment", "confirmation"]) && details_step?
@order.back_to_address
end
end