mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Moved checkout services into a specific folder under app/services
This commit is contained in:
@@ -42,7 +42,7 @@ class CheckoutController < Spree::StoreController
|
||||
end
|
||||
|
||||
def update
|
||||
params_adapter = CheckoutFormDataAdapter.new(params, @order, spree_current_user)
|
||||
params_adapter = Checkout::FormDataAdapter.new(params, @order, spree_current_user)
|
||||
return update_failed unless @order.update_attributes(params_adapter.order_params)
|
||||
|
||||
fire_event('spree.checkout.update')
|
||||
@@ -169,7 +169,7 @@ class CheckoutController < Spree::StoreController
|
||||
end
|
||||
|
||||
def redirect_to_payment_gateway
|
||||
redirect_path = CheckoutPaymentRedirect.new(params).path
|
||||
redirect_path = Checkout::PaymentRedirect.new(params).path
|
||||
return if redirect_path.blank?
|
||||
|
||||
render json: { path: redirect_path }, status: :ok
|
||||
|
||||
68
app/services/checkout/form_data_adapter.rb
Normal file
68
app/services/checkout/form_data_adapter.rb
Normal file
@@ -0,0 +1,68 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Adapts checkout form data (params) so that the order can be directly saved to the database
|
||||
module Checkout
|
||||
class FormDataAdapter
|
||||
attr_reader :shipping_method_id
|
||||
|
||||
def initialize(params, order, current_user)
|
||||
@params = params
|
||||
@order = order
|
||||
@current_user = current_user
|
||||
|
||||
move_payment_source_to_payment_attributes!
|
||||
|
||||
set_amount_in_payments_attributes
|
||||
|
||||
construct_saved_card_attributes if @params[:order][:existing_card_id]
|
||||
|
||||
@shipping_method_id = @params[:order].delete(:shipping_method_id)
|
||||
end
|
||||
|
||||
def order_params
|
||||
@params[:order]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# For payment step, filter order parameters to produce the expected
|
||||
# nested attributes for a single payment and its source,
|
||||
# discarding attributes for payment methods other than the one selected
|
||||
def move_payment_source_to_payment_attributes!
|
||||
return unless @params[:payment_source].present? &&
|
||||
payment_source_params = delete_payment_source_params!
|
||||
|
||||
@params[:order][:payments_attributes].first[:source_attributes] = payment_source_params
|
||||
end
|
||||
|
||||
def delete_payment_source_params!
|
||||
params.delete(:payment_source)[
|
||||
@params[:order][:payments_attributes].first[:payment_method_id].underscore
|
||||
]
|
||||
end
|
||||
|
||||
def set_amount_in_payments_attributes
|
||||
return unless @params[:order][:payments_attributes]
|
||||
|
||||
@params[:order][:payments_attributes].first[:amount] = @order.total
|
||||
end
|
||||
|
||||
def construct_saved_card_attributes
|
||||
existing_card_id = @params[:order].delete(:existing_card_id)
|
||||
return if existing_card_id.blank?
|
||||
|
||||
move_to_payment_attributes(existing_card_id)
|
||||
|
||||
@params[:order][:payments_attributes].first.delete :source_attributes
|
||||
end
|
||||
|
||||
def move_to_payment_attributes(existing_card_id)
|
||||
credit_card = Spree::CreditCard.find(existing_card_id)
|
||||
if credit_card.try(:user_id).blank? || credit_card.user_id != @current_user.try(:id)
|
||||
raise Spree::Core::GatewayError, I18n.t(:invalid_credit_card)
|
||||
end
|
||||
|
||||
@params[:order][:payments_attributes].first[:source] = credit_card
|
||||
end
|
||||
end
|
||||
end
|
||||
27
app/services/checkout/payment_redirect.rb
Normal file
27
app/services/checkout/payment_redirect.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Provides the redirect path if a redirect to the payment gateway is needed
|
||||
module Checkout
|
||||
class PaymentRedirect
|
||||
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
|
||||
end
|
||||
@@ -1,66 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Adapts checkout form data (params) so that the order can be directly saved to the database
|
||||
class CheckoutFormDataAdapter
|
||||
attr_reader :shipping_method_id
|
||||
|
||||
def initialize(params, order, current_user)
|
||||
@params = params
|
||||
@order = order
|
||||
@current_user = current_user
|
||||
|
||||
move_payment_source_to_payment_attributes!
|
||||
|
||||
set_amount_in_payments_attributes
|
||||
|
||||
construct_saved_card_attributes if @params[:order][:existing_card_id]
|
||||
|
||||
@shipping_method_id = @params[:order].delete(:shipping_method_id)
|
||||
end
|
||||
|
||||
def order_params
|
||||
@params[:order]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# For payment step, filter order parameters to produce the expected
|
||||
# nested attributes for a single payment and its source,
|
||||
# discarding attributes for payment methods other than the one selected
|
||||
def move_payment_source_to_payment_attributes!
|
||||
return unless @params[:payment_source].present? &&
|
||||
payment_source_params = delete_payment_source_params!
|
||||
|
||||
@params[:order][:payments_attributes].first[:source_attributes] = payment_source_params
|
||||
end
|
||||
|
||||
def delete_payment_source_params!
|
||||
params.delete(:payment_source)[
|
||||
@params[:order][:payments_attributes].first[:payment_method_id].underscore
|
||||
]
|
||||
end
|
||||
|
||||
def set_amount_in_payments_attributes
|
||||
return unless @params[:order][:payments_attributes]
|
||||
|
||||
@params[:order][:payments_attributes].first[:amount] = @order.total
|
||||
end
|
||||
|
||||
def construct_saved_card_attributes
|
||||
existing_card_id = @params[:order].delete(:existing_card_id)
|
||||
return if existing_card_id.blank?
|
||||
|
||||
move_to_payment_attributes(existing_card_id)
|
||||
|
||||
@params[:order][:payments_attributes].first.delete :source_attributes
|
||||
end
|
||||
|
||||
def move_to_payment_attributes(existing_card_id)
|
||||
credit_card = Spree::CreditCard.find(existing_card_id)
|
||||
if credit_card.try(:user_id).blank? || credit_card.user_id != @current_user.try(:id)
|
||||
raise Spree::Core::GatewayError, I18n.t(:invalid_credit_card)
|
||||
end
|
||||
|
||||
@params[:order][:payments_attributes].first[:source] = credit_card
|
||||
end
|
||||
end
|
||||
@@ -1,25 +0,0 @@
|
||||
# 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