<commit_before>module Epay
  module Api
    class << self
      def authorize(post)
        # Authorize transaction:
        RestClient.post AUTHORIZE_URL, post do |response|
          # The authorization request redirects to either accept or decline url:
          if location = response.headers[:location]
            query = CGI::parse(URI.parse(location.gsub(' ', '%20')).query)
          
            Hash[query.map do |k, v|
              [k, v.is_a?(Array) && v.size == 1 ? v[0] : v] # make values like ['v'] into 'v'
            end]
          else
            # No location header found
            raise ApiError, response
          end
        end
      end
      
      def default_post_for_params(params)        
        {
          :merchantnumber => Epay.merchant_number,
          
          :cardno     => params[:card_no],
          :cvc        => params[:cvc],
          :expmonth   => params[:exp_month],
          :expyear    => params[:exp_year],
          
          :amount     => (params[:amount] * 100).to_i,
          :currency   => Epay::CURRENCY_CODES[(params[:currency] || Epay.default_currency).to_sym],
          :orderid    => params[:order_no],
          
          :accepturl  => AUTHORIZE_URL + "?accept=1",
          :declineurl => AUTHORIZE_URL + "?decline=1",
        }
      end
      
      def handle_failed_response(response)
        case response.data['epayresponse']
        when "-1002" then raise InvalidMerchantNumber
        when "-1008" then raise TransactionNotFound
        else              raise ApiError, response.data['epayresponse']
        end
      end
      
      def request(url, action, params = {}, &block)
        service_url = "#{url}.asmx"
        soap_action = url + '/' + action
        
        params[:merchantnumber] ||= Epay.merchant_number
        params[:pwd] = <PASSWORD> if Epay.password.present?

        headers = {
          'Content-Type'  => 'text/xml; charset=utf-8',
          'SOAPAction'    => soap_action,
          'User-Agent'    => "Ruby / epay (#{VERSION})"
        }

        # Setup the SOAP body:
        xml = Builder::XmlMarkup.new(:indent => 2)
        xml.instruct!
        xml.tag! 'soap:Envelope', { 'xmlns:xsi' => 'http://schemas.xmlsoap.org/soap/envelope/',
                                    'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
                                    'xmlns:soap' => 'http://schemas.xmlsoap.org/soap/envelope/' } do

          xml.tag! 'soap:Body' do
            xml.tag! action, { 'xmlns' => url } do
              params.each do |attribute, value|
                xml.tag! attribute, value if value.present?
              end
            end
          end
        end
        
        RestClient.post service_url, xml.target!, headers do |raw_response|
          response = Response.new(raw_response, action)
          <commit_msg>Fix block style for Ruby 1.8.7<commit_after>module Epay
  module Api
    class << self
      def authorize(post)
        # Authorize transaction:
        RestClient.post AUTHORIZE_URL, post do |response|
          # The authorization request redirects to either accept or decline url:
          if location = response.headers[:location]
            query = CGI::parse(URI.parse(location.gsub(' ', '%20')).query)
          
            Hash[query.map do |k, v|
              [k, v.is_a?(Array) && v.size == 1 ? v[0] : v] # make values like ['v'] into 'v'
            end]
          else
            # No location header found
            raise ApiError, response
          end
        end
      end
      
      def default_post_for_params(params)        
        {
          :merchantnumber => Epay.merchant_number,
          
          :cardno     => params[:card_no],
          :cvc        => params[:cvc],
          :expmonth   => params[:exp_month],
          :expyear    => params[:exp_year],
          
          :amount     => (params[:amount] * 100).to_i,
          :currency   => Epay::CURRENCY_CODES[(params[:currency] || Epay.default_currency).to_sym],
          :orderid    => params[:order_no],
          
          :accepturl  => AUTHORIZE_URL + "?accept=1",
          :declineurl => AUTHORIZE_URL + "?decline=1",
        }
      end
      
      def handle_failed_response(response)
        case response.data['epayresponse']
        when "-1002" then raise InvalidMerchantNumber
        when "-1008" then raise TransactionNotFound
        else              raise ApiError, response.data['epayresponse']
        end
      end
      
      def request(url, action, params = {}, &block)
        service_url = "#{url}.asmx"
        soap_action = url + '/' + action
        
        params[:merchantnumber] ||= Epay.merchant_number
        params[:pwd] = <PASSWORD>.password if Epay.password.present?

        headers = {
          'Content-Type'  => 'text/xml; charset=utf-8',
          'SOAPAction'    => soap_action,
          'User-Agent'    => "Ruby / epay (#{VERSION})"
        }

        # Setup the SOAP body:
        xml = Builder::XmlMarkup.new(:indent => 2)
        xml.instruct!
        xml.tag! 'soap:Envelope', { 'xmlns:xsi' => 'http://schemas.xmlsoap.org/soap/envelope/',
                                    'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
                                    'xmlns:soap' => 'http://schemas.xmlsoap.org/soap/envelope/' } do

          xml.tag! 'soap:Body' do
            xml.tag! action, { 'xmlns' => url } do
              params.each do |attribute, value|
                xml.tag! attribute, value if value.present?
              end
            end
          end
        end
        
        RestClient.post service_url, xml.target!, headers do |raw_response, request, result|
          response = Response.new(raw_response, action)
          
{'fraction_non_alphanumeric': 0.12246665511865581, 'fraction_numerical': 0.008834228304174605, 'mean_word_length': 1.6596038691847075, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 28}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3658713', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1658, 'n_words': 365}