Class: Timbradocfdi::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/timbradocfdi.rb,
lib/timbradocfdi/cancel.rb,
lib/timbradocfdi/obtain.rb,
lib/timbradocfdi/results.rb,
lib/timbradocfdi/stamping.rb,
lib/timbradocfdi/register_emisor.rb

Constant Summary collapse

BASE_URI =
"http://201.175.12.89/serviciointegracionpruebas/Timbrado.asmx?wsdl"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ Generator

Se inicializa la clase con un usuario y su respectivo cliente con el soap



20
21
22
23
# File 'lib/timbradocfdi.rb', line 20

def initialize(user)
  @client = Savon.client(wsdl: BASE_URI)
  @user = user
end

Instance Attribute Details

#clientObject

Attributo accesible (lectura y escritura)



16
17
18
# File 'lib/timbradocfdi.rb', line 16

def client
  @client
end

#userObject

Attributo accesible (lectura y escritura)



16
17
18
# File 'lib/timbradocfdi.rb', line 16

def user
  @user
end

Instance Method Details

#cancelaCFDI(rfcEmisor, folioUUID) ⇒ Object

Cancela CFDI



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/timbradocfdi/cancel.rb', line 7

def cancelaCFDI(rfcEmisor, folioUUID)
  
  options = {
    :usuarioIntegrador =>   @user,
    :rfcEmisor         =>   rfcEmisor,
    :folioUUID         =>   folioUUID,
  }

  response = request("cancela_cfdi", options)
  parse_result_cancelaCFDI response.body
end

#file_reader(path) ⇒ Object

Read certificate and encode to base64



28
29
30
31
# File 'lib/timbradocfdi.rb', line 28

def file_reader(path)
  raw = File.read(path)
  Base64.encode64(raw)
end

#obtieneCFDI(rfcEmisor, folioUUID) ⇒ Object

Registro emisor



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/timbradocfdi/obtain.rb', line 7

def obtieneCFDI(rfcEmisor, folioUUID)
  
  options = {
    :usuarioIntegrador =>   @user,
    :rfcEmisor         =>   rfcEmisor,
    :folioUUID         =>   folioUUID,
  }

  response = request("obtiene_cfdi", options)
  parse_result_obtieneCFDI response.body
end

#parse_result_cancelaCFDI(result) ⇒ Object

Parse result for cancelaCFDI



63
64
65
66
67
68
69
70
71
# File 'lib/timbradocfdi/results.rb', line 63

def parse_result_cancelaCFDI(result)
  object = result.to_hash 
  result_object = object[:cancela_cfdi_response][:cancela_cfdi_result][:any_type]

  {
    code: result_object[1],
    message: (result_object[2].to_s).encode("utf-8")
  }
end

#parse_result_obtieneCFDI(result) ⇒ Object

Parse result for obtieneCFDI



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/timbradocfdi/results.rb', line 42

def parse_result_obtieneCFDI(result)
  object = result.to_hash
  result_object = object[:obtiene_cfdi_response][:obtiene_cfdi_result][:any_type]

  if result_object[1].to_i === 0 
    return_object = {
      code: 0,
      xml: result_object[3],
      qr: result_object[4],
      details: result_object[5]
    }
  else
    return_object = {
      code: result_object[1],
      message: result_object[2]
    }
  end
end

#parse_result_registro_emisor(result) ⇒ Object

Parse result for registro emisor



7
8
9
10
11
12
13
14
15
# File 'lib/timbradocfdi/results.rb', line 7

def parse_result_registro_emisor(result)
  object        = result.to_hash
  result_object = object[:registra_emisor_response][:registra_emisor_result][:any_type]

  {
    code: result_object[1],
    message: (result_object[2].to_s).encode("utf-8")
  }
end

#parse_result_timbraCFDI(result) ⇒ Object

Parse result for timbraCFDI



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/timbradocfdi/results.rb', line 19

def parse_result_timbraCFDI(result)
  object = result.to_hash
  result_object = object[:timbra_cfdi_response][:timbra_cfdi_result][:any_type]

  if result_object[1].to_i === 0 
    return_object = {
      code: 0,
      xml: result_object[3],
      qr: result_object[4],
      details: result_object[5]
    }
  else
    return_object = {
      code: result_object[1],
      message: result_object[2]
    }
  end

  return_object
end

#registroEmisor(rfcEmisor, base64Cer, base64Key, contrasena) ⇒ Object

Registro emisor



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/timbradocfdi/register_emisor.rb', line 7

def registroEmisor(rfcEmisor, base64Cer, base64Key, contrasena)
  
  options = {
    :usuarioIntegrador =>   @user,
    :rfcEmisor         =>   rfcEmisor,
    :base64Cer         =>   file_reader(base64Cer),
    :base64Key         =>   file_reader(base64Key),
    :contrasena        =>   contrasena
  }

  response = request("registra_emisor", options)
  parse_result_registro_emisor response.body
end

#request(function, options = {}) ⇒ Object

Method for generate HTTP Request post



35
36
37
38
# File 'lib/timbradocfdi.rb', line 35

def request(function, options = {})
  operation = function.intern
  @client.call(operation, message: options)
end

#timbraCFDI(xmlComprobante, idComprobante) ⇒ Object

TimbraCFDI



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/timbradocfdi/stamping.rb', line 7

def timbraCFDI(xmlComprobante, idComprobante)

  options = {
    :usuarioIntegrador    =>   @user,
    :xmlComprobanteBase64 =>   file_reader(xmlComprobante),
    :idComprobante        =>   idComprobante,
  }

  response = request("timbra_cfdi", options)
  parse_result_timbraCFDI response.body
end