<commit_before># coding: utf-8

require 'gpgme'

module Payzilla
  module Gateways
    class Yamoney < Gateway
      register_settings    %w(url currency password gpg_key)
      register_attachments %w(public_key secret_key)

      def check(payment)
        begin
          result = send '1002',
            :TR_NR      => payment.id,
            :DSTACNT_NR => payment.account,
            :TR_AMT     => payment.enrolled_amount,
            :CUR_CD     => @config.setting_currency,
            :SIGN       => sign([payment.id, 1002, payment.account, payment.enrolled_amount, @config.setting_currency])

          return retval(result)
        rescue Errno::ECONNRESET
          return {:success => false, :error => -1000}
        end
      end

      def pay(payment)
        begin
          result = send '1',
            :TR_NR      => payment.id,
            :DSTACNT_NR => payment.account,
            :TR_AMT     => payment.enrolled_amount,
            :CUR_CD     => @config.setting_currency,
            :CONT       => "Пополнение кошелька".encode("Windows-1251"),
            :SIGN       => sign([payment.id, 1002, payment.account, payment.enrolled_amount, @config.setting_currency])

          return retval(result)
        rescue Errno::ECONNRESET
          return {:success => false, :error => -1000}
        end
      end

    private

      def retval(result)
        if result[:RES_CD] == "0"
          return {:success => true, :error => "0"}
        else
          return {:success => false, :error => result[:ERR_CD]}
        end
      end

      def sign(values)
        attach_keys

        crypto = GPGME::Crypto.new :armor => true
        crypto.clearsign(values.map{|x| x.to_s}.join('&'),
                            {
                              :password => <PASSWORD>.setting_password,
                              :signer   => @config.setting_gpg_key
                            }
                        )
      end

      def attach_keys
        %w(public secret).each do |key|
          if GPGME::Key.find(key.to_sym, @config.setting_gpg_key).empty?
            GPGME::Key.import(File.open(@config.send("attachment_#{key}_key".to_sym)))
          end
        end
      end

      def send(operation, params)
        params[:ACT_CD] = operation
        params[:VERSION] = '2.02'

        resource = RestClient::Resource.new(@config.setting_url)

        result = resource.post :params => params
        sign   = GPGME::Crypto.new(:armor => true)
        result = sign.verify(result.to_s) do |sig|
          return {:RES_CD => "1", :ERR_CD => "Bad signature" } if sig.bad?
        end
        result = result.to_s.split("\n").map{|x| x.split("=")}.flatten
        result = Hash[*result].with_indifferent_access

        return result
      end

    end
  end
end<commit_msg>Yamoney fix<commit_after># coding: utf-8

require 'gpgme'

module Payzilla
  module Gateways
    class Yamoney < Gateway
      register_settings    %w(url currency password gpg_key)
      register_attachments %w(public_key secret_key)

      def check(payment)
        begin
          result = send '1002',
            :TR_NR      => payment.id,
            :DSTACNT_NR => payment.account,
            :TR_AMT     => payment.enrolled_amount,
            :CUR_CD     => @config.setting_currency,
            :SIGN       => sign([payment.id, 1002, payment.account, payment.enrolled_amount, @config.setting_currency])

          return retval(result)
        rescue Errno::ECONNRESET
          return {:success => false, :error => -1000}
        end
      end

      def pay(payment)
        begin
          result = send '1',
            :TR_NR      => payment.id,
            :DSTACNT_NR => payment.account,
            :TR_AMT     => payment.enrolled_amount,
            :CUR_CD     => @config.setting_currency,
            :CONT       => "Пополнение кошелька".encode("Windows-1251"),
            :SIGN       => sign([payment.id, 1002, payment.account, payment.enrolled_amount, @config.setting_currency])

          return retval(result)
        rescue Errno::ECONNRESET
          return {:success => false, :error => -1000}
        end
      end

    private

      def retval(result)
        if result[:RES_CD] == "0"
          return {:success => true, :error => "0"}
        else
          return {:success => false, :error => result[:ERR_CD]}
        end
      end

      def sign(values)
        attach_keys

        crypto = GPGME::Crypto.new :armor => true
        crypto.clearsign(values.map{|x| x.to_s}.join('&'),
                            {
                              :password => <PASSWORD>,
                              :signer   => @config.setting_gpg_key
                            }
                        )
      end

      def attach_keys
        %w(public secret).each do |key|
          if GPGME::Key.find(key.to_sym, @config.setting_gpg_key).empty?
            GPGME::Key.import(File.open(@config.send("attachment_#{key}_key".to_sym)))
          end
        end
      end

      def send(operation, params)
        params[:ACT_CD] = operation
        params[:VERSION] = '2.02'

        resource = RestClient::Resource.new(@config.setting_url)

        result = resource.post :params => params
        sign   = GPGME::Crypto.new(:armor => true)
        params = sign.verify(result.to_s) do |sig|
          result = {:RES_CD => "1", :ERR_CD => "Bad signature" } if sig.bad?
        end

        return result if result.kind_of(Hash)
        result = params.to_s.split("\n").map{|x| x.split("=")}.flatten
        result = Hash[*result].with_indifferent_access

        return result
      end

    end
  end
end
{'fraction_non_alphanumeric': 0.11746987951807229, 'fraction_numerical': 0.011339475549255847, 'mean_word_length': 1.7509746588693957, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 57, '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': '5607154', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1661, 'n_words': 346}