Class: Spree::Admin::PaymentsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/spree/admin/payments_controller.rb

Instance Method Summary collapse

Methods included from SetsUserLanguageLocaleKey

#set_user_language_locale_key

Instance Method Details

#createObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/spree/admin/payments_controller.rb', line 28

def create
  @payment = PaymentCreate.new(@order, object_params).build
  if @payment.payment_method.source_required? && params[:card].present? && params[:card] != 'new'
    @payment.source = @payment.payment_method.payment_source_class.find_by(id: params[:card])
  end

  begin
    if @payment.save
      if @order.completed?
        # If the order was already complete then go ahead and process the payment
        # (auth and/or capture depending on payment method configuration)
        @payment.process! if @payment.checkout?
      else
        # Transition order as far as it will go.
        while @order.next; end
      end

      flash[:success] = flash_message_for(@payment, :successfully_created)
      redirect_to admin_order_payments_path(@order)
    else
      flash[:error] = t('spree.payment_could_not_be_created')
      render :new
    end
  rescue Spree::Core::GatewayError => error
    flash[:error] = error.message.to_s
    redirect_to new_admin_order_payment_path(@order)
  end
end

#fireObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/spree/admin/payments_controller.rb', line 57

def fire
  return unless (event = params[:e]) && @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.send("#{event}!")
    flash[:success] = t('spree.payment_updated')
  else
    flash[:error] = t('spree.cannot_perform_operation')
  end
rescue Spree::Core::GatewayError => ge
  flash[:error] = ge.message.to_s
ensure
  redirect_to admin_order_payments_path(@order)
end

#indexObject



18
19
20
21
22
# File 'app/controllers/spree/admin/payments_controller.rb', line 18

def index
  @payments = @order.payments.includes(refunds: :reason)
  @refunds = @payments.flat_map(&:refunds)
  redirect_to new_admin_order_payment_url(@order) if @payments.empty?
end

#newObject



24
25
26
# File 'app/controllers/spree/admin/payments_controller.rb', line 24

def new
  @payment = @order.payments.build(amount: @order.outstanding_balance)
end