class ManifestacaoSefaz::Manifesto

Attributes

CNPJ[RW]
chNFe[RW]
private_key_file_name[RW]
private_key_file_password[RW]
x509_certificate_file_name[RW]

Public Instance Methods

cancelar_nfe!() click to toggle source
# File lib/manifestacao_sefaz.rb, line 10
          def cancelar_nfe!
                  if self.private_key_file_name.nil? || self.private_key_file_password.nil? || self.x509_certificate_file_name.nil? || self.CNPJ.nil? || self.chNFe.nil?
                                  return "Campos Obrigatórios não atribuídos e operação abortada: private_key_file_name, private_key_file_password, x509_certificate_file_name, CNPJ, chNFe"
                  end

                  cod_evento = "210240"
                  descricao_evento = "Operação não realizada"

headers = {'Content-Type' => 'text/xml; charset=UTF-8'}

uri = URI.parse('https://www.nfe.fazenda.gov.br/RecepcaoEvento/RecepcaoEvento.asmx?op=nfeRecepcaoEvento')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

p "#{Rails.public_path}/#{self.private_key_file_name}"
p "#{Rails.public_path}/#{self.x509_certificate_file_name}"

private_key = OpenSSL::PKey::RSA.new(File.read("#{Rails.public_path}/#{self.private_key_file_name}"), self.private_key_file_password)
certificate = OpenSSL::X509::Certificate.new(File.read("#{Rails.public_path}/#{self.x509_certificate_file_name}"))



                  http.key = private_key
http.cert = certificate

dh = DateTime.now.xmlschema

xml = %{<envEvento versao="1.00" xmlns="http://www.portalfiscal.inf.br/nfe">
                  <idLote>135170005324982</idLote>
                  <evento versao="1.00">
                          <infEvento Id="ID#{cod_evento}#{chNFe}01">
                              <cOrgao>91</cOrgao>
                              <tpAmb>1</tpAmb>
                              <CNPJ>#{self.CNPJ}</CNPJ>
                              <chNFe>#{self.chNFe}</chNFe>
                              <dhEvento>#{dh}</dhEvento>
                              <tpEvento>#{cod_evento}</tpEvento>
                              <nSeqEvento>1</nSeqEvento>
                              <verEvento>1.00</verEvento>
                              <detEvento versao="1.00">
                                  <descEvento>#{descricao_evento}</descEvento>
                                  <xJust>Confirmacao indevida</xJust>
                              </detEvento>
                          </infEvento>
                  </evento>
              </envEvento>}.gsub(/\>\s{1,}\</,"><").gsub(/\>\s{2,}\</,"><").gsub("\" >","\">").strip

doc = Nokogiri::XML.parse xml
evento = doc.css("infEvento").first.clone
evento["xmlns"] = "http://www.portalfiscal.inf.br/nfe"            
  
digest_value = Base64.encode64(OpenSSL::Digest::SHA1.digest(Nokogiri::XML(evento.to_html).canonicalize(Nokogiri::XML::XML_C14N_1_1))).strip

str_signature =                   %{<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
                          <SignedInfo>
                              <CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\"/>
                              <SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"/>
                              <Reference>
                                  <Transforms>
                                      <Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\"/>
                                      <Transform Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\"/>
                                  </Transforms>
                                  <DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>
                                  <DigestValue>?</DigestValue>
                              </Reference>
                          </SignedInfo>
                          <SignatureValue>?</SignatureValue>
                          <KeyInfo>
                              <X509Data>
                                  <X509Certificate>?</X509Certificate>
                              </X509Data>
                          </KeyInfo>
                      </Signature>}.gsub(/\>\s{1,}\</,"><").gsub(/\>\s{2,}\</,"><").gsub("\" >","\">").strip

signature = Nokogiri::XML.parse str_signature
signature.css("DigestValue").first.content = digest_value
signature.css("Reference").first["URI"] = "#ID#{cod_evento}#{self.chNFe}01"
signature.css("SignedInfo").first["xmlns"] = "http://www.w3.org/2000/09/xmldsig#"
signature.css("X509Certificate").first.content = "#{certificate.to_s.gsub(/\-+[A-Z]+ CERTIFICATE\-+/, "").strip()}"

signed_info = signature.css("SignedInfo").first
signed_info["xmlns"] = "http://www.w3.org/2000/09/xmldsig#"

signature_value = Base64.encode64(private_key.sign(OpenSSL::Digest::SHA1.new, Nokogiri::XML(signed_info.to_html).canonicalize(Nokogiri::XML::XML_C14N_1_1))).strip
signature.css("//SignatureValue").first.content = signature_value.strip
doc.css("envEvento evento infEvento").first.add_next_sibling signature.to_html

xml_final = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
                  <soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">
                  <soap12:Header>
                      <nfeCabecMsg xmlns=\"http://www.portalfiscal.inf.br/nfe/wsdl/RecepcaoEvento\">
                          <versaoDados>1.00</versaoDados>
                          <cUF>91</cUF>
                      </nfeCabecMsg>
                  </soap12:Header>
                  <soap12:Body>
                   <nfeDadosMsg xmlns=\"http://www.portalfiscal.inf.br/nfe/wsdl/RecepcaoEvento\">
                      #{doc.to_xml.gsub(/\>\s{1,}\</,"><").gsub("<?xml version=\"1.0\"?>","")}
                   </nfeDadosMsg>
                  </soap12:Body>
              </soap12:Envelope>"

resp, body = http.post(uri.path, xml_final , headers)

     # manifestacao = ManifestacaoRetorno.new()
     # hash = CobraVsMongoose.xml_to_hash(resp.body)
     # manifestacao.cStat = hash["soap:Envelope"]["soap:Body"]["nfeRecepcaoEventoResult"]["retEnvEvento"]["retEvento"]["infEvento"]["cStat"]["$"]

     # if resp.code == "200"
     #      manifestacao.tpEvento = hash["soap:Envelope"]["soap:Body"]["nfeRecepcaoEventoResult"]["retEnvEvento"]["retEvento"]["infEvento"]["tpEvento"]["$"]
     #      manifestacao.xEvento = hash["soap:Envelope"]["soap:Body"]["nfeRecepcaoEventoResult"]["retEnvEvento"]["retEvento"]["infEvento"]["xEvento"]["$"]
     #      if manifestacao.cStat == 135
     #          manifestacao.CNPJDest = hash["soap:Envelope"]["soap:Body"]["nfeRecepcaoEventoResult"]["retEnvEvento"]["retEvento"]["infEvento"]["CNPJDest"]["$"]
     #      end
     #  end

     #  manifestacao.xMotivo =  hash["soap:Envelope"]["soap:Body"]["nfeRecepcaoEventoResult"]["retEnvEvento"]["retEvento"]["infEvento"]["xMotivo"]["$"]
     #  manifestacao.user_id = user.id
     #  manifestacao.xml = resp.body
     #  manifestacao.nfe_id = self.id
     #  manifestacao.code = resp.code

     #  manifestacao.save!

  puts resp.body
  end