Module: Webmoney

Includes:
RequestResult, RequestRetval, RequestXML
Defined in:
lib/wmid.rb,
lib/purse.rb,
lib/passport.rb,
lib/webmoney.rb,
lib/messenger.rb,
lib/interfaces.rb

Overview

Module for Webmoney lib. Instance contain info for WMT-interfaces requests (wmid, key, etc). Implement general requests.

Defined Under Namespace

Modules: RequestResult, RequestRetval, RequestXML Classes: CaCertificateError, IncorrectPurseError, IncorrectWmidError, Messenger, NonExistentWmidError, Passport, Purse, RequestError, ResultError, WebmoneyError, Wmid

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RequestResult

#result_balance, #result_bussines_level, #result_check_sign, #result_check_user, #result_create_invoice, #result_create_transaction, #result_find_wm, #result_get_passport, #result_login, #result_outgoing_invoices, #result_send_message, #result_trust_me

Methods included from RequestRetval

#retval_balance, #retval_check_user, #retval_common, #retval_create_invoice, #retval_find_wm, #retval_get_passport, #retval_login, #retval_outgoing_invoices

Methods included from RequestXML

#xml_balance, #xml_bussines_level, #xml_check_sign, #xml_check_user, #xml_create_invoice, #xml_create_transaction, #xml_find_wm, #xml_get_passport, #xml_i_trust, #xml_login, #xml_outgoing_invoices, #xml_send_message, #xml_trust_me

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



33
34
35
# File 'lib/webmoney.rb', line 33

def error
  @error
end

#errormsgObject (readonly)

Returns the value of attribute errormsg.



33
34
35
# File 'lib/webmoney.rb', line 33

def errormsg
  @errormsg
end

#interfacesObject (readonly)

Returns the value of attribute interfaces.



33
34
35
# File 'lib/webmoney.rb', line 33

def interfaces
  @interfaces
end

#last_requestObject (readonly)

Returns the value of attribute last_request.



33
34
35
# File 'lib/webmoney.rb', line 33

def last_request
  @last_request
end

#last_responseObject (readonly)

Returns the value of attribute last_response.



33
34
35
# File 'lib/webmoney.rb', line 33

def last_response
  @last_response
end

#messengerObject

Returns the value of attribute messenger.



34
35
36
# File 'lib/webmoney.rb', line 34

def messenger
  @messenger
end

#ridObject (readonly)

Returns the value of attribute rid.



33
34
35
# File 'lib/webmoney.rb', line 33

def rid
  @rid
end

#wmidObject (readonly)

Returns the value of attribute wmid.



33
34
35
# File 'lib/webmoney.rb', line 33

def wmid
  @wmid
end

Instance Method Details

#classic?Boolean

Webmoney instance is classic type?

Returns:

  • (Boolean)


145
146
147
# File 'lib/webmoney.rb', line 145

def classic?
  !! @signer
end

#detect_file(option) ⇒ Object

When x509, key and cert is path to file or filename in ~/.wm/, or initialized PKey::RSA and X509::Certificate objects



66
67
68
69
# File 'lib/webmoney.rb', line 66

def detect_file(option)
  pathes = %w(%s ~/.wm/%s)
  pathes.map{|path| File.expand_path(path % option)}.detect{|path| File.file?(path)}
end

#filter_str(str) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/webmoney.rb', line 128

def filter_str(str)
  if @force_encoding
    str_out = utf8_to_cp1251(str)
    [cp1251_to_utf8(str_out), str_out]
  else
    [str, utf8_to_cp1251(str)]
  end
end

#initialize(opt = {}) ⇒ Object

Required options:

  • :wmid - WMID

Optional:

  • :password - on Classic key or Light X509 certificate & key

  • :key - Base64 string for Classic key

OR

  • :key - OpenSSL::PKey::RSA or OpenSSL::PKey::DSA object

  • :cert - OpenSSL::X509::Certificate object

  • :ca_cert - file CA certificate or path to directory with certs (in PEM format)



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/webmoney.rb', line 56

def initialize(opt = {})

  unless check_libxml_version
    $stderr.puts "WARNING: webmoney lib will not work correctly with nokogori compiled with libxml2 version < 2.7.0"
  end

  @wmid = Wmid.new(opt[:wmid])

  # When x509, key and cert is path to file or filename in ~/.wm/,
  # or initialized PKey::RSA and X509::Certificate objects
  def detect_file(option)
    pathes = %w(%s ~/.wm/%s)
    pathes.map{|path| File.expand_path(path % option)}.detect{|path| File.file?(path)}
  end

  if file = detect_file(opt[:key])
    # light
    @key = OpenSSL::PKey::RSA.new(File.read(file), opt[:password])
    @cert = OpenSSL::X509::Certificate.new(File.read(detect_file(opt[:cert])))
  elsif opt[:key].is_a? OpenSSL::PKey::RSA
    # initialized OpenSSL::PKey::RSA objects
    @key = opt[:key]
    @cert = opt[:cert]
  elsif opt[:password]
    # key is classic base64-encoded key
    @signer = Signer.new(@wmid, opt[:password], opt[:key])
  end

  # ca_cert or default
  @ca_cert =
    if opt[:ca_cert].nil?
       File.dirname(__FILE__) + '/../ssl-certs/ca_bundle.crt'
    else
      opt[:ca_cert]
    end

  @rid = opt[:rid]

  # encode will raise exception,
  # when uncovertable character in input sequence. It is default behavior.
  # With option :force_encoding uncovertable characters will be cutted.
  @force_encoding = opt[:force_encoding]

  # for backward compatibility with ruby 1.8
  if String.new.respond_to?(:encode)

    # was: @ic_out
    def utf8_to_cp1251(str)
      return str if str.nil? || str.length < 1
      @force_encoding ? str.encode('CP1251', 'UTF-8', :undef => :replace, :replace => '') : str.encode('CP1251', 'UTF-8')
    end

    # was: @ic_in
    def cp1251_to_utf8(str)
      return str if str.empty?
      str.encode('UTF-8', 'CP1251')
    end

  else
    require 'iconv'

    # was: @ic_out
    def utf8_to_cp1251(str)
      @force_encoding ? Iconv.iconv('CP1251//IGNORE', 'UTF-8', str)[0] : Iconv.iconv('CP1251', 'UTF-8', str)[0]
    end

    # was: @ic_in
    def cp1251_to_utf8(str)
      Iconv.iconv('UTF-8', 'CP1251', str)[0]
    end
  end

  def filter_str(str)
    if @force_encoding
      str_out = utf8_to_cp1251(str)
      [cp1251_to_utf8(str_out), str_out]
    else
      [str, utf8_to_cp1251(str)]
    end
  end

  prepare_interface_urls

  # initialize workers by self
  Purse.worker = self
  Passport.worker = self
end

#interface_urlsObject

Presets for interfaces



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/interfaces.rb', line 4

def interface_urls
  {
    :create_invoice     => { :url => 'XMLInvoice.asp' },       # x1
    :create_transaction => { :url => 'XMLTrans.asp' },         # x2
    :operation_history  => { :url => 'XMLOperations.asp' },    # x3
    :outgoing_invoices  => { :url => 'XMLOutInvoices.asp' },   # x4
    :finish_protect     => { :url => 'XMLFinishProtect.asp' }, # x5
    :send_message       => { :url => 'XMLSendMsg.asp' },       # x6
    :check_sign         => { :url => 'XMLClassicAuth.asp' },   # x7
    :find_wm            => { :url => 'XMLFindWMPurse.asp' },   # x8
    :balance            => { :url => 'XMLPurses.asp' },        # x9
    :incoming_invoices  => { :url => 'XMLInInvoices.asp' },    # x10
    :get_passport       => { :url => 'https://passport.webmoney.ru/asp/XMLGetWMPassport.asp' , # x11
                             :x509 => lambda {|url| url.sub(/\.asp$/, 'Cert.asp')} },
    :reject_protection  => { :url => 'XMLRejectProtect.asp' }, # x13
    :transaction_moneyback => { :url => 'XMLTransMoneyback.asp' }, # x14
    :i_trust            => { :url => 'XMLTrustList.asp'  },    # x15
    :trust_me           => { :url => 'XMLTrustList2.asp' },    # x15
    :trust_save         => { :url => 'XMLTrustSave2.asp' },    # x15
    :create_purse       => { :url => 'XMLCreatePurse.asp' },   # x16
    :create_contract    => { :url => 'https://arbitrage.webmoney.ru/xml/X17_CreateContract.aspx', },  # x17
    :get_contract_info  => { :url => 'https://arbitrage.webmoney.ru/xml/X17_GetContractInfo.aspx' }, # x17
    :transaction_get    => { :url => 'https://merchant.webmoney.ru/conf/xml/XMLTransGet.asp' },      # x18
    :check_user         => { :url => 'https://apipassport.webmoney.ru/XMLCheckUser.aspx',            # x19
                             :x509 => lambda {|url| 'https://apipassportcrt.webmoney.ru/XMLCheckUserCert.aspx' } },
    :bussines_level     => { :url => 'https://stats.wmtransfer.com/levels/XMLWMIDLevel.aspx' },
    :login              => { :url => 'https://login.wmtransfer.com/ws/authorize.xiface' },           # login
  }
end

#request(iface, opt = {}) ⇒ Object

Generic function for request to WMT-interfaces

Raises:

  • (ArgumentError)


167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/webmoney.rb', line 167

def request(iface, opt ={})
  raise ArgumentError, "should be hash" unless opt.kind_of?(Hash)

  # Use self wmid when not defined
  opt[:wmid] ||= @wmid

  # Do request
  res = https_request(iface, make_xml(iface, opt))

  # Parse response
  doc = Nokogiri::XML(res)
  parse_retval(iface, doc)
  make_result(iface, doc)
end

#send_message(params) ⇒ Object

Send message through Queue and Thread

Params: { :wmid, :subj, :text }



153
154
155
156
# File 'lib/webmoney.rb', line 153

def send_message(params)
  @messenger = Messenger.new(self){} if @messenger.nil?
  @messenger.push(params)
end

#sign(str) ⇒ Object

Signing string by instance wmid’s, return signed string



185
186
187
# File 'lib/webmoney.rb', line 185

def sign(str)
  @signer.sign(str) unless str.nil? || str.empty?
end

#w3s_urlObject

Preset for W3S



38
39
40
# File 'lib/webmoney.rb', line 38

def w3s_url
  'https://w3s.wmtransfer.com/asp/'
end

#wmid_exist?(wmid) ⇒ Boolean

Check existent WMID or not

Params: wmid

Returns:

  • (Boolean)


161
162
163
# File 'lib/webmoney.rb', line 161

def wmid_exist?(wmid)
  request(:find_wm, :wmid => Wmid.new(wmid))[:retval] == 1
end