Class: Spree::Admin::PaymentsController

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

Instance Method Summary collapse

Instance Method Details

#createObject



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

def create
  invoke_callbacks(:create, :before)
  @payment ||= @order.payments.build(object_params)
  if @payment.payment_method.source_required? && params[:card].present? and params[:card] != 'new'
    @payment.source = @payment.payment_method.payment_source_class.find_by_id(params[:card])
  end

  begin
    if @payment.save
      invoke_callbacks(:create, :after)
      # Transition order as far as it will go.
      while @order.next; end
      # If "@order.next" didn't trigger payment processing already (e.g. if the order was
      # already complete) then trigger it manually now
      @payment.process! if @order.completed? && @payment.checkout?
      flash[:success] = flash_message_for(@payment, :successfully_created)
      redirect_to admin_order_payments_path(@order)
    else
      invoke_callbacks(:create, :fails)
      flash[:error] = Spree.t(:payment_could_not_be_created)
      render :new
    end
  rescue Spree::Core::GatewayError => e
    invoke_callbacks(:create, :fails)
    flash[:error] = "#{e.message}"
    redirect_to new_admin_order_payment_path(@order)
  end
end

#fireObject



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

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

#indexObject



13
14
15
16
17
# File 'app/controllers/spree/admin/payments_controller.rb', line 13

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



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

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