Class: Afipws::WSAA

Inherits:
Object
  • Object
show all
Defined in:
lib/afipws/wsaa.rb

Constant Summary collapse

WSDL =
{
  development: 'https://wsaahomo.afip.gov.ar/ws/services/LoginCms?wsdl',
  production: 'https://wsaa.afip.gov.ar/ws/services/LoginCms?wsdl',
  test: Root + '/spec/fixtures/wsaa/wsaa.wsdl'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ WSAA

Returns a new instance of WSAA.



11
12
13
14
15
16
17
18
19
20
# File 'lib/afipws/wsaa.rb', line 11

def initialize options = {}
  @env = (options[:env] || :test).to_sym
  @key = options[:key]
  @cert = options[:cert]
  @service = options[:service] || 'wsfe'
  @ttl = options[:ttl] || 2400
  @cuit = options[:cuit]
  @client = Client.new Hash(options[:savon]).reverse_merge(wsdl: WSDL[@env])
  @ta_path = options[:ta_path] || File.join(Dir.pwd, 'tmp', "#{@cuit}-#{@env}-#{@service}-ta.dump")
end

Instance Attribute Details

#certObject (readonly)

Returns the value of attribute cert.



3
4
5
# File 'lib/afipws/wsaa.rb', line 3

def cert
  @cert
end

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/afipws/wsaa.rb', line 3

def client
  @client
end

#cuitObject (readonly)

Returns the value of attribute cuit.



3
4
5
# File 'lib/afipws/wsaa.rb', line 3

def cuit
  @cuit
end

#envObject (readonly)

Returns the value of attribute env.



3
4
5
# File 'lib/afipws/wsaa.rb', line 3

def env
  @env
end

#keyObject (readonly)

Returns the value of attribute key.



3
4
5
# File 'lib/afipws/wsaa.rb', line 3

def key
  @key
end

#serviceObject (readonly)

Returns the value of attribute service.



3
4
5
# File 'lib/afipws/wsaa.rb', line 3

def service
  @service
end

#taObject (readonly)

Returns the value of attribute ta.



3
4
5
# File 'lib/afipws/wsaa.rb', line 3

def ta
  @ta
end

Instance Method Details

#authObject



60
61
62
63
# File 'lib/afipws/wsaa.rb', line 60

def auth
  ta = obtener_y_cachear_ta
  {token: ta[:token], sign: ta[:sign]}
end

#codificar_tra(pkcs7) ⇒ Object



41
42
43
# File 'lib/afipws/wsaa.rb', line 41

def codificar_tra pkcs7
  pkcs7.to_pem.lines.to_a[1..-2].join
end

#firmar_tra(tra, key, crt) ⇒ Object



35
36
37
38
39
# File 'lib/afipws/wsaa.rb', line 35

def firmar_tra tra, key, crt
  key = OpenSSL::PKey::RSA.new key
  crt = OpenSSL::X509::Certificate.new crt
  OpenSSL::PKCS7.sign crt, key, tra
end

#generar_tra(service, ttl) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/afipws/wsaa.rb', line 22

def generar_tra service, ttl
  xml = Builder::XmlMarkup.new indent: 2
  xml.instruct!
  xml.loginTicketRequest version: 1 do
    xml.header do
      xml.uniqueId Time.now.to_i
      xml.generationTime xsd_datetime Time.now - ttl
      xml.expirationTime xsd_datetime Time.now + ttl
    end
    xml.service service
  end
end

#loginObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/afipws/wsaa.rb', line 49

def 
  response = @client.request :login_cms, in0: tra(@key, @cert, @service, @ttl)
  ta = Nokogiri::XML(Nokogiri::XML(response.to_xml).text)
  {
    token: ta.css('token').text,
    sign: ta.css('sign').text,
    generation_time: from_xsd_datetime(ta.css('generationTime').text),
    expiration_time: from_xsd_datetime(ta.css('expirationTime').text)
  }
end

#tra(key, cert, service, ttl) ⇒ Object



45
46
47
# File 'lib/afipws/wsaa.rb', line 45

def tra key, cert, service, ttl
  codificar_tra firmar_tra(generar_tra(service, ttl), key, cert)
end