Class: CatarseMoip::MoipController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- CatarseMoip::MoipController
- Defined in:
- app/controllers/catarse_moip/moip_controller.rb
Defined Under Namespace
Classes: TransactionStatus
Instance Attribute Summary collapse
-
#contribution ⇒ Object
Returns the value of attribute contribution.
Instance Method Summary collapse
- #create_notification ⇒ Object
- #first_update_contribution ⇒ Object
- #get_moip_token ⇒ Object
- #js ⇒ Object
- #moip_response ⇒ Object
- #process_moip_message ⇒ Object
- #review ⇒ Object
Instance Attribute Details
#contribution ⇒ Object
Returns the value of attribute contribution.
6 7 8 |
# File 'app/controllers/catarse_moip/moip_controller.rb', line 6 def contribution @contribution end |
Instance Method Details
#create_notification ⇒ Object
24 25 26 27 28 29 30 |
# File 'app/controllers/catarse_moip/moip_controller.rb', line 24 def create_notification @contribution = PaymentEngines.find_payment key: params[:id_transacao] if @contribution.payment_method == 'MoIP' || @contribution.payment_method.nil? return render :nothing => true, :status => 200 rescue Exception => e return render :text => "#{e.inspect}: #{e.message} recebemos: #{params}", :status => 422 end |
#first_update_contribution ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'app/controllers/catarse_moip/moip_controller.rb', line 101 def first_update_contribution response = ::MoIP.query(contribution.payment_token) if response && response["Autorizacao"] params = response["Autorizacao"]["Pagamento"] params = params.first unless params.respond_to?(:key) contribution.with_lock do if params["Status"] == "Autorizado" contribution.confirm! elsif contribution.pending? contribution.waiting! end contribution.update_attributes({ :payment_id => params["CodigoMoIP"], :payment_choice => params["FormaPagamento"], :payment_method => 'MoIP', :payment_service_fee => params["TaxaMoIP"] }) if params end end end |
#get_moip_token ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'app/controllers/catarse_moip/moip_controller.rb', line 54 def get_moip_token @contribution = PaymentEngines.find_payment id: params[:id], user_id: current_user.id ::MoipTransparente::Config.test = (PaymentEngines.configuration[:moip_test] == 'true') ::MoipTransparente::Config.access_token = PaymentEngines.configuration[:moip_token] ::MoipTransparente::Config.access_key = PaymentEngines.configuration[:moip_key] @moip = ::MoipTransparente::Checkout.new invoice = { razao: "Apoio para o projeto '#{contribution.project.name}'", id: contribution.key, total: contribution.value.to_s, acrescimo: '0.00', desconto: '0.00', cliente: { id: contribution.user.id, nome: contribution.payer_name, email: contribution.payer_email, logradouro: "#{contribution.address_street}, #{contribution.address_number}", complemento: contribution.address_complement, bairro: contribution.address_neighbourhood, cidade: contribution.address_city, uf: contribution.address_state, cep: contribution.address_zip_code, telefone: contribution.address_phone_number } } response = @moip.get_token(invoice) session[:thank_you_id] = contribution.project.id contribution.update_column :payment_token, response[:token] if response and response[:token] render json: { get_token_response: response, moip: @moip, widget_tag: { tag_id: 'MoipWidget', token: response[:token], callback_success: 'checkoutSuccessful', callback_error: 'checkoutFailure' } } end |
#js ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/controllers/catarse_moip/moip_controller.rb', line 32 def js tries = 0 begin @moip = ::MoipTransparente::Checkout.new render :text => open(@moip.get_javascript_url).set_encoding('ISO-8859-1').read.encode('utf-8') rescue Exception => e tries += 1 retry unless tries > 3 raise e end end |
#moip_response ⇒ Object
48 49 50 51 52 |
# File 'app/controllers/catarse_moip/moip_controller.rb', line 48 def moip_response @contribution = PaymentEngines.find_payment id: params[:id], user_id: current_user.id first_update_contribution unless params[:response]['StatusPagamento'] == 'Falha' render nothing: true, status: 200 end |
#process_moip_message ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'app/controllers/catarse_moip/moip_controller.rb', line 124 def contribution.with_lock do first_update_contribution if contribution.payment_method.nil? payment_notification = PaymentEngines.create_payment_notification contribution_id: contribution.id, extra_data: JSON.parse(params.to_json.force_encoding('iso-8859-1').encode('utf-8')) payment_id = (contribution.payment_id.gsub(".", "").to_i rescue 0) if payment_id <= params[:cod_moip].to_i contribution.update_attributes payment_id: params[:cod_moip] if (params[:valor].to_i/100.0) < contribution.value && params[:valor] return contribution.invalid! unless contribution.invalid_payment? end case params[:status_pagamento].to_i when TransactionStatus::PROCESS payment_notification.deliver_process_notification when TransactionStatus::AUTHORIZED, TransactionStatus::FINISHED contribution.confirm! unless contribution.confirmed? when TransactionStatus::WRITTEN_BACK, TransactionStatus::REFUNDED contribution.refund! unless contribution.refunded? when TransactionStatus::CANCELED unless contribution.canceled? contribution.cancel! if contribution.payment_choice.downcase == 'boletobancario' payment_notification.deliver_slip_canceled_notification end end end end end end |
#review ⇒ Object
44 45 46 |
# File 'app/controllers/catarse_moip/moip_controller.rb', line 44 def review @moip = ::MoipTransparente::Checkout.new end |