Skip to content

Commit

Permalink
Merge pull request #4509 from luisramos0/backend_ctrl_payments
Browse files Browse the repository at this point in the history
Bring spree_backend payments controller to OFN
  • Loading branch information
luisramos0 authored Feb 4, 2020
2 parents d80554a + e192207 commit c4bf4f0
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 64 deletions.
8 changes: 3 additions & 5 deletions .rubocop_manual_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ Layout/LineLength:
- app/controllers/checkout_controller.rb
- app/controllers/spree/admin/adjustments_controller_decorator.rb
- app/controllers/spree/admin/orders_controller_decorator.rb
- app/controllers/spree/admin/payments_controller_decorator.rb
- app/controllers/spree/credit_cards_controller.rb
- app/controllers/spree/paypal_controller_decorator.rb
- app/controllers/stripe/callbacks_controller.rb
Expand Down Expand Up @@ -365,7 +364,8 @@ Metrics/AbcSize:
- app/controllers/spree/admin/orders_controller_decorator.rb
- app/controllers/spree/admin/overview_controller.rb
- app/controllers/spree/admin/payment_methods_controller.rb
- app/controllers/spree/admin/payments_controller_decorator.rb
- app/controllers/spree/admin/payments_controller.rb
- app/controllers/spree/admin/products_controller_decorator.rb
- app/controllers/spree/admin/reports_controller.rb
- app/controllers/spree/admin/resource_controller.rb
- app/controllers/spree/admin/products_controller.rb
Expand Down Expand Up @@ -496,7 +496,6 @@ Metrics/CyclomaticComplexity:
- app/controllers/admin/enterprise_fees_controller.rb
- app/controllers/admin/enterprises_controller.rb
- app/controllers/checkout_controller.rb
- app/controllers/spree/admin/payments_controller_decorator.rb
- app/controllers/spree/admin/taxons_controller.rb
- app/controllers/spree/orders_controller.rb
- app/helpers/checkout_helper.rb
Expand Down Expand Up @@ -527,7 +526,6 @@ Metrics/PerceivedComplexity:
- app/controllers/admin/enterprises_controller.rb
- app/controllers/api/variants_controller.rb
- app/controllers/checkout_controller.rb
- app/controllers/spree/admin/payments_controller_decorator.rb
- app/controllers/spree/admin/taxons_controller.rb
- app/controllers/spree/orders_controller.rb
- app/helpers/checkout_helper.rb
Expand Down Expand Up @@ -569,7 +567,7 @@ Metrics/MethodLength:
- app/controllers/spree/admin/image_settings_controller.rb
- app/controllers/spree/admin/orders/customer_details_controller_decorator.rb
- app/controllers/spree/admin/payment_methods_controller.rb
- app/controllers/spree/admin/payments_controller_decorator.rb
- app/controllers/spree/admin/payments_controller.rb
- app/controllers/spree/admin/reports_controller.rb
- app/controllers/spree/admin/resource_controller.rb
- app/controllers/spree/admin/products_controller.rb
Expand Down
129 changes: 129 additions & 0 deletions app/controllers/spree/admin/payments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# frozen_string_literal: true

module Spree
module Admin
class PaymentsController < Spree::Admin::BaseController
before_filter :load_order, except: [:show]
before_filter :load_payment, only: [:fire, :show]
before_filter :load_data
before_filter :can_transition_to_payment

respond_to :html

def index
@payments = @order.payments
redirect_to new_admin_order_payment_url(@order) if @payments.empty?
end

def new
@payment = @order.payments.build
end

def create
@payment = @order.payments.build(object_params)
load_payment_source

begin
unless @payment.save
redirect_to admin_order_payments_path(@order)
return
end

if @order.completed?
@payment.process!
flash[:success] = flash_message_for(@payment, :successfully_created)

redirect_to admin_order_payments_path(@order)
else
AdvanceOrderService.new(@order).call!

flash[:success] = Spree.t(:new_order_completed)
redirect_to edit_admin_order_url(@order)
end
rescue Spree::Core::GatewayError => e
flash[:error] = e.message.to_s
redirect_to new_admin_order_payment_path(@order)
end
end

# When a user fires an event, take them back to where they came from
# (we can't use respond_override because Spree no longer uses respond_with)
def fire
event = params[:e]
return unless event && @payment.payment_source

# Because we have a transition method also called void, we do this to avoid conflicts.
event = "void_transaction" if event == "void"
if @payment.public_send("#{event}!")
flash[:success] = t(:payment_updated)
else
flash[:error] = t(:cannot_perform_operation)
end
rescue Spree::Core::GatewayError => e
flash[:error] = e.message
ensure
redirect_to request.referer
end

private

def load_payment_source
if @payment.payment_method.is_a?(Spree::Gateway) &&
@payment.payment_method.payment_profiles_supported? &&
params[:card].present? &&
(params[:card] != 'new')
@payment.source = CreditCard.find_by_id(params[:card])
end
end

def object_params
if params[:payment] &&
params[:payment_source] &&
source_params = params.delete(:payment_source)[params[:payment][:payment_method_id]]
params[:payment][:source_attributes] = source_params
end
params[:payment]
end

def load_data
@amount = params[:amount] || load_order.total

# Only show payments for the order's distributor
@payment_methods = PaymentMethod.
available(:back_end).
select{ |pm| pm.has_distributor? @order.distributor }

@payment_method = if @payment && @payment.payment_method
@payment.payment_method
else
@payment_methods.first
end

@previous_cards = @order.credit_cards.with_payment_profile
end

# At this point admin should have passed through Customer Details step
# where order.next is called which leaves the order in payment step
#
# Orders in complete step also allows to access this controller
#
# Otherwise redirect user to that step
def can_transition_to_payment
return if @order.payment? || @order.complete?

flash[:notice] = Spree.t(:fill_in_customer_info)
redirect_to edit_admin_order_customer_url(@order)
end

def load_order
@order = Order.find_by_number!(params[:order_id])
authorize! action, @order
@order
end

def load_payment
@payment = Payment.find(params[:id])
end
end
end
end
59 changes: 0 additions & 59 deletions app/controllers/spree/admin/payments_controller_decorator.rb

This file was deleted.

11 changes: 11 additions & 0 deletions app/helpers/spree/admin/payments_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Spree
module Admin
module PaymentsHelper
def payment_method_name(payment)
# hack to allow us to retrieve the name of a "deleted" payment method
id = payment.payment_method_id
Spree::PaymentMethod.find_with_destroyed(id).name
end
end
end
end
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2988,6 +2988,8 @@ See the %{link} to find out more about %{sitename}'s features and to start using
zipcode: Postcode
weight: Weight (per kg)
error_user_destroy_with_orders: "Users with completed orders may not be deleted"
cannot_create_payment_without_payment_methods: "You cannot create a payment for an order without any payment methods defined."
please_define_payment_methods: "Please define some payment methods first."
options: "Options"

actions:
Expand Down
6 changes: 6 additions & 0 deletions config/routes/spree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@
end

resources :adjustments

resources :payments do
member do
put :fire
end
end
end

resources :users do
Expand Down

0 comments on commit c4bf4f0

Please sign in to comment.