|
| 1 | +module CatarseMoip::Payment |
| 2 | + class MoipController < ApplicationController |
| 3 | + before_filter :setup_moip |
| 4 | + |
| 5 | + def checkout |
| 6 | + @backer = current_user.backs.not_confirmed.find params[:id] |
| 7 | + begin |
| 8 | + response = MoIP.checkout(payment_info) |
| 9 | + @backer.update_attribute :payment_token, response["Token"] |
| 10 | + session[:_payment_token] = response["Token"] |
| 11 | + |
| 12 | + redirect_to MoIP.moip_page(response["Token"]) |
| 13 | + rescue Exception => e |
| 14 | + Airbrake.notify({ :error_class => "Checkout MOIP Error", :error_message => "MOIP Error: #{e.inspect}", :parameters => params}) rescue nil |
| 15 | + Rails.logger.info "-----> #{e.inspect}" |
| 16 | + flash[:failure] = t('projects.backers.checkout.moip_error') |
| 17 | + return redirect_to main_app.new_project_backer_path(@backer.project) # |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + protected |
| 22 | + |
| 23 | + def setup_moip |
| 24 | + MoIP.setup do |config| |
| 25 | + config.uri = (::Configuration[:moip_uri] || 'https://moip.com.br/') |
| 26 | + config.token = ::Configuration[:moip_token] |
| 27 | + config.key = ::Configuration[:moip_key] |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + def payer_info |
| 32 | + { |
| 33 | + nome: current_user.full_name, |
| 34 | + email: current_user.email, |
| 35 | + logradouro: current_user.address_street, |
| 36 | + numero: current_user.address_number, |
| 37 | + complemento: current_user.address_complement, |
| 38 | + bairro: current_user.address_neighbourhood, |
| 39 | + cidade: current_user.address_city, |
| 40 | + estado: current_user.address_state, |
| 41 | + pais: 'BRA', |
| 42 | + cep: current_user.address_zip_code, |
| 43 | + tel_fixo: current_user.phone_number |
| 44 | + } |
| 45 | + end |
| 46 | + |
| 47 | + def payment_info |
| 48 | + { |
| 49 | + valor: "%0.0f" % (@backer.value), |
| 50 | + id_proprio: @backer.key, |
| 51 | + razao: "Apoio para o projeto '#{@backer.project.name}'", |
| 52 | + forma: "BoletoBancario", |
| 53 | + dias_expiracao: 2, |
| 54 | + pagador: payer_info, |
| 55 | + url_retorno: main_app.thank_you_url |
| 56 | + } |
| 57 | + end |
| 58 | + end |
| 59 | +end |
0 commit comments