<commit_before># coding: utf-8
module CatarsePagarme
  class CreditCardsController < CatarsePagarme::ApplicationController
    MAX_SOFT_DESCRIPTOR_LENGTH = 13

    def create
      transaction = CreditCardTransaction.new(credit_card_attributes, payment).charge!

      render json: { payment_status: transaction.status }
    rescue PagarMe::PagarMeError => e
      payment.destroy if payment.persisted? && !payment.gateway_id.present?

      render json: { payment_status: 'failed', message: e.message }
    end

    def get_installment_json
      render json: installments_for_json.to_json
    end

    def get_encryption_key_json
      render json: { key: CatarsePagarme.configuration.ecr_key }
    end

    protected

    def credit_card_attributes
      hash = {
        payment_method: 'credit_card',
        amount: delegator.value_with_installment_tax(get_installment),
        postback_url: ipn_pagarme_index_url(
          host: CatarsePagarme.configuration.host,
          subdomain: CatarsePagarme.configuration.subdomain,
          protocol: CatarsePagarme.configuration.protocol
        ),
        soft_descriptor: payment.project.permalink.gsub(/[\W\_]/, ' ')[0, MAX_SOFT_DESCRIPTOR_LENGTH],
        installments: get_installment,
        customer: {
          email: contribution.payer_email,
          name: contribution.payer_name,
          document_number: document_number,
          address: {
            street: contribution.address_street,
            neighborhood: neighborhood,
            zipcode: zip_code,
            street_number: address_number,
            complementary: contribution.address_complement,
            city: contribution.address_city,
            state: contribution.address_state,
            country: contribution.country.try(:name)
          },
          phone: {
            ddd: phone_matches.try(:[], 1),
            number: phone_matches.try(:[], 2)
          }
        },
        metadata: metadata_attributes,
        antifraud_metadata: af_metadata
      }

      if params[:card_hash].present?
        hash[:card_hash] = params[:card_hash]
      else
        hash[:card_id] = params[:card_id]
      end

      hash[:save_card] = (params[:save_card] == 'true')

      hash
    end

    def address_number
      international? ? 100 : contribution.address_number
    end

    def document_number
      international? ? '00000000000' : contribution.payer_document.gsub(/[-.\/_\s]/,'')
    end

    def phone_matches
      international? ? ['', '33', '33335555'] : contribution.address_phone_number.gsub(/[\s,-]/, '').match(/\((.*)\)(\d+)/)
    end

    def zip_code
      international? ? '00000000' : contribution.address_zip_code.gsub(/[-.]/, '')
    end

    def neighborhood
      international? ? 'international' : contribution.address_neighbourhood
    end

    def international?
      contribution.international?
    end

    def get_installment
      if payment.value.to_f < CatarsePagarme.configuration.minimum_value_for_installment.to_f
        1
      elsif params[:payment_card_installments].to_i > 0
        params[:payment_card_installments].to_i
      else
        1
      end
    end

    def installments_for_json
      if contribution.value.to_f >= CatarsePagarme.configuration.minimum_value_for_installment.to_f
        project = payment.project
        installments = payment.pagarme_delegator.get_installments['installments']
        collection = installments.map do |installment|
          installment_number = installment[0].to_i
          if installment_number <= (project.try(:total_installments) || CatarsePagarme.configuration.max_installments.to_i)
            amount = installment[1]['installment_amount'] / 100.0

            {amount: amount, number: installment_number}
          end
        end
      else
        collection = [{amount: payment.value, number: 1}]
      end
      collection.compact
    end

    def af_metadata
      {
        session_id: contribution.id,
        ip: contribution.user.current_sign_in_ip,
        platform: "web",
        register: {
          id: contribution.user.id,
          email: contribution.user.email,
          registered_at: contribution.user.created_at,
          login_source: "registered"
        },
        billing: {
          customer: {
            name: contribution.payer_name,
            document_number: contribution.payer_document.try(:gsub, /[-.\/_\s]/, ''),
            born_at: "",
            gender: ""
          },
          address: {
            country: contribution.country.try(:name),
            state: contribution.address_state,
            city: contribution.address_city,
            zipcode: contribution.address_zip_code,
            neighborhood: contribution.address_neighbourhood,
            street: contribution.address_street,
            street_number: contribution.address_number,
            complementary: contribution.address_complement,
            latitude: '',
            longitude: ''
          },
          card_holder_name: "",
          phone_numbers: [
            {
              ddi: "",
              ddd: phone_matches.try(:[], 1),
              number: phone_matches.try(:[], 2)
            }
          ]
        },
        contribution: [
          {
            contribution_id: contribution.id,
            project_name: payment.project.name,
            project_online: payment.project.online_at,<commit_msg>ids on antifraud_metadata should be a string<commit_after># coding: utf-8
module CatarsePagarme
  class CreditCardsController < CatarsePagarme::ApplicationController
    MAX_SOFT_DESCRIPTOR_LENGTH = 13

    def create
      transaction = CreditCardTransaction.new(credit_card_attributes, payment).charge!

      render json: { payment_status: transaction.status }
    rescue PagarMe::PagarMeError => e
      payment.destroy if payment.persisted? && !payment.gateway_id.present?

      render json: { payment_status: 'failed', message: e.message }
    end

    def get_installment_json
      render json: installments_for_json.to_json
    end

    def get_encryption_key_json
      render json: { key: CatarsePagarme.configuration.ecr_key }
    end

    protected

    def credit_card_attributes
      hash = {
        payment_method: 'credit_card',
        amount: delegator.value_with_installment_tax(get_installment),
        postback_url: ipn_pagarme_index_url(
          host: CatarsePagarme.configuration.host,
          subdomain: CatarsePagarme.configuration.subdomain,
          protocol: CatarsePagarme.configuration.protocol
        ),
        soft_descriptor: payment.project.permalink.gsub(/[\W\_]/, ' ')[0, MAX_SOFT_DESCRIPTOR_LENGTH],
        installments: get_installment,
        customer: {
          email: contribution.payer_email,
          name: contribution.payer_name,
          document_number: document_number,
          address: {
            street: contribution.address_street,
            neighborhood: neighborhood,
            zipcode: zip_code,
            street_number: address_number,
            complementary: contribution.address_complement,
            city: contribution.address_city,
            state: contribution.address_state,
            country: contribution.country.try(:name)
          },
          phone: {
            ddd: phone_matches.try(:[], 1),
            number: phone_matches.try(:[], 2)
          }
        },
        metadata: metadata_attributes,
        antifraud_metadata: af_metadata
      }

      if params[:card_hash].present?
        hash[:card_hash] = params[:card_hash]
      else
        hash[:card_id] = params[:card_id]
      end

      hash[:save_card] = (params[:save_card] == 'true')

      hash
    end

    def address_number
      international? ? 100 : contribution.address_number
    end

    def document_number
      international? ? '00000000000' : contribution.payer_document.gsub(/[-.\/_\s]/,'')
    end

    def phone_matches
      international? ? ['', '33', '33335555'] : contribution.address_phone_number.gsub(/[\s,-]/, '').match(/\((.*)\)(\d+)/)
    end

    def zip_code
      international? ? '00000000' : contribution.address_zip_code.gsub(/[-.]/, '')
    end

    def neighborhood
      international? ? 'international' : contribution.address_neighbourhood
    end

    def international?
      contribution.international?
    end

    def get_installment
      if payment.value.to_f < CatarsePagarme.configuration.minimum_value_for_installment.to_f
        1
      elsif params[:payment_card_installments].to_i > 0
        params[:payment_card_installments].to_i
      else
        1
      end
    end

    def installments_for_json
      if contribution.value.to_f >= CatarsePagarme.configuration.minimum_value_for_installment.to_f
        project = payment.project
        installments = payment.pagarme_delegator.get_installments['installments']
        collection = installments.map do |installment|
          installment_number = installment[0].to_i
          if installment_number <= (project.try(:total_installments) || CatarsePagarme.configuration.max_installments.to_i)
            amount = installment[1]['installment_amount'] / 100.0

            {amount: amount, number: installment_number}
          end
        end
      else
        collection = [{amount: payment.value, number: 1}]
      end
      collection.compact
    end

    def af_metadata
      {
        session_id: contribution.id.to_s,
        ip: contribution.user.current_sign_in_ip,
        platform: "web",
        register: {
          id: contribution.user.id.to_s,
          email: contribution.user.email,
          registered_at: contribution.user.created_at,
          login_source: "registered"
        },
        billing: {
          customer: {
            name: contribution.payer_name,
            document_number: contribution.payer_document.try(:gsub, /[-.\/_\s]/, ''),
            born_at: "",
            gender: ""
          },
          address: {
            country: contribution.country.try(:name),
            state: contribution.address_state,
            city: contribution.address_city,
            zipcode: contribution.address_zip_code.try(:gsub, /[-.\/_\s]/, ''),
            neighborhood: contribution.address_neighbourhood,
            street: contribution.address_street,
            street_number: contribution.address_number,
            complementary: contribution.address_complement,
            latitude: '',
            longitude: ''
          },
          card_holder_name: "",
          phone_numbers: [
            {
              ddi: "",
              ddd: phone_matches.try(:[], 1),
              number: phone_matches.try(:[], 2)
            }
          ]
        },
        contribution: [
          {
            contribution_id: contribution.id.to_s,
            project_name: payment.project.name,
            project_online: payment.project.online_at,
{'fraction_non_alphanumeric': 0.09726052583195441, 'fraction_numerical': 0.009192866335723478, 'mean_word_length': 2.2708959711365004, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '1887050', 'n_tokens_mistral': 3117, 'n_tokens_neox': 2988, 'n_words': 534}