<commit_before>class PaymentDetail < ActiveRecord::Base
  include ActionView::Helpers::NumberHelper

  belongs_to :backer

  def update_from_service
    if self.backer.payment_method == 'MoIP'
      response = request_to_moip
      process_moip_response(response) if response.present?
    elsif self.backer.payment_method == 'PayPal'
      response = PaypalApi.transaction_details(self.backer.key)
      process_paypal_response(response) if response.present?
    end
  end

  def process_paypal_response(response)
    # For moment only attribute persisted in database is the
    # service fee.
    self.update_attributes(response)
  end

  def process_moip_response(response)
    if response["Autorizacao"].present?
      authorized  = response["Autorizacao"]

      self.payer_name  = authorized["Pagador"]["Nome"]
      self.payer_email = authorized["Pagador"]["Email"]
      self.city        = authorized["EnderecoCobranca"]["Cidade"]
      self.uf          = authorized["EnderecoCobranca"]["Estado"]

      if authorized["Pagamento"].present?
        payment                      = authorized["Pagamento"]
        if payment.kind_of?(Array) && payment.first.present?
            self.payment_method          = payment.first["FormaPagamento"]
            self.net_amount              = payment.first["ValorLiquido"].to_f
            self.total_amount            = payment.first["TotalPago"].to_f
            self.service_tax_amount      = payment.first["TaxaMoIP"].to_f
            self.backer_amount_tax       = payment.first["TaxaParaPagador"].to_f
            self.payment_status          = payment.first["Status"]
            self.service_code            = payment.first["CodigoMoIP"]
            self.institution_of_payment  = payment.first["InstituicaoPagamento"]
            self.payment_date            = payment.first["Data"]
          else
            self.payment_method          = payment["FormaPagamento"]
            self.net_amount              = payment["ValorLiquido"].to_f
            self.total_amount            = payment["TotalPago"].to_f
            self.service_tax_amount      = payment["TaxaMoIP"].to_f
            self.backer_amount_tax       = payment["TaxaParaPagador"].to_f
            self.payment_status          = payment["Status"]
            self.service_code            = payment["CodigoMoIP"]
            self.institution_of_payment  = payment["InstituicaoPagamento"]
            self.payment_date            = payment["Data"]
        end
      end

      self.save!
    end
  end

  def request_to_moip
    MoIP::Client.query(backer.payment_token)
  rescue
    []
  end

  def display_service_tax
    number_to_currency service_tax_amount, :unit => "R$", :precision => 2, :delimiter => '.'
  end

  def display_net_amount
    number_to_currency net_amount, :unit => "R$", :precision => 2, :delimiter => '.'
  end

  def display_total_amount
    number_to_currency total_amount, :unit => "R$", :precision => 2, :delimiter => '.'
  end

  def display_payment_date
    I18n.l(payment_date, :format => :simple) if payment_date.present?
  end

end
<commit_msg>send notification when have exception into process moip request<commit_after>class PaymentDetail < ActiveRecord::Base
  include ActionView::Helpers::NumberHelper

  belongs_to :backer

  def update_from_service
    if self.backer.payment_method == 'MoIP'
      response = request_to_moip
      process_moip_response(response) if response.present?
    elsif self.backer.payment_method == 'PayPal'
      response = PaypalApi.transaction_details(self.backer.key)
      process_paypal_response(response) if response.present?
    end
  end

  def process_paypal_response(response)
    # For moment only attribute persisted in database is the
    # service fee.
    self.update_attributes(response)
  end

  def process_moip_response(response)
    begin
      if response["Autorizacao"].present?
        authorized  = response["Autorizacao"]

        self.payer_name  = authorized["Pagador"]["Nome"]
        self.payer_email = authorized["Pagador"]["Email"]
        self.city        = authorized["EnderecoCobranca"]["Cidade"]
        self.uf          = authorized["EnderecoCobranca"]["Estado"]

        if authorized["Pagamento"].present?
          payment                      = authorized["Pagamento"]
          if payment.kind_of?(Array) && payment.first.present?
            self.payment_method          = payment.first["FormaPagamento"]
            self.net_amount              = payment.first["ValorLiquido"].to_f
            self.total_amount            = payment.first["TotalPago"].to_f
            self.service_tax_amount      = payment.first["TaxaMoIP"].to_f
            self.backer_amount_tax       = payment.first["TaxaParaPagador"].to_f
            self.payment_status          = payment.first["Status"]
            self.service_code            = payment.first["CodigoMoIP"]
            self.institution_of_payment  = payment.first["InstituicaoPagamento"]
            self.payment_date            = payment.first["Data"]
          else
            self.payment_method          = payment["FormaPagamento"]
            self.net_amount              = payment["ValorLiquido"].to_f
            self.total_amount            = payment["TotalPago"].to_f
            self.service_tax_amount      = payment["TaxaMoIP"].to_f
            self.backer_amount_tax       = payment["TaxaParaPagador"].to_f
            self.payment_status          = payment["Status"]
            self.service_code            = payment["CodigoMoIP"]
            self.institution_of_payment  = payment["InstituicaoPagamento"]
            self.payment_date            = payment["Data"]
          end
        end

        self.save!
      end
    rescue Exception => e
      Airbrake.notify({ :error_class => "Payment Detail Error [process moip response]", :error_message => "Error: #{e.inspect}", :parameters => params}) rescue nil
    end
  end

  def request_to_moip
    MoIP::Client.query(backer.payment_token)
  rescue
    []
  end

  def display_service_tax
    number_to_currency service_tax_amount, :unit => "R$", :precision => 2, :delimiter => '.'
  end

  def display_net_amount
    number_to_currency net_amount, :unit => "R$", :precision => 2, :delimiter => '.'
  end

  def display_total_amount
    number_to_currency total_amount, :unit => "R$", :precision => 2, :delimiter => '.'
  end

  def display_payment_date
    I18n.l(payment_date, :format => :simple) if payment_date.present?
  end

end

{'fraction_non_alphanumeric': 0.10271856657398826, 'fraction_numerical': 0.0015446400988569664, 'mean_word_length': 2.5286103542234333, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3922716', 'n_tokens_mistral': 1901, 'n_tokens_neox': 1903, 'n_words': 326}