nfse_gyn 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +83 -0
- data/LICENSE.txt +21 -0
- data/README.md +113 -0
- data/Rakefile +12 -0
- data/bin/console +27 -0
- data/bin/setup +8 -0
- data/lib/nfse_gyn/configuration.rb +35 -0
- data/lib/nfse_gyn/consultar_nfse.rb +58 -0
- data/lib/nfse_gyn/gerar_nfse.rb +49 -0
- data/lib/nfse_gyn/gerar_nfse_xml.rb +120 -0
- data/lib/nfse_gyn/response.rb +31 -0
- data/lib/nfse_gyn/sanitizer.rb +17 -0
- data/lib/nfse_gyn/soap_client.rb +31 -0
- data/lib/nfse_gyn/version.rb +5 -0
- data/lib/nfse_gyn.rb +28 -0
- data/nfse_gyn.gemspec +30 -0
- data/spec/fixtures/ssl/cert.pem +17 -0
- data/spec/fixtures/ssl/key.pem +18 -0
- data/spec/fixtures/xmls/invalid_consultar_nfse_response.xml +21 -0
- data/spec/fixtures/xmls/invalid_gerar_nfse_response.xml +25 -0
- data/spec/fixtures/xmls/valid_consultar_nfse_request.xml +13 -0
- data/spec/fixtures/xmls/valid_consultar_nfse_response.xml +93 -0
- data/spec/fixtures/xmls/valid_gerar_nfse_request.xml +75 -0
- data/spec/fixtures/xmls/valid_gerar_nfse_response.xml +95 -0
- data/spec/lib/configuration_spec.rb +48 -0
- data/spec/lib/consultar_nfse_spec.rb +45 -0
- data/spec/lib/gerar_nfse_spec.rb +50 -0
- data/spec/lib/gerar_nfse_xml_spec.rb +145 -0
- data/spec/lib/sanitizer_spec.rb +15 -0
- data/spec/nfse_gyn_spec.rb +11 -0
- data/spec/spec_helper.rb +32 -0
- metadata +193 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 01a27dc406ace444898a4dca5f3588e136367c7c6083f398cfe147bd87b4d667
|
4
|
+
data.tar.gz: e6257d16d7d6f976f6f9ce595ef9fb5892cff2079311f156f328be0099a32aaf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ea2d7880cd0ba4f2f4efbf56b7833a199616e4662b1edcf120d33e377decb0fb270c6b18d811461a72829492b67d639e851d3398929e787e9fa0e902e9eedbb7
|
7
|
+
data.tar.gz: aebb617cb978903d7b1f9e024909e8ec12443b5bfe05b37da0ef3d047f2ec5a2c727cab84bb45fb3621df3bf2e93d43c2d94359cd8c25db529e58c32145d651d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
nfse_gyn (0.1.0)
|
5
|
+
activemodel (~> 3.0)
|
6
|
+
savon (~> 2.9)
|
7
|
+
signer (~> 1.10)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
activemodel (3.2.22.5)
|
13
|
+
activesupport (= 3.2.22.5)
|
14
|
+
builder (~> 3.0.0)
|
15
|
+
activesupport (3.2.22.5)
|
16
|
+
i18n (~> 0.6, >= 0.6.4)
|
17
|
+
multi_json (~> 1.0)
|
18
|
+
addressable (2.8.0)
|
19
|
+
public_suffix (>= 2.0.2, < 5.0)
|
20
|
+
akami (1.3.1)
|
21
|
+
gyoku (>= 0.4.0)
|
22
|
+
nokogiri
|
23
|
+
builder (3.0.4)
|
24
|
+
coderay (1.1.3)
|
25
|
+
concurrent-ruby (1.1.9)
|
26
|
+
diff-lcs (1.5.0)
|
27
|
+
gyoku (1.4.0)
|
28
|
+
builder (>= 2.1.2)
|
29
|
+
rexml (~> 3.0)
|
30
|
+
httpi (2.4.5)
|
31
|
+
rack
|
32
|
+
socksify
|
33
|
+
i18n (0.9.5)
|
34
|
+
concurrent-ruby (~> 1.0)
|
35
|
+
method_source (1.0.0)
|
36
|
+
mini_portile2 (2.4.0)
|
37
|
+
multi_json (1.15.0)
|
38
|
+
nokogiri (1.9.1)
|
39
|
+
mini_portile2 (~> 2.4.0)
|
40
|
+
nori (2.6.0)
|
41
|
+
pry (0.14.2)
|
42
|
+
coderay (~> 1.1)
|
43
|
+
method_source (~> 1.0)
|
44
|
+
public_suffix (3.1.1)
|
45
|
+
rack (1.6.13)
|
46
|
+
rake (10.5.0)
|
47
|
+
rexml (3.2.5)
|
48
|
+
rspec (2.99.0)
|
49
|
+
rspec-core (~> 2.99.0)
|
50
|
+
rspec-expectations (~> 2.99.0)
|
51
|
+
rspec-mocks (~> 2.99.0)
|
52
|
+
rspec-core (2.99.2)
|
53
|
+
rspec-expectations (2.99.2)
|
54
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
55
|
+
rspec-mocks (2.99.4)
|
56
|
+
savon (2.12.1)
|
57
|
+
akami (~> 1.2)
|
58
|
+
builder (>= 2.1.2)
|
59
|
+
gyoku (~> 1.2)
|
60
|
+
httpi (~> 2.3)
|
61
|
+
nokogiri (>= 1.8.1)
|
62
|
+
nori (~> 2.4)
|
63
|
+
wasabi (~> 3.4)
|
64
|
+
signer (1.10.0)
|
65
|
+
nokogiri (>= 1.5.1, != 1.12.3, != 1.12.2, != 1.12.1, != 1.12.0)
|
66
|
+
socksify (1.7.1)
|
67
|
+
wasabi (3.7.0)
|
68
|
+
addressable
|
69
|
+
httpi (~> 2.0)
|
70
|
+
nokogiri (>= 1.4.2)
|
71
|
+
|
72
|
+
PLATFORMS
|
73
|
+
ruby
|
74
|
+
|
75
|
+
DEPENDENCIES
|
76
|
+
bundler (~> 1.7)
|
77
|
+
nfse_gyn!
|
78
|
+
pry
|
79
|
+
rake (~> 10.0)
|
80
|
+
rspec
|
81
|
+
|
82
|
+
BUNDLED WITH
|
83
|
+
1.17.3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Ramon Vicente
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
# NFSeGyn
|
2
|
+
|
3
|
+
NFSeGyn é uma gem que visa a integração com o webservice SOAP da prefeitura de Goiânia para Gerar NFS-e (Nota Fiscal de Serviços Eletrônica)
|
4
|
+
|
5
|
+
### Operações Suportadas:
|
6
|
+
* *GerarNfse*
|
7
|
+
* *ConsultarNfseRps*
|
8
|
+
|
9
|
+
## Instalação
|
10
|
+
|
11
|
+
Adicione essa linha na Gemfile da sua aplicação:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'nfse_gyn'
|
15
|
+
```
|
16
|
+
E então execute:
|
17
|
+
|
18
|
+
```bash
|
19
|
+
bundle
|
20
|
+
```
|
21
|
+
|
22
|
+
Ou instale você mesmo, conforme abaixo:
|
23
|
+
|
24
|
+
```bash
|
25
|
+
gem install nfse_gyn
|
26
|
+
```
|
27
|
+
|
28
|
+
## Como usar?
|
29
|
+
|
30
|
+
### Gerando os certificados necessários
|
31
|
+
|
32
|
+
```bash
|
33
|
+
# exportar o certificado apartir de *.pfx
|
34
|
+
$ openssl pkcs12 -in expired_certificate.pfx -clcerts -nokeys -out cert.crt
|
35
|
+
|
36
|
+
# converte o certificado .cer para o formato .pem
|
37
|
+
$ openssl x509 -in cert.crt -out cert.pem -outform PEM
|
38
|
+
|
39
|
+
# exportar a chave privada apartir de *.pfx
|
40
|
+
$ openssl pkcs12 -in expired_certificate.pfx -nocerts -out cert_key.key
|
41
|
+
```
|
42
|
+
|
43
|
+
### Configurando os certificados
|
44
|
+
|
45
|
+
Crie um arquivo `config/initializers/nfse_gyn.rb` e adicione:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
NfseGyn.configure do |nfse|
|
49
|
+
nfse.test_mode = true
|
50
|
+
nfse.log_level = :debug
|
51
|
+
nfse.cnpj = '000000000000000'
|
52
|
+
nfse.inscricao_municipal = '0000000'
|
53
|
+
nfse.codigo_tributacao_municipio = '00000000'
|
54
|
+
nfse.valor_aliquota = 0
|
55
|
+
nfse.cert_path = '/path/to/certificate.pem'
|
56
|
+
nfse.cert_key_path = '/path/to/key.pem'
|
57
|
+
nfse.cert_key_password = 'certificate_key_password'
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
### Criando uma NFS-e
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
# valores a serem usados na NFSeGyn a ser gerada
|
65
|
+
nota_fiscal_info = {
|
66
|
+
identification_number: '3033',
|
67
|
+
description: 'Teste de RPS',
|
68
|
+
data_emissao: '2024-01-11T23:33:22',
|
69
|
+
total: 100.10,
|
70
|
+
customer: {
|
71
|
+
document_type: 'Cnpj',
|
72
|
+
document_number: '98924674000187',
|
73
|
+
name: 'XPTO Tecnologia Ltda',
|
74
|
+
address: {
|
75
|
+
street: 'Rua das Rosas',
|
76
|
+
number: '111',
|
77
|
+
complement: 'Sobre Loja',
|
78
|
+
neighborhood: 'Centro',
|
79
|
+
city_ibge_code: '0025300',
|
80
|
+
state_code: 'GO',
|
81
|
+
zipcode: '74672680'
|
82
|
+
}
|
83
|
+
}
|
84
|
+
}
|
85
|
+
|
86
|
+
# Operação GerarNfse
|
87
|
+
service = NfseGyn::GerarNfse.new(nota_fiscal_info)
|
88
|
+
|
89
|
+
# Operação ConsultarNfseRps
|
90
|
+
service = NfseGyn::ConsultarNfse.new(14, 'TESTE', 1)
|
91
|
+
|
92
|
+
response = service.execute!
|
93
|
+
```
|
94
|
+
|
95
|
+
## Testes
|
96
|
+
|
97
|
+
Para rodar os testes, execute:
|
98
|
+
|
99
|
+
```bash
|
100
|
+
bundle exec rspec
|
101
|
+
```
|
102
|
+
|
103
|
+
## Contribuindo
|
104
|
+
|
105
|
+
1. Faça um Fork
|
106
|
+
2. Crie um branch para a nova funcionalidade (`git checkout -b minha-nova-funcionalidade`)
|
107
|
+
3. Faça o commit de suas alterações (`git commit -am 'Adicionada nova funcionalidade'`)
|
108
|
+
4. Faça um push da sua nova funconalidade (`git push origin minha-nova-funcionalidade`)
|
109
|
+
5. Submeta uma nova Pull Request
|
110
|
+
|
111
|
+
## Creditos
|
112
|
+
|
113
|
+
Essa gem é baseada na gem [nfse-carioca](https://rubygems.org/gems/nfse-carioca)
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "pry"
|
6
|
+
require_relative '../lib/nfse_gyn'
|
7
|
+
require "irb"
|
8
|
+
|
9
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
10
|
+
# with your gem easier. You can also use a different console, if you like.
|
11
|
+
|
12
|
+
|
13
|
+
def reload!(print = true)
|
14
|
+
puts 'Reloading ...' if print
|
15
|
+
# Main project directory.
|
16
|
+
root_dir = File.expand_path('..', __dir__)
|
17
|
+
# Directories within the project that should be reloaded.
|
18
|
+
reload_dirs = %w{lib}
|
19
|
+
# Loop through and reload every file in all relevant project directories.
|
20
|
+
reload_dirs.each do |dir|
|
21
|
+
Dir.glob("#{root_dir}/#{dir}/**/*.rb").each { |f| load(f) }
|
22
|
+
end
|
23
|
+
# Return true when complete.
|
24
|
+
true
|
25
|
+
end
|
26
|
+
|
27
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
module NfseGyn
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :wsdl,
|
4
|
+
:test_mode,
|
5
|
+
:log_level,
|
6
|
+
:ca_cert_path,
|
7
|
+
:cert_key_path,
|
8
|
+
:cert_path,
|
9
|
+
:cert_key_password,
|
10
|
+
:cnpj,
|
11
|
+
:inscricao_municipal,
|
12
|
+
:codigo_municipio,
|
13
|
+
:rps_tipo,
|
14
|
+
:valor_aliquota,
|
15
|
+
:codigo_tributacao_municipio
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@test_mode = false
|
19
|
+
@log_level = :debug
|
20
|
+
@wsdl = 'https://nfse.goiania.go.gov.br/ws/nfse.asmx?wsdl'
|
21
|
+
|
22
|
+
@codigo_municipio = '0025300'
|
23
|
+
@rps_tipo = 1
|
24
|
+
|
25
|
+
@valor_aliquota = 2
|
26
|
+
|
27
|
+
@codigo_tributacao_municipio = ''
|
28
|
+
end
|
29
|
+
|
30
|
+
def rps_serie
|
31
|
+
return 'TESTE' if test_mode
|
32
|
+
'A'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module NfseGyn
|
2
|
+
class ConsultarNfse
|
3
|
+
attr_reader :invoice_number
|
4
|
+
attr_reader :invoice_serie
|
5
|
+
attr_reader :invoice_type
|
6
|
+
|
7
|
+
def initialize(invoice_number, invoice_serie, invoice_type)
|
8
|
+
@client = NfseGyn::SoapClient.new
|
9
|
+
@invoice_number = invoice_number
|
10
|
+
@invoice_serie = invoice_serie
|
11
|
+
@invoice_type = invoice_type
|
12
|
+
end
|
13
|
+
|
14
|
+
def execute!
|
15
|
+
@client.call(:consultar_nfse_rps, self)
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_xml
|
19
|
+
Gyoku.xml(
|
20
|
+
'ConsultarNfseRpsEnvio' => {
|
21
|
+
'@xmlns' => 'http://nfse.goiania.go.gov.br/xsd/nfse_gyn_v02.xsd',
|
22
|
+
|
23
|
+
'IdentificacaoRps' => {
|
24
|
+
'Numero' => invoice_number,
|
25
|
+
'Serie' => invoice_serie,
|
26
|
+
'Tipo' => invoice_type
|
27
|
+
},
|
28
|
+
'Prestador' => {
|
29
|
+
'CpfCnpj' => {
|
30
|
+
'Cnpj' => NfseGyn.configuration.cnpj
|
31
|
+
},
|
32
|
+
'InscricaoMunicipal' => NfseGyn.configuration.inscricao_municipal
|
33
|
+
}
|
34
|
+
}
|
35
|
+
)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
class Response < NfseGyn::Response
|
41
|
+
def content
|
42
|
+
@content ||= output['ConsultarNfseRpsResposta']
|
43
|
+
end
|
44
|
+
|
45
|
+
def number
|
46
|
+
content['CompNfse']['Nfse']['InfNfse']['Numero'] if successful?
|
47
|
+
end
|
48
|
+
|
49
|
+
def verification_code
|
50
|
+
content['CompNfse']['Nfse']['InfNfse']['CodigoVerificacao'] if successful?
|
51
|
+
end
|
52
|
+
|
53
|
+
def error?
|
54
|
+
!content['CompNfse'] || content['ListaMensagemRetorno']['MensagemRetorno']['Codigo'] != 'L000'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'nfse_gyn/gerar_nfse_xml'
|
2
|
+
|
3
|
+
module NfseGyn
|
4
|
+
class GerarNfse
|
5
|
+
def initialize(invoice)
|
6
|
+
@invoice = invoice
|
7
|
+
@client = NfseGyn::SoapClient.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def execute!
|
11
|
+
@client.call(:gerar_nfse, self)
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_xml
|
15
|
+
GerarNfseXML.new(@invoice).to_xml
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
class Response < NfseGyn::Response
|
21
|
+
def content
|
22
|
+
@content ||= output['GerarNfseResposta']
|
23
|
+
end
|
24
|
+
|
25
|
+
def number
|
26
|
+
content['ListaNfse']['CompNfse']['Nfse']['InfNfse']['Numero'] if successful?
|
27
|
+
end
|
28
|
+
|
29
|
+
def verification_code
|
30
|
+
content['ListaNfse']['CompNfse']['Nfse']['InfNfse']['CodigoVerificacao'] if successful?
|
31
|
+
end
|
32
|
+
|
33
|
+
def error?
|
34
|
+
!content['ListaNfse'] || content['ListaMensagemRetorno']['MensagemRetorno']['Codigo'] != 'L000'
|
35
|
+
end
|
36
|
+
|
37
|
+
def errors
|
38
|
+
if error?
|
39
|
+
message = content['ListaMensagemRetorno']['MensagemRetorno']
|
40
|
+
message = [message] if message.is_a?(Hash)
|
41
|
+
|
42
|
+
message
|
43
|
+
end
|
44
|
+
rescue StandardError
|
45
|
+
content
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_support/core_ext'
|
5
|
+
require 'signer'
|
6
|
+
|
7
|
+
module NfseGyn
|
8
|
+
class GerarNfseXML
|
9
|
+
def initialize(invoice_info)
|
10
|
+
@invoice = invoice_info
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_xml
|
14
|
+
"<GerarNfseEnvio xmlns=\"http://nfse.goiania.go.gov.br/xsd/nfse_gyn_v02.xsd\">#{sign(body)}</GerarNfseEnvio>"
|
15
|
+
end
|
16
|
+
|
17
|
+
def body
|
18
|
+
Gyoku.xml(
|
19
|
+
'Rps' => {
|
20
|
+
'InfDeclaracaoPrestacaoServico' => {
|
21
|
+
'@xmlns' => 'http://nfse.goiania.go.gov.br/xsd/nfse_gyn_v02.xsd',
|
22
|
+
'Rps' => {
|
23
|
+
'@Id' => @invoice[:identification_number],
|
24
|
+
'IdentificacaoRps' => {
|
25
|
+
'Numero' => @invoice[:identification_number],
|
26
|
+
'Serie' => NfseGyn.configuration.rps_serie,
|
27
|
+
'Tipo' => NfseGyn.configuration.rps_tipo
|
28
|
+
},
|
29
|
+
'DataEmissao' => @invoice[:data_emissao],
|
30
|
+
'Status' => 1
|
31
|
+
},
|
32
|
+
|
33
|
+
'Servico' => {
|
34
|
+
'Valores' => {
|
35
|
+
'ValorServicos' => @invoice[:total],
|
36
|
+
'Aliquota' => NfseGyn.configuration.valor_aliquota
|
37
|
+
},
|
38
|
+
|
39
|
+
'CodigoTributacaoMunicipio' => NfseGyn.configuration.codigo_tributacao_municipio,
|
40
|
+
'Discriminacao' => clear(@invoice[:description]),
|
41
|
+
'CodigoMunicipio' => NfseGyn.configuration.codigo_municipio
|
42
|
+
},
|
43
|
+
|
44
|
+
'Prestador' => {
|
45
|
+
'CpfCnpj' => {
|
46
|
+
'Cnpj' => NfseGyn.configuration.cnpj
|
47
|
+
},
|
48
|
+
'InscricaoMunicipal' => NfseGyn.configuration.inscricao_municipal
|
49
|
+
},
|
50
|
+
|
51
|
+
'Tomador' => tomador
|
52
|
+
}
|
53
|
+
}
|
54
|
+
)
|
55
|
+
end
|
56
|
+
|
57
|
+
def sign(xml)
|
58
|
+
return xml if !certificate.present? || !private_key.present?
|
59
|
+
signer = Signer.new(xml, canonicalize_algorithm: :c14n_1_0, wss: false)
|
60
|
+
signer.cert = OpenSSL::X509::Certificate.new(certificate)
|
61
|
+
signer.private_key = OpenSSL::PKey::RSA.new(private_key, NfseGyn.configuration.cert_key_password)
|
62
|
+
signer.security_node = signer.document.root
|
63
|
+
node = signer.document.dup.at_xpath("//*[@Id=#{@invoice[:identification_number]}]")
|
64
|
+
signer.digest!(node, enveloped: true)
|
65
|
+
signer.sign!(issuer_serial: true)
|
66
|
+
signer.document.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION).strip
|
67
|
+
end
|
68
|
+
|
69
|
+
def certificate
|
70
|
+
@certificate ||= File.read(NfseGyn.configuration.cert_path) if NfseGyn.configuration.cert_path.present?
|
71
|
+
end
|
72
|
+
|
73
|
+
def private_key
|
74
|
+
@private_key ||= File.read(NfseGyn.configuration.cert_key_path) if NfseGyn.configuration.cert_key_path.present?
|
75
|
+
end
|
76
|
+
|
77
|
+
def clear(value)
|
78
|
+
NfseGyn::Sanitizer.new(value).clear
|
79
|
+
end
|
80
|
+
|
81
|
+
def document_type
|
82
|
+
@invoice[:customer][:document_type]
|
83
|
+
end
|
84
|
+
|
85
|
+
def document_number
|
86
|
+
@invoice[:customer][:document_number]
|
87
|
+
end
|
88
|
+
|
89
|
+
def tomador
|
90
|
+
xml = {}
|
91
|
+
|
92
|
+
if document_type.present?
|
93
|
+
xml['IdentificacaoTomador'] = {
|
94
|
+
'CpfCnpj' => { document_type => document_number }
|
95
|
+
}
|
96
|
+
end
|
97
|
+
|
98
|
+
if @invoice[:customer][:name].present?
|
99
|
+
xml['RazaoSocial'] = clear(@invoice[:customer][:name])
|
100
|
+
end
|
101
|
+
|
102
|
+
if @invoice[:customer][:address].present?
|
103
|
+
xml['Endereco'] = {
|
104
|
+
'Endereco' => clear(@invoice[:customer][:address][:street]),
|
105
|
+
'Numero' => clear(@invoice[:customer][:address][:number])
|
106
|
+
}
|
107
|
+
|
108
|
+
if @invoice[:customer][:address][:complement].present?
|
109
|
+
xml['Endereco']['Complemento'] = clear(@invoice[:customer][:address][:complement])
|
110
|
+
end
|
111
|
+
|
112
|
+
xml['Endereco']['Bairro'] = clear(@invoice[:customer][:address][:neighborhood])
|
113
|
+
xml['Endereco']['CodigoMunicipio'] = @invoice[:customer][:address][:city_ibge_code]
|
114
|
+
xml['Endereco']['Uf'] = @invoice[:customer][:address][:state_code]
|
115
|
+
xml['Endereco']['Cep'] = @invoice[:customer][:address][:zipcode].scan(/\d/).join
|
116
|
+
end
|
117
|
+
xml
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module NfseGyn
|
2
|
+
class Response
|
3
|
+
def initialize(payload)
|
4
|
+
@payload = payload
|
5
|
+
end
|
6
|
+
|
7
|
+
def class_name
|
8
|
+
self.class.to_s.split('::')[1]
|
9
|
+
end
|
10
|
+
|
11
|
+
def successful?
|
12
|
+
!error?
|
13
|
+
end
|
14
|
+
|
15
|
+
def error?
|
16
|
+
binding.pry
|
17
|
+
content['ListaMensagemRetorno'].present?
|
18
|
+
end
|
19
|
+
|
20
|
+
def error_message
|
21
|
+
if error? && content['ListaMensagemRetorno']['MensagemRetorno'].present?
|
22
|
+
content['ListaMensagemRetorno']['MensagemRetorno']['Correcao'].try(:strip)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def output
|
27
|
+
response = @payload.body.try(:values).try(:first)
|
28
|
+
@output ||= Nori.new.parse(response.try(:values).try(:first) || response)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module NfseGyn
|
2
|
+
class Sanitizer
|
3
|
+
attr_reader :value
|
4
|
+
|
5
|
+
def initialize(value)
|
6
|
+
@value = value
|
7
|
+
end
|
8
|
+
|
9
|
+
def clear
|
10
|
+
return nil if value.nil?
|
11
|
+
temp = I18n.transliterate(value)
|
12
|
+
temp.tr!('-', ' ')
|
13
|
+
temp.gsub!(/,|\./, '')
|
14
|
+
temp.scan(/\w+|\s+/i).join
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'savon'
|
2
|
+
|
3
|
+
module NfseGyn
|
4
|
+
class SoapClient
|
5
|
+
def call(method, object)
|
6
|
+
xml = "<ArquivoXML><![CDATA[#{object.to_xml}]]></ArquivoXML>"
|
7
|
+
payload = client.call(method, message: xml)
|
8
|
+
object.class::Response.new payload
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def client
|
14
|
+
@client ||= Savon.client(
|
15
|
+
wsdl: NfseGyn.configuration.wsdl,
|
16
|
+
env_namespace: :soap,
|
17
|
+
namespace_identifier: nil,
|
18
|
+
element_form_default: :unqualified,
|
19
|
+
ssl_verify_mode: :none,
|
20
|
+
ssl_cert_file: NfseGyn.configuration.cert_path,
|
21
|
+
ssl_cert_key_file: NfseGyn.configuration.cert_key_path,
|
22
|
+
ssl_ca_cert_file: NfseGyn.configuration.ca_cert_path,
|
23
|
+
ssl_cert_key_password: NfseGyn.configuration.cert_key_password,
|
24
|
+
log_level: NfseGyn.configuration.log_level,
|
25
|
+
log: true,
|
26
|
+
open_timeout: 120,
|
27
|
+
read_timeout: 120
|
28
|
+
)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/nfse_gyn.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ostruct'
|
4
|
+
require 'active_model'
|
5
|
+
|
6
|
+
require 'nfse_gyn/version'
|
7
|
+
require 'nfse_gyn/configuration'
|
8
|
+
require 'nfse_gyn/sanitizer'
|
9
|
+
require 'nfse_gyn/soap_client'
|
10
|
+
require 'nfse_gyn/response'
|
11
|
+
require 'nfse_gyn/gerar_nfse'
|
12
|
+
require 'nfse_gyn/consultar_nfse'
|
13
|
+
|
14
|
+
HTTPI.adapter = :net_http
|
15
|
+
|
16
|
+
module NfseGyn
|
17
|
+
def self.configure
|
18
|
+
yield configuration
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.configuration
|
22
|
+
@configuration ||= NfseGyn::Configuration.new
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.reset!
|
26
|
+
@configuration = nil
|
27
|
+
end
|
28
|
+
end
|