<commit_before>module ActiveMerchant #:nodoc:
  module Billing #:nodoc:
    module MoipCore #:nodoc:
      URL_ACTIONS = {
          'authenticate'  => '/ws/alpha/EnviarInstrucao/Unica',
          'pay'           => '/rest/pagamento?callback=?'
      }

      CARD_BRAND = {
          'visa'             => 'Visa',
          'master'           => 'Mastercard',
          'diners_club'      => 'Diners',
          'american_express' => 'AmericanExpress'
      }

      PAYMENT_METHODS = {
          'itau'             => 'DebitoBancario',
          'bradesco'         => 'DebitoBancario',
          'banrisul'         => 'DebitoBancario',
          'banco_do_brasil'  => 'DebitoBancario',
          'boleto_bancario'  => 'BoletoBancario',
          'visa'             => 'CartaoCredito',
          'master'           => 'CartaoCredito',
          'diners_club'      => 'CartaoCredito',
          'american_express' => 'CartaoCredito'
      }


      def initialize(options = {})
        requires!(options, :token, :api_key)
        super
      end

      private

      def build_authenticate_request(money, options)
        xml = Builder::XmlMarkup.new
        xml.tag! 'EnviarInstrucao' do
          xml.tag! 'InstrucaoUnica', TipoValidacao: 'Transparente' do

            xml.tag! 'IdProprio', options[:order_id]
            xml.tag! 'Razao',     options[:reason]

            xml.tag! 'Valores' do
              currency = options[:currency] || default_currency
              xml.tag! 'Valor',     amount(money),                moeda: currency
              xml.tag! 'Acrescimo', amount(options[:additional]), moeda: currency unless options[:additional].blank?
              xml.tag! 'Deducao',   amount(options[:discount]),   moeda: currency unless options[:discount].blank?
            end

            xml.tag! 'Pagador' do
              add_customer_data(xml, options[:payer])
              add_address(xml, options[:address] || options[:billing_address])
            end

            add_commissions(xml, options[:commissions])           unless options[:commissions].blank?
            add_installments_options(xml, options[:installments]) unless options[:installments].blank?
            add_receiver(xml, options[:receiver])                 unless options[:receiver].blank?
            add_payment_slip_data(xml, options[:payment_slip])    unless options[:payment_slip].blank?

            xml.tag! 'URLNotificacao', options[:notification_url] unless options[:notification_url].blank?
            xml.tag! 'URLRetorno', options[:return_url]           unless options[:return_url].blank?

          end
        end
        xml.target!
      end

      def build_pay_params(authorization, options)
        params = {
            :pagamentoWidget => {
                :token => authorization,
                :referer => options[:referer],
                :dadosPagamento => {
                    :Forma => payment_methods(options[:payment_method])
                }
            }
        }

        case params[:pagamentoWidget][:dadosPagamento][:Forma]
          when 'CartaoCredito'
            requires!(options, :creditcard)
            add_creditcard(params[:pagamentoWidget][:dadosPagamento], options[:payment_method], options[:creditcard])
          when 'DebitoBancario'
            params[:pagamentoWidget][:dadosPagamento][:Instituicao] = options[:payment_method].classify
        end

        {
            :pagamentoWidget => params.to_json
        }
      end

      def payment_methods(method)
        if method.is_a?(CreditCard)
          'CartaoCredito'
        else
          PAYMENT_METHODS[method.underscore]
        end
      end

      def add_creditcard(params, creditcard_or_vault, options)
        params[:Parcelas] = options[:installments] || 1

        if creditcard_or_vault.is_a?(String)
          params[:Instituicao] = CARD_BRAND[creditcard_or_vault.underscore]
          params[:CartaoCredito] =  {
              :Cofre => options[:vault_id],
              :CodigoSeguranca => options[:verification_value]
          }
        else
          params[:Instituicao] =  CARD_BRAND[creditcard_or_vault.brand]
          params[:CartaoCredito] =  {
              :Numero =>          creditcard_or_vault.number,
              :Expiracao =>       "#{format(creditcard_or_vault.month, :two_digits)}/#{format(creditcard_or_vault.year, :two_digits)}",
              :CodigoSeguranca => creditcard_or_vault.verification_value,
              :Portador => {
                  :Nome =>           creditcard_or_vault.name,
                  :Telefone =>       options[:phone],
                  :Identidade =>     options[:identity_document],
                  :DataNascimento => options[:birthday]
              }
          }
        end
      end<commit_msg>fixing moip core payment method<commit_after>module ActiveMerchant #:nodoc:
  module Billing #:nodoc:
    module MoipCore #:nodoc:
      URL_ACTIONS = {
          'authenticate'  => '/ws/alpha/EnviarInstrucao/Unica',
          'pay'           => '/rest/pagamento?callback=?'
      }

      CARD_BRAND = {
          'visa'             => 'Visa',
          'master'           => 'Mastercard',
          'diners_club'      => 'Diners',
          'american_express' => 'AmericanExpress'
      }

      PAYMENT_METHODS = {
          'itau'             => 'DebitoBancario',
          'bradesco'         => 'DebitoBancario',
          'banrisul'         => 'DebitoBancario',
          'banco_do_brasil'  => 'DebitoBancario',
          'boleto_bancario'  => 'BoletoBancario',
          'visa'             => 'CartaoCredito',
          'master'           => 'CartaoCredito',
          'diners_club'      => 'CartaoCredito',
          'american_express' => 'CartaoCredito'
      }


      def initialize(options = {})
        requires!(options, :token, :api_key)
        super
      end

      private

      def build_authenticate_request(money, options)
        xml = Builder::XmlMarkup.new
        xml.tag! 'EnviarInstrucao' do
          xml.tag! 'InstrucaoUnica', TipoValidacao: 'Transparente' do

            xml.tag! 'IdProprio', options[:order_id]
            xml.tag! 'Razao',     options[:reason]

            xml.tag! 'Valores' do
              currency = options[:currency] || default_currency
              xml.tag! 'Valor',     amount(money),                moeda: currency
              xml.tag! 'Acrescimo', amount(options[:additional]), moeda: currency unless options[:additional].blank?
              xml.tag! 'Deducao',   amount(options[:discount]),   moeda: currency unless options[:discount].blank?
            end

            xml.tag! 'Pagador' do
              add_customer_data(xml, options[:payer])
              add_address(xml, options[:address] || options[:billing_address])
            end

            add_commissions(xml, options[:commissions])           unless options[:commissions].blank?
            add_installments_options(xml, options[:installments]) unless options[:installments].blank?
            add_receiver(xml, options[:receiver])                 unless options[:receiver].blank?
            add_payment_slip_data(xml, options[:payment_slip])    unless options[:payment_slip].blank?

            xml.tag! 'URLNotificacao', options[:notification_url] unless options[:notification_url].blank?
            xml.tag! 'URLRetorno', options[:return_url]           unless options[:return_url].blank?

          end
        end
        xml.target!
      end

      def build_pay_params(authorization, options)
        params = {
            :pagamentoWidget => {
                :token => authorization,
                :referer => options[:referer],
                :dadosPagamento => {
                    :Forma => payment_methods(options[:payment_method])
                }
            }
        }

        case params[:pagamentoWidget][:dadosPagamento][:Forma]
          when 'CartaoCredito'
            requires!(options, :creditcard)
            add_creditcard(params[:pagamentoWidget][:dadosPagamento], options[:payment_method], options[:creditcard])
          when 'DebitoBancario'
            params[:pagamentoWidget][:dadosPagamento][:Instituicao] = options[:payment_method].classify
        end

        {
            :pagamentoWidget => params.to_json
        }
      end

      def payment_methods(method)
        if method.is_a?(CreditCard)
          'CartaoCredito'
        else
          PAYMENT_METHODS[method.to_s.underscore]
        end
      end

      def add_creditcard(params, creditcard_or_vault, options)
        params[:Parcelas] = options[:installments] || 1

        if creditcard_or_vault.is_a?(String)
          params[:Instituicao] = CARD_BRAND[creditcard_or_vault.underscore]
          params[:CartaoCredito] =  {
              :Cofre => options[:vault_id],
              :CodigoSeguranca => options[:verification_value]
          }
        else
          params[:Instituicao] =  CARD_BRAND[creditcard_or_vault.brand]
          params[:CartaoCredito] =  {
              :Numero =>          creditcard_or_vault.number,
              :Expiracao =>       "#{format(creditcard_or_vault.month, :two_digits)}/#{format(creditcard_or_vault.year, :two_digits)}",
              :CodigoSeguranca => creditcard_or_vault.verification_value,
              :Portador => {
                  :Nome =>           creditcard_or_vault.name,
                  :Telefone =>       options[:phone],
                  :Identidade =>     options[:identity_document],
                  :DataNascimento => options[:birthday]
              }
          }
        end
      end
{'fraction_non_alphanumeric': 0.11363873526035688, 'fraction_numerical': 0.0002087029114056141, 'mean_word_length': 1.9543773119605425, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 38}, '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': '4507321', 'n_tokens_mistral': 2847, 'n_tokens_neox': 2762, 'n_words': 425}