Class: CatarseMoip::NotificationsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/catarse_moip/notifications_controller.rb

Defined Under Namespace

Classes: TransactionStatus

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#paymentObject

Returns the value of attribute payment.



6
7
8
# File 'app/controllers/catarse_moip/notifications_controller.rb', line 6

def payment
  @payment
end

Instance Method Details

#createObject



24
25
26
27
28
29
# File 'app/controllers/catarse_moip/notifications_controller.rb', line 24

def create
  process_moip_message
  return render :nothing => true, :status => 200
rescue Exception => e
  return render :text => "#{e.inspect}: #{e.message} recebemos: #{params}", :status => 422
end

#process_moip_messageObject



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
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/catarse_moip/notifications_controller.rb', line 35

def process_moip_message
  payment.with_lock do
    payment_notification = payment.payment_notifications.create({
      contribution: payment.contribution,
      extra_data: JSON.parse(
        params.to_json.force_encoding('iso-8859-1').encode('utf-8'))
    })

    payment_id = (payment.gateway_id.gsub(".", "").to_i rescue 0)

    if payment_id <= params[:cod_moip].to_i
      payment.update_attributes payment_id: params[:cod_moip]

      if (params[:valor].to_i/100.0) < payment.value && params[:valor]
        #return payment.invalid! unless payment.invalid_payment?
        return
      end

      case params[:status_pagamento].to_i
      when TransactionStatus::PROCESS
        payment_notification.deliver_process_notification
      when TransactionStatus::AUTHORIZED, TransactionStatus::FINISHED
        payment.pay! unless payment.paid?
      when TransactionStatus::WRITTEN_BACK, TransactionStatus::REFUNDED
        payment.refund! unless payment.refunded?
      when TransactionStatus::CANCELED
        unless payment.refused?
          payment.refuse!
          if payment.slip_payment?
            payment_notification.deliver_slip_canceled_notification
          end
        end
      end
    end
  end
end