Class: Spree::Admin::PaymentsController

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

Instance Method Summary collapse

Instance Method Details

#createObject



26
27
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
# File 'app/controllers/spree/admin/payments_controller.rb', line 26

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



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

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



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

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



22
23
24
# File 'app/controllers/spree/admin/payments_controller.rb', line 22

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