Extract paypal redirect logic to service class

This commit is contained in:
luisramos0
2020-02-04 19:42:45 +00:00
parent 4fbd2cfa52
commit 6ce50a5fa5
2 changed files with 30 additions and 9 deletions

View File

@@ -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

View File

@@ -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