mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Extract paypal redirect logic to service class
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
25
app/services/checkout_payment_redirect.rb
Normal file
25
app/services/checkout_payment_redirect.rb
Normal 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
|
||||
Reference in New Issue
Block a user