1+ using DFe . Classes . Entidades ;
2+ using DFe . Classes . Flags ;
3+ using DFe . Utils ;
4+ using DFe . Utils . Assinatura ;
5+ using NFe . Classes ;
6+ using NFe . Classes . Informacoes . Identificacao . Tipos ;
17using System ;
28using System . Collections . Generic ;
39using System . Linq ;
410using System . Security . Cryptography ;
511using System . Security . Cryptography . X509Certificates ;
612using System . Text ;
7- using DFe . Classes . Entidades ;
8- using DFe . Classes . Flags ;
9- using DFe . Utils ;
10- using NFe . Classes ;
11- using NFe . Classes . Informacoes . Identificacao . Tipos ;
1213
1314namespace NFe . Utils . InformacoesSuplementares
1415{
@@ -358,7 +359,7 @@ public static string ObterUrlConsulta(this infNFeSupl infNFeSupl, Classes.NFe nf
358359 /// <summary>
359360 /// Obtém a URL para montagem do QR-Code
360361 /// </summary>
361- public static string ObterUrlQrCode ( this infNFeSupl infNFeSupl , Classes . NFe nfe , VersaoQrCode versaoQrCode , string cIdToken , string csc )
362+ public static string ObterUrlQrCode ( this infNFeSupl infNFeSupl , Classes . NFe nfe , VersaoQrCode versaoQrCode , string cIdToken , string csc , ConfiguracaoCertificado _cfgCertificado = null )
362363 {
363364 Func < string , string > msgErro = parametro => $ "O { parametro } não foi informado!";
364365
@@ -376,7 +377,7 @@ public static string ObterUrlQrCode(this infNFeSupl infNFeSupl, Classes.NFe nfe,
376377 case VersaoQrCode . QrCodeVersao2 :
377378 return ObterUrlQrCode2 ( infNFeSupl , nfe , cIdToken , csc , versaoServico ) ;
378379 case VersaoQrCode . QrCodeVersao3 :
379- throw new ArgumentOutOfRangeException ( "versaoQrCode" , versaoQrCode , "Para versão 3.0 do QR-Code utilize a função ObterUrlQrCode3" ) ;
380+ return ObterUrlQrCode3 ( infNFeSupl , nfe , versaoServico , _cfgCertificado ) ;
380381 default :
381382 throw new ArgumentOutOfRangeException ( "versaoQrCode" , versaoQrCode , null ) ;
382383 }
@@ -495,32 +496,22 @@ public static string ObterUrlQrCode2ComParametro(this infNFeSupl infNFeSupl, Tip
495496 /// <summary>
496497 /// Obtém a URL para uso no QR-Code, versão 3.0 - leiaute 4.00+
497498 /// </summary>
498- public static string ObterUrlQrCode3 ( infNFeSupl infNFeSupl , Classes . NFe nfe , VersaoServico versaoServico , X509Certificate2 certificadoDigital , Encoding encoding = null )
499+ public static string ObterUrlQrCode3 ( this infNFeSupl infNFeSupl , Classes . NFe nfe , VersaoServico versaoServico , ConfiguracaoCertificado cfgCertificado , Encoding encoding = null )
499500 {
500- const string pipe = "|" ;
501+ if ( cfgCertificado == null || string . IsNullOrWhiteSpace ( cfgCertificado . Serial ) )
502+ throw new ArgumentNullException ( "CertificadoDigital" , "Para gerar a assinatura do QR-Code versão 3.0 EM CONTINGENCIA é necessário informar o certificado digital utilizado na assinatura da NFC-e, verificar Número de Série e Senha." ) ;
501503
502- // Chave de Acesso da NFC-e
503- var chave = nfe . infNFe . Id . Substring ( 3 ) ;
504- // Versão do QR-Code
505- var versaoQrCode = 3 ;
506- // Identificação do Ambiente (1 – Produção, 2 – Homologação)
507- var ambiente = ( int ) nfe . infNFe . ide . tpAmb ;
504+ const string pipe = "|" ;
508505
509- // Monta os parâmetros base
510- var dadosBase = string . Concat (
511- chave , pipe ,
512- versaoQrCode , pipe ,
513- ambiente
514- ) ;
506+ string chave = nfe . infNFe . Id . Substring ( 3 ) ;
507+ int versaoQrCode = 3 ;
508+ int ambiente = ( int ) nfe . infNFe . ide . tpAmb ;
509+ string dadosBase = string . Concat ( chave , pipe , versaoQrCode , pipe , ambiente ) ;
515510
516511 if ( nfe . infNFe . ide . tpEmis == TipoEmissao . teOffLine )
517512 {
518- // Dia da emissão
519- var diaEmi = nfe . infNFe . ide . dhEmi . Day . ToString ( "D2" ) ;
520- // Valor total da NFC-e
521- var valorNfce = nfe . infNFe . total . ICMSTot . vNF . ToString ( "0.00" ) . Replace ( ',' , '.' ) ;
522-
523- // tp_idDest e idDest
513+ string diaEmi = nfe . infNFe . ide . dhEmi . Day . ToString ( "D2" ) ;
514+ string valorNfce = nfe . infNFe . total . ICMSTot . vNF . ToString ( "0.00" ) . Replace ( ',' , '.' ) ;
524515 string tp_idDest = string . Empty ;
525516 string idDest = string . Empty ;
526517 if ( nfe . infNFe . dest != null )
@@ -542,44 +533,17 @@ public static string ObterUrlQrCode3(infNFeSupl infNFeSupl, Classes.NFe nfe, Ver
542533 }
543534 }
544535
545- // Monta os parâmetros base
546- dadosBase = string . Concat ( dadosBase , pipe ,
547- diaEmi , pipe ,
548- valorNfce , pipe ,
549- tp_idDest , pipe ,
550- idDest
551- ) ;
536+ dadosBase = string . Concat ( dadosBase , pipe , diaEmi , pipe , valorNfce , pipe , tp_idDest , pipe , idDest ) ;
552537
553538 if ( encoding == null )
554539 encoding = Encoding . UTF8 ;
555540
556- if ( certificadoDigital == null )
557- throw new ArgumentNullException ( nameof ( certificadoDigital ) , "Para gerar a assinatura do QR-Code versão 3.0 EM CONTINGENCIA é necessário informar o certificado digital utilizado na assinatura da NFC-e." ) ;
558-
559- // Assinatura SHA-1 dos parâmetros COM uso do certificado digital
560- var assinatura = Convert . ToBase64String ( CreateSignaturePkcs1 ( certificadoDigital , encoding . GetBytes ( dadosBase ) ) ) ;
541+ string assinatura = Convert . ToBase64String ( CertificadoDigital . ObterAssinaturaPkcs1 ( cfgCertificado , encoding . GetBytes ( dadosBase ) ) ) ;
561542 dadosBase = string . Concat ( dadosBase , pipe , assinatura ) ;
562543 }
563544
564- // Monta a URL base (ja com ?p= ao final)
565- var url = ObterUrlQrCode2ComParametro ( infNFeSupl , nfe . infNFe . ide . tpAmb , nfe . infNFe . ide . cUF , versaoServico ) ;
566-
545+ string url = ObterUrlQrCode2ComParametro ( infNFeSupl , nfe . infNFe . ide . tpAmb , nfe . infNFe . ide . cUF , versaoServico ) ;
567546 return string . Concat ( url , dadosBase ) ;
568547 }
569-
570- private static byte [ ] CreateSignaturePkcs1 ( X509Certificate2 certificado , byte [ ] Value )
571- {
572- var rsa = certificado . GetRSAPrivateKey ( ) ;
573-
574- RSAPKCS1SignatureFormatter rsaF = new RSAPKCS1SignatureFormatter ( rsa ) ;
575-
576- SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider ( ) ;
577-
578- byte [ ] hash = sha1 . ComputeHash ( Value ) ;
579-
580- rsaF . SetHashAlgorithm ( "SHA1" ) ;
581-
582- return rsaF . CreateSignature ( hash ) ;
583- }
584548 }
585549}
0 commit comments