Class: Admin::PaymentsController

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

Instance Method Summary collapse

Instance Method Details

#createObject



21
22
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
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/admin/payments_controller.rb', line 21

def create
  @payment = @order.payments.build(object_params)
  if @payment.payment_method.is_a?(Gateway) && @payment.payment_method.payment_profiles_supported? && params[:card].present? and params[:card] != 'new'
    @payment.source = Creditcard.find_by_id(params[:card])
  end

  begin
    unless @payment.save
      respond_with(@payment) { |format| format.html { redirect_to admin_order_payments_path(@order) } }
          #render :json => @payment.to_json, :status => 201

      return
    end

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

      respond_with(@payment) { |format| format.html { redirect_to admin_order_payments_path(@order) } }
          #render :json => @payment.to_json, :status => 201

    else
      #This is the first payment (admin created order)
      until @order.completed?
        @order.next!
      end
      flash.notice = t('new_order_completed')
      respond_with(@payment) { |format| format.html { redirect_to admin_order_url(@order) } }
          #render :json => @payment.to_json, :status => 201

    end

  rescue Spree::GatewayError => e
    flash[:error] = "#{e.message}"
    
    respond_with(@payment) { |format| format.html { redirect_to new_admin_payment_path(@order) } }
       #render :json => @payment.to_json, :status => 201

  end
end

#fireObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/admin/payments_controller.rb', line 62

def fire
  # TODO: consider finer-grained control for this type of action (right now anyone in admin role can perform)
  return unless event = params[:e] and @payment.payment_source
  if @payment.payment_source.send("#{event}", @payment)
    flash.notice = t('payment_updated')
    #render :text => "sucess payment updated"
  else
    flash[:error] = t('cannot_perform_operation')
          #render :text => "payment updated failed "

  end
rescue Spree::GatewayError => ge
  flash[:error] = "#{ge.message}"
ensure
  respond_with(@payment) { |format| format.html { redirect_to admin_order_payments_path(@order) } }
end

#indexObject



8
9
10
11
12
13
# File 'app/controllers/admin/payments_controller.rb', line 8

def index
  @payments = @order.payments
 #render :json => @payments.to_json, :status => 201
 
 respond_with(@payments)
end

#newObject



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

def new
  @payment = @order.payments.build
  #render :json => @payment.to_json, :status => 201
  respond_with(@payment)
end