From 6ce50a5fa585a1a49e42d6b2bd6c239c34324489 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Tue, 4 Feb 2020 19:42:45 +0000 Subject: [PATCH] Extract paypal redirect logic to service class --- app/controllers/checkout_controller.rb | 14 +++++-------- app/services/checkout_payment_redirect.rb | 25 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 app/services/checkout_payment_redirect.rb diff --git a/app/controllers/checkout_controller.rb b/app/controllers/checkout_controller.rb index c77b9d27d0..9abdc82a85 100644 --- a/app/controllers/checkout_controller.rb +++ b/app/controllers/checkout_controller.rb @@ -152,7 +152,7 @@ class CheckoutController < Spree::StoreController def checkout_workflow(shipping_method_id) while @order.state != "complete" if @order.state == "payment" - return if redirect_to_paypal_express_form_if_needed + return if redirect_to_payment_gateway end @order.select_shipping_method(shipping_method_id) if @order.state == "delivery" @@ -166,15 +166,11 @@ class CheckoutController < Spree::StoreController update_result end - def redirect_to_paypal_express_form_if_needed - return unless params[:order][:payments_attributes] + def redirect_to_payment_gateway + redirect_path = CheckoutPaymentRedirect.new(params).path + return if redirect_path.blank? - payment_method_id = params[:order][:payments_attributes].first[:payment_method_id] - payment_method = Spree::PaymentMethod.find(payment_method_id) - return unless payment_method.is_a?(Spree::Gateway::PayPalExpress) - - render json: { path: spree.paypal_express_path(payment_method_id: payment_method.id) }, - status: :ok + render json: { path: redirect_path }, status: :ok true end diff --git a/app/services/checkout_payment_redirect.rb b/app/services/checkout_payment_redirect.rb new file mode 100644 index 0000000000..27ff3eb4d2 --- /dev/null +++ b/app/services/checkout_payment_redirect.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +# Provides the redirect path if a redirect to the payment gateway is needed +class CheckoutPaymentRedirect + def initialize(params) + @params = params + end + + # Returns the path to the Paypal Express form if a redirect is needed + def path + return unless @params[:order][:payments_attributes] + + payment_method_id = @params[:order][:payments_attributes].first[:payment_method_id] + payment_method = Spree::PaymentMethod.find(payment_method_id) + return unless payment_method.is_a?(Spree::Gateway::PayPalExpress) + + spree_routes_helper.paypal_express_path(payment_method_id: payment_method.id) + end + + private + + def spree_routes_helper + Spree::Core::Engine.routes_url_helpers + end +end