From 564b33479fe0b8da817f0f6f13d19864995bcfd0 Mon Sep 17 00:00:00 2001 From: kipsang Date: Thu, 16 Apr 2026 13:05:11 +0300 Subject: [PATCH] Refactor checkout redirection to use a unified path helper for improved clarity and maintainability --- .../spree/api/v1/ipay_controller.rb | 10 +++++++--- .../spree/checkout_controller_decorator.rb | 20 +++++++++++-------- .../spree/gateway_callbacks_controller.rb | 2 +- app/controllers/spree/ipay_controller.rb | 10 +++++++--- .../spree/ipay_controller_decorator.rb | 13 ++++++++---- 5 files changed, 36 insertions(+), 19 deletions(-) diff --git a/app/controllers/spree/api/v1/ipay_controller.rb b/app/controllers/spree/api/v1/ipay_controller.rb index ffe9864..6258add 100644 --- a/app/controllers/spree/api/v1/ipay_controller.rb +++ b/app/controllers/spree/api/v1/ipay_controller.rb @@ -180,7 +180,7 @@ def return # Here you might want to implement a status check with iPay # For now, we'll just redirect to payment info page - redirect_to spree.checkout_state_path(:payment), + redirect_to checkout_state_path_for(order, 'payment'), notice: 'We are still processing your payment. Please check back soon.' return end @@ -188,13 +188,13 @@ def return # If payment failed if @payment.failed? || @payment.void? - redirect_to spree.checkout_state_path(:payment), + redirect_to checkout_state_path_for(order, 'payment'), alert: 'Payment was not completed. Please try again or use a different payment method.' return end # Default fallback - redirect_to spree.checkout_state_path(order.state), + redirect_to checkout_state_path_for(order), notice: 'Please complete your order.' rescue StandardError redirect_to spree.root_path, @@ -220,6 +220,10 @@ def status private + def checkout_state_path_for(order, state = order.state) + spree.checkout_state_path(order.token, state) + end + # Dummy method to satisfy Spree API controller expectations def try_spree_current_user nil diff --git a/app/controllers/spree/checkout_controller_decorator.rb b/app/controllers/spree/checkout_controller_decorator.rb index 3c50474..a255214 100644 --- a/app/controllers/spree/checkout_controller_decorator.rb +++ b/app/controllers/spree/checkout_controller_decorator.rb @@ -59,14 +59,14 @@ def handle_ipay_redirect error_message = Rails.env.development? ? e.message : 'Unable to process payment. Please try again.' respond_to do |format| - format.html { redirect_to checkout_state_path(@order.state), error: error_message } + format.html { redirect_to checkout_state_path_for(@order), error: error_message } format.json { render json: { status: 'error', message: error_message }, status: :unprocessable_entity } end end rescue StandardError => e respond_to do |format| format.html do - redirect_to checkout_state_path(:payment), error: "Payment processing failed: #{e.message}" + redirect_to checkout_state_path_for(@order, 'payment'), error: "Payment processing failed: #{e.message}" end format.json do render json: { @@ -89,9 +89,9 @@ def update respond_to do |format| format.html do if @order.next - redirect_to checkout_state_path(@order.state) + redirect_to checkout_state_path_for(@order) else - redirect_to checkout_state_path(@order.state) + redirect_to checkout_state_path_for(@order) end end @@ -156,19 +156,23 @@ def update end private + + def checkout_state_path_for(order, state = order.state) + checkout_state_path(order.token, state) + end def next_step_url_for(order, next_step) return unless next_step case next_step when 'address' - checkout_state_path('address') + checkout_state_path_for(order, 'address') when 'delivery' - checkout_state_path('delivery') + checkout_state_path_for(order, 'delivery') when 'payment' - checkout_state_path('payment') + checkout_state_path_for(order, 'payment') when 'confirm' - checkout_state_path('confirm') + checkout_state_path_for(order, 'confirm') when 'complete' order_path(order, order_token: order.guest_token) end diff --git a/app/controllers/spree/gateway_callbacks_controller.rb b/app/controllers/spree/gateway_callbacks_controller.rb index 33f21bb..7db3dbe 100644 --- a/app/controllers/spree/gateway_callbacks_controller.rb +++ b/app/controllers/spree/gateway_callbacks_controller.rb @@ -120,7 +120,7 @@ def confirm esc_heading = ERB::Util.html_escape(meta[:heading]) esc_color = ERB::Util.html_escape(meta[:color]) # assumed safe, as it's generated SVG markup esc_root_path = ERB::Util.html_escape(spree.root_path) - esc_payment_path = ERB::Util.html_escape(spree.checkout_state_path(order.state)) + esc_payment_path = ERB::Util.html_escape(spree.checkout_state_path(order.token, order.state)) # Build details table safely using helpers @details = helpers.content_tag(:table, diff --git a/app/controllers/spree/ipay_controller.rb b/app/controllers/spree/ipay_controller.rb index 66841d8..f627d1a 100644 --- a/app/controllers/spree/ipay_controller.rb +++ b/app/controllers/spree/ipay_controller.rb @@ -20,16 +20,20 @@ def interactive_checkout redirect_to redirect_url, allow_other_host: true else - redirect_to checkout_state_path(@order.state), alert: "Unable to process payment. Please try again." + redirect_to checkout_state_path_for(@order), alert: "Unable to process payment. Please try again." end rescue ActiveRecord::RecordNotFound => e redirect_to cart_path, alert: "Order not found." rescue => e - redirect_to checkout_state_path(@order&.state || :cart), - alert: "An error occurred while processing your payment. Please try again." + redirect_to(@order.present? ? checkout_state_path_for(@order) : cart_path, + alert: "An error occurred while processing your payment. Please try again.") end private + + def checkout_state_path_for(order, state = order.state) + checkout_state_path(order.token, state) + end # Extract callback parameters based on iPay documentation def extract_callback_params diff --git a/app/controllers/spree/ipay_controller_decorator.rb b/app/controllers/spree/ipay_controller_decorator.rb index 7ecca4e..5712d05 100644 --- a/app/controllers/spree/ipay_controller_decorator.rb +++ b/app/controllers/spree/ipay_controller_decorator.rb @@ -24,8 +24,7 @@ def interactive_checkout redirect_to redirect_url, allow_other_host: true else - redirect_to checkout_state_path(@order.state), - alert: "Unable to process payment. Please try again." + redirect_to checkout_state_path_for(@order), alert: "Unable to process payment. Please try again." end end @@ -57,8 +56,10 @@ def interactive_checkout rescue => e respond_to do |format| format.html do - redirect_to checkout_state_path(@order&.state || :cart), - alert: "An error occurred while processing your payment. Please try again." + redirect_to( + @order.present? ? checkout_state_path_for(@order) : cart_path, + alert: "An error occurred while processing your payment. Please try again." + ) end format.json do render json: { @@ -72,6 +73,10 @@ def interactive_checkout private + def checkout_state_path_for(order, state = order.state) + checkout_state_path(order.token, state) + end + def set_headers response.headers['Cache-Control'] = 'no-cache, no-store, max-age=0, must-revalidate' response.headers['Pragma'] = 'no-cache'