sw-sdk-ruby 0.0.2.2.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,12 @@
1
+ require_relative '../Services.rb'
2
+ require_relative 'pendings_request.rb'
3
+
4
+ class Pendings < Services
5
+ def self.set(params)
6
+ Services::set_data(params)
7
+ end
8
+
9
+ def self.get_pendings(rfc)
10
+ return SwPendings::pendings(Services::get_url, Services::get_token, rfc)
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ require 'net/http'
2
+ require_relative '../Response/generic_response.rb'
3
+
4
+ class SwPendings
5
+ def self.pendings(url, token, rfc)
6
+ url = URI(url + "/pendings/" + rfc)
7
+ http = Net::HTTP.new(url.host, url.port)
8
+ request = Net::HTTP::Get.new(url)
9
+ request["Authorization"] = 'bearer ' + token
10
+ request["Content-Type"] = 'application/json'
11
+ request["Cache-Control"] = 'no-cache'
12
+ response = http.request(request)
13
+ response_obj = GenericResponse.new(response)
14
+ return response_obj.validate_status_code(response_obj)
15
+ end
16
+ end
@@ -0,0 +1,24 @@
1
+ require_relative '../Services.rb'
2
+ require_relative 'relations_request.rb'
3
+
4
+ class Relations < Services
5
+ def self.set(params)
6
+ Services::set_data(params)
7
+ end
8
+
9
+ def self.relations_csd(uuid, rfc, password, csd, key)
10
+ return SwRelations::relations_csd(Services::get_url, Services::get_token, uuid, rfc, password, csd, key)
11
+ end
12
+
13
+ def self.relations_uuid(uuid, rfc)
14
+ return SwRelations::relations_uuid(Services::get_url, Services::get_token, uuid, rfc)
15
+ end
16
+
17
+ def self.relations_pfx(uuid, rfc, password, pfx)
18
+ return SwRelations::relations_pfx(Services::get_url, Services::get_token, uuid, rfc, password, pfx)
19
+ end
20
+
21
+ def self.relations_xml(xml)
22
+ return SwRelations::relations_xml(Services::get_url, Services::get_token, xml)
23
+ end
24
+ end
@@ -0,0 +1,57 @@
1
+ require 'net/http'
2
+ require 'securerandom'
3
+ require_relative '../Response/generic_response.rb'
4
+
5
+ class SwRelations
6
+
7
+ def self.relations_uuid(url, token, uuid, rfc)
8
+ url = URI(url + "/relations/" + rfc + "/" + uuid)
9
+ http = Net::HTTP.new(url.host, url.port)
10
+ request = Net::HTTP::Post.new(url)
11
+ request["Authorization"] = 'bearer ' + token
12
+ request["Cache-Control"] = 'no-cache'
13
+ response = http.request(request)
14
+ response_obj = GenericResponse.new(response)
15
+ return response_obj.validate_status_code(response_obj)
16
+ end
17
+
18
+ def self.relations_csd(url, token, uuid, rfc, password, csd, key)
19
+ url = URI(url + "/relations/csd")
20
+ http = Net::HTTP.new(url.host, url.port)
21
+ request = Net::HTTP::Post.new(url)
22
+ request["Content-Type"] = 'application/json'
23
+ request["Authorization"] = 'bearer ' + token
24
+ request["Cache-Control"] = 'no-cache'
25
+ request.body = '{"uuid": "' + uuid + '", "password": "' + password + '", "rfc": "' + rfc + '", "b64Cer": "' + csd + '", "b64Key": "' + key + '"}'
26
+ response = http.request(request)
27
+ response_obj = GenericResponse.new(response)
28
+ return response_obj.validate_status_code(response_obj)
29
+ end
30
+
31
+ def self.relations_pfx(url, token, uuid, rfc, password, pfx)
32
+ url = URI(url + "/relations/pfx")
33
+ http = Net::HTTP.new(url.host, url.port)
34
+ request = Net::HTTP::Post.new(url)
35
+ request["Content-Type"] = 'application/json'
36
+ request["Authorization"] = 'bearer ' + token
37
+ request["Cache-Control"] = 'no-cache'
38
+ request.body = '{"uuid": "' + uuid + '", "password": "' + password + '", "rfc": "' + rfc + '", "b64Pfx": "' + pfx + '"}'
39
+ response = http.request(request)
40
+ response_obj = GenericResponse.new(response)
41
+ return response_obj.validate_status_code(response_obj)
42
+ end
43
+
44
+ def self.relations_xml(url, token, xml)
45
+ url = URI(url + "/relations/xml")
46
+ boundary = '----' + SecureRandom.urlsafe_base64
47
+ http = Net::HTTP.new(url.host, url.port)
48
+ request = Net::HTTP::Post.new(url)
49
+ request["Authorization"] = 'bearer ' + token
50
+ request["Content-Type"] = 'multipart/form-data;boundary="' + boundary + '"'
51
+ request["Cache-Control"] = 'no-cache'
52
+ request.body = "--" + boundary + "\r\nContent-Type: text/xml\r\nContent-Transfer-Encoding: binary\r\nContent-Disposition: form-data; name=\"xml\"; filename=\"xml\"\r\n\r\n" + xml + "\r\n--" + boundary + "--"
53
+ response = http.request(request)
54
+ response_obj = GenericResponse.new(response)
55
+ return response_obj.validate_status_code(response_obj)
56
+ end
57
+ end
data/lib/Response.rb ADDED
@@ -0,0 +1,46 @@
1
+ require_relative 'Services.rb'
2
+ class Response
3
+ @status = nil
4
+ @message = nil
5
+ @messageDetail = nil
6
+ @data = nil
7
+ @status_code = nil
8
+ @response = nil
9
+
10
+ def get_status
11
+ return @status
12
+ end
13
+ def get_message
14
+ return @message
15
+ end
16
+ def get_messageDetail
17
+ return @messageDetail
18
+ end
19
+ def get_data
20
+ return @data
21
+ end
22
+ def get_status_code
23
+ return @status_code
24
+ end
25
+ def get_response
26
+ return @response
27
+ end
28
+
29
+ def validate_status_code(response)
30
+ if(response.get_status == 'success')
31
+ return response
32
+ else
33
+ case response.get_status_code.to_i
34
+ when 300..399
35
+ Services::raise_exception("\nmessage: #{response.get_message}\nmessageDetail: #{response.get_messageDetail}\nstatus: #{response.get_status}\nstatusCode: #{response.get_status_code} Redireccion")
36
+ when 400..499
37
+ if response.get_message =~ /^307/
38
+ return response
39
+ end
40
+ Services::raise_exception("\nmessage: #{response.get_message}\nmessageDetail: #{response.get_messageDetail}\nstatus: #{response.get_status}\nstatusCode: #{response.get_status_code} Error del cliente")
41
+ when 500..599
42
+ Services::raise_exception("\nmessage: #{response.get_message}\nmessageDetail: #{response.get_messageDetail}\nstatus: #{response.get_status}\nstatusCode: #{response.get_status_code} Error del servidor")
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,30 @@
1
+ require_relative '../Response.rb'
2
+ require 'net/http'
3
+ require 'json'
4
+
5
+ class GenericResponse < Response
6
+ def initialize(response)
7
+ set(response)
8
+ end
9
+ def set(response)
10
+ begin
11
+ @status_code = response.code
12
+ if response.body
13
+ @response = JSON.parse(response.body)
14
+ if @status_code.to_i == 200
15
+ @status = @response['status']
16
+ @data = @response['data']
17
+ else
18
+ @status = 'error'
19
+ @message = @response['message']
20
+ @messageDetail = @response['messageDetail']
21
+ end
22
+ else
23
+ response.value
24
+ end
25
+ rescue => exception
26
+ @status = 'error'
27
+ @message = exception.backtrace
28
+ end
29
+ end
30
+ end
data/lib/Services.rb ADDED
@@ -0,0 +1,60 @@
1
+ require 'time'
2
+
3
+ class Services
4
+ @token = ""
5
+ @url = ""
6
+ @user = ""
7
+ @password = ""
8
+ @expiration_date = Time.at(0)
9
+
10
+ def self.get_url
11
+ return @url
12
+ end
13
+
14
+ def self.get_user
15
+ return @user
16
+ end
17
+
18
+ def self.get_password
19
+ return @password
20
+ end
21
+
22
+ def self.set_token(token)
23
+ @token = token
24
+ end
25
+
26
+ def self.get_token
27
+ if (@token.nil? or @token.empty?) or (Time.now > @expiration_date)
28
+ response_obj = Auth::authentication
29
+ @token = response_obj.get_token
30
+ @expiration_date = Time.at(response_obj.get_time_expire)
31
+ end
32
+ return @token
33
+ end
34
+
35
+ private
36
+ def self.raise_exception(text)
37
+ raise text
38
+ end
39
+
40
+ private
41
+ def self.set_data(params)
42
+ if (params.has_key?('url') and params['url'])
43
+ @url = params['url']
44
+ else
45
+ Services::raise_exception("URL debe especificarse")
46
+ end
47
+
48
+ if (params.has_key?('user') and params['user']) and (params.has_key?('password') and params['password'])
49
+ @user = params['user']
50
+ @password = params['password']
51
+ else if (params.has_key?('token') and params['token'])
52
+ @token = params['token']
53
+ @expiration_date = Time.at(999_999_999)
54
+ else
55
+ Services::raise_exception("Datos de autenticación deben especificarse")
56
+ end
57
+ end
58
+ end
59
+
60
+ end
@@ -0,0 +1,21 @@
1
+ require_relative '../Services.rb'
2
+ require_relative 'stamp_request.rb'
3
+
4
+ class Stamp < Services
5
+ def self.set(params)
6
+ Services::set_data(params)
7
+ end
8
+
9
+ def self.stamp_v1(xml, b64 = false)
10
+ SwStamp::stamp(Services::get_url, Services::get_token, 'v1', xml, b64)
11
+ end
12
+ def self.stamp_v2(xml, b64 = false)
13
+ SwStamp::stamp(Services::get_url, Services::get_token, 'v2', xml, b64)
14
+ end
15
+ def self.stamp_v3(xml, b64 = false)
16
+ SwStamp::stamp(Services::get_url, Services::get_token, 'v3', xml, b64)
17
+ end
18
+ def self.stamp_v4(xml, b64 = false)
19
+ SwStamp::stamp(Services::get_url, Services::get_token, 'v4', xml, b64)
20
+ end
21
+ end
@@ -0,0 +1,26 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'securerandom'
4
+ require_relative '../Response/generic_response.rb'
5
+
6
+ class SwStamp
7
+ def self.stamp(url, token, version, xml, base64)
8
+ b64 = ""
9
+ if base64
10
+ b64 = '/b64/'
11
+ end
12
+ boundary = '----' + SecureRandom.urlsafe_base64
13
+ body = "--" + boundary + "\r\nContent-Type: text/xml\r\nContent-Transfer-Encoding: binary\r\nContent-Disposition: form-data; name=\"xml\"; filename=\"xml\"\r\n\r\n" + xml + "\r\n--" + boundary + "--"
14
+ url = URI(url+"/cfdi33/stamp/"+version+b64)
15
+ http = Net::HTTP.new(url.host, url.port)
16
+ request = Net::HTTP::Post.new(url)
17
+ request["Authorization"] = 'bearer ' + token
18
+ request["Content-Type"] = 'multipart/form-data; boundary="' + boundary + '"'
19
+ request["Cache-Control"] = 'no-cache'
20
+ request["Content-length"] = body.length
21
+ request.body = body
22
+ response = http.request(request)
23
+ response_obj = GenericResponse.new(response)
24
+ return response_obj.validate_status_code(response_obj)
25
+ end
26
+ end
@@ -0,0 +1,7 @@
1
+ require_relative 'status_cfdi_request.rb'
2
+
3
+ class StatusCfdi
4
+ def self.status(rfc_emisor, rfc_receptor, total, uuid, url, action)
5
+ return SwStatusCfdi::status(url, action, rfc_emisor, rfc_receptor, total, uuid)
6
+ end
7
+ end
@@ -0,0 +1,30 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require_relative 'status_cfdi_response.rb'
4
+
5
+ class SwStatusCfdi
6
+ def self.status(url, action, rfc_emisor, rfc_receptor, total,uuid)
7
+ data = <<-EOF
8
+ <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
9
+ <soapenv:Header/>
10
+ <soapenv:Body>
11
+ <tem:Consulta>
12
+ <tem:expresionImpresa><![CDATA[?re=#{rfc_emisor.encode(:xml => :text)}&rr=#{rfc_receptor.encode(:xml => :text)}&tt=#{total}&id=#{uuid}]]></tem:expresionImpresa>
13
+ </tem:Consulta>
14
+ </soapenv:Body>
15
+ </soapenv:Envelope>
16
+ EOF
17
+
18
+ url = URI(url)
19
+ http = Net::HTTP.new(url.host, url.port)
20
+ http.use_ssl = true
21
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
22
+ request = Net::HTTP::Post.new(url)
23
+ request["Content-Type"] = 'text/xml; charset=utf-8'
24
+ request["SOAPAction"] = action
25
+ request.body = data
26
+ response = http.request(request)
27
+ response.body.force_encoding("UTF-8")
28
+ return StatusCfdiResponse.new(response)
29
+ end
30
+ end
@@ -0,0 +1,66 @@
1
+ class StatusCfdiResponse
2
+ @status_code = nil
3
+ @response = nil
4
+ @codigo_estatus = nil
5
+ @es_cancelable = nil
6
+ @estado = nil
7
+ @estatus_cancelacion = nil
8
+ def initialize(response)
9
+ set(response)
10
+ end
11
+
12
+ def set(response)
13
+ begin
14
+ @status_code = response.code
15
+ @response = response.read_body
16
+ @codigo_estatus = search_codigoEstatus(@response)
17
+ @es_cancelable = search_EsCancelable(@response)
18
+ @estado = search_Estado(@response)
19
+ @estatus_cancelacion = search_EsatusCancelacion(@response)
20
+ rescue
21
+ end
22
+ end
23
+ private
24
+ def search_codigoEstatus(response)
25
+ string = response.split("<a:CodigoEstatus>", 2)
26
+ string2 = string[1].split("</a:CodigoEstatus>", 2)
27
+ return string2[0]
28
+ end
29
+ private
30
+ def search_EsCancelable(response)
31
+ string = response.split("<a:EsCancelable>", 2)
32
+ string2 = string[1].split("</a:EsCancelable>", 2)
33
+ return string2[0]
34
+ end
35
+ private
36
+ def search_Estado(response)
37
+ string = response.split("<a:Estado>", 2)
38
+ string2 = string[1].split("</a:Estado>", 2)
39
+ return string2[0]
40
+ end
41
+ private
42
+ def search_EsatusCancelacion(response)
43
+ string = response.split("<a:EstatusCancelacion>", 2)
44
+ string2 = string[1].split("</a:EstatusCancelacion>", 2)
45
+ return string2[0]
46
+ end
47
+ public
48
+ def get_status_code
49
+ return @status_code
50
+ end
51
+ def get_response
52
+ return @response
53
+ end
54
+ def get_codigoEstatus
55
+ return @codigo_estatus
56
+ end
57
+ def get_esCancelable
58
+ return @es_cancelable
59
+ end
60
+ def get_estado
61
+ return @estado
62
+ end
63
+ def get_estatusCancelacion
64
+ return @estatus_cancelacion
65
+ end
66
+ end
@@ -0,0 +1,20 @@
1
+ require_relative '../Services.rb'
2
+ require_relative 'validate_request.rb'
3
+
4
+ class Validate < Services
5
+ def self.set(params)
6
+ Services::set_data(params)
7
+ end
8
+
9
+ def self.validate_xml(xml)
10
+ SwValidate::validate_xml(Services::get_url, Services::get_token, xml)
11
+ end
12
+
13
+ def self.validate_LRFC(rfc)
14
+ SwValidate::validate_LRFC(Services::get_url, Services::get_token, rfc)
15
+ end
16
+
17
+ def self.validate_LCO(numero_certificado)
18
+ SwValidate::validate_LCO(Services::get_url, Services::get_token, numero_certificado)
19
+ end
20
+ end
@@ -0,0 +1,44 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require_relative '../Response/generic_response.rb'
4
+ require 'securerandom'
5
+
6
+ class SwValidate
7
+ def self.validate_xml(url, token, xml)
8
+ boundary = '----' + SecureRandom.urlsafe_base64
9
+ cbody = '--' + boundary + '\r\nContent-Type: text/xml'+"\r\n" + 'Content-Transfer-Encoding: binary'+"\r\n"+'Content-Disposition: form-data; name="xml"; filename="33.xml"' + "\r\n\r\n" + xml + "\r\n"'--' + boundary + '-- '
10
+ url = URI(url+"/validate/cfdi33")
11
+ http = Net::HTTP.new(url.host, url.port)
12
+ request = Net::HTTP::Post.new(url)
13
+ request["Authorization"] = 'bearer ' + token
14
+ request["Content-Type"] = 'multipart/form-data; boundary="' + boundary + '"'
15
+ request["Content-Length"] = cbody.length
16
+ request["Cache-Control"] = 'no-cache'
17
+ request.body = cbody
18
+ response = http.request(request)
19
+ response_obj = GenericResponse.new(response)
20
+ return response_obj.validate_status_code(response_obj)
21
+ end
22
+
23
+ def self.validate_LRFC(url, token, rfc)
24
+ url = URI(url + "/lrfc/" + rfc)
25
+ http = Net::HTTP.new(url.host, url.port)
26
+ request = Net::HTTP::Get.new(url)
27
+ request["Authorization"] = 'bearer ' + token
28
+ request["Cache-Control"] = 'no-cache'
29
+ response = http.request(request)
30
+ response_obj = GenericResponse.new(response)
31
+ return response_obj.validate_status_code(response_obj)
32
+ end
33
+
34
+ def self.validate_LCO(url, token, num_cert)
35
+ url = URI(url + "/lco/" + num_cert)
36
+ http = Net::HTTP.new(url.host, url.port)
37
+ request = Net::HTTP::Get.new(url)
38
+ request["Authorization"] = 'bearer ' + token
39
+ request["Cache-Control"] = 'no-cache'
40
+ response = http.request(request)
41
+ response_obj = GenericResponse.new(response)
42
+ return response_obj.validate_status_code(response_obj)
43
+ end
44
+ end