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.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
# 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])
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

#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

Obtiene un TA, lo cachea hasta que expire, y devuelve el hash Auth listo para pasarle al Client en los otros WS.



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

def auth
  @ta =  if ta_expirado?
  { auth: { token: @ta[:token], sign: @ta[:sign], cuit: @cuit } }
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



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

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
      # TODO me parece que no le da mucha bola el WS al expirationTime
      xml.expirationTime xsd_datetime Time.now + ttl
    end
    xml.service service
  end
end

#loginObject



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

def 
  response = @client.raw_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) }
rescue Savon::SOAPFault => f
  raise WSError, f.message
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