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.
This commit is contained in:
Gaetan Craig-Riou
2023-10-18 09:45:05 +11:00
parent d0e38c8d10
commit b09054a76a
3 changed files with 54 additions and 2 deletions

View File

@@ -13,6 +13,10 @@ module CheckoutSteps
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"
@@ -39,9 +43,19 @@ module CheckoutSteps
def check_step
case @order.state
when "cart", "address", "delivery"
redirect_to checkout_step_path(:details) unless params[:step] == "details"
redirect_to checkout_step_path(:details) unless details_step?
when "payment"
redirect_to checkout_step_path(:payment) if params[:step] == "summary"
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

View File

@@ -24,6 +24,8 @@ class SplitCheckoutController < ::BaseController
def edit
redirect_to_step_based_on_order unless params[:step]
update_order_state if params[:step]
check_step if params[:step]
return if available_shipping_methods.any?

View File

@@ -67,6 +67,42 @@ describe SplitCheckoutController, type: :controller do
expect(response).to redirect_to checkout_step_path(:payment)
end
end
context "when order state is 'confirmation'" do
before do
order.update!(state: "confirmation")
end
context "when loading payment step" do
it "updates the order state to payment" do
get :edit, params: { step: "payment" }
expect(order.reload.state).to eq("payment")
end
end
context "when loading address step" do
it "updates the order state to address" do
get :edit, params: { step: "details" }
expect(order.reload.state).to eq("address")
end
end
end
context "when order state is 'payment'" do
context "when loading address step" do
before do
order.update!(state: "payment")
end
it "updates the order state to address" do
get :edit, params: { step: "details" }
expect(order.reload.state).to eq("address")
end
end
end
end
end