Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update AssessorPublicoServiceClient.cs #58

Merged
merged 1 commit into from
Apr 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@
using OpenAC.Net.Core.Extensions;
using OpenAC.Net.DFe.Core;
using System;
using System.Collections.Specialized;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Xml.Linq;

namespace OpenAC.Net.NFSe.Providers
{
internal sealed class AssessorPublicoServiceClient : NFSeHttpServiceClient, IServiceClient
internal sealed class AssessorPublicoServiceClient : NFSeSoapServiceClient, IServiceClient
{
#region Constructors

public AssessorPublicoServiceClient(ProviderAssessorPublico provider, TipoUrl tipoUrl, X509Certificate2 certificado) : base(provider, tipoUrl, certificado)
public AssessorPublicoServiceClient(ProviderAssessorPublico provider, TipoUrl tipoUrl, X509Certificate2 certificado) : base(provider, tipoUrl, SoapVersion.Soap12)
{
//MessageVersion = SoapVersion.Soap11;
}
Expand All @@ -51,6 +52,26 @@ public AssessorPublicoServiceClient(ProviderAssessorPublico provider, TipoUrl ti

#region Methods

private static string GeraHashMD5(string texto)
{
byte[] btyScr = System.Text.ASCIIEncoding.ASCII.GetBytes(texto);

System.Security.Cryptography.MD5CryptoServiceProvider ObjMd5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] BtyRes = ObjMd5.ComputeHash(btyScr);
int Parte1 = BtyRes.Length * 2;
int Parte2 = BtyRes.Length / 8; //esta certo aqui sem (decimal)
int intTotal = Parte1 + Parte2;
StringBuilder strRes = new StringBuilder(intTotal);

for (int intI = 0; intI <= BtyRes.Length - 1; intI++)
{
strRes.Append(BitConverter.ToString(BtyRes, intI, 1));
}
ObjMd5?.Dispose();

return (strRes.ToString().TrimEnd(new char[] { ' ' })).ToLowerInvariant(); ;
}

public string Enviar(string cabec, string msg) => throw new NotImplementedException("Enviar nao implementada/suportada para este provedor.");

public string EnviarSincrono(string cabec, string msg)
Expand All @@ -59,9 +80,9 @@ public string EnviarSincrono(string cabec, string msg)
message.Append("<nfse:Nfse.Execute>");
message.Append("<nfse:Operacao>1</nfse:Operacao>");
message.Append($"<nfse:Usuario>{Provider.Configuracoes.WebServices.Usuario}</nfse:Usuario>");
message.Append($"<nfse:Senha>{Provider.Configuracoes.WebServices.Senha}</nfse:Senha>");
message.Append($"<nfse:Senha>{GeraHashMD5(Provider.Configuracoes.WebServices.Senha)}</nfse:Senha>");
message.Append("<nfse:Webxml>");
message.AppendCData(msg);
message.Append(msg);
message.Append("</nfse:Webxml>");
message.Append("</nfse:Nfse.Execute>");

Expand Down Expand Up @@ -115,24 +136,68 @@ private string Execute(string action, string message, params string[] responseTa
var result = ValidarUsernamePassword();
if (!result) throw new DFe.Core.OpenDFeCommunicationException("Faltou informar username e/ou password");

return Execute(action, message, responseTag);
return Execute(action, message, responseTag, new string[0]);
}

private bool ValidarUsernamePassword()
{
return !string.IsNullOrEmpty(Provider.Configuracoes.WebServices.Usuario) && !string.IsNullOrEmpty(Provider.Configuracoes.WebServices.Senha);
}

//protected override string TratarRetorno(XElement xmlDocument, string[] responseTag)
//{
// var element = xmlDocument?.ElementAnyNs("Fault");
// if (element == null)
// return xmlDocument?.ElementAnyNs("return")?.Value;
protected override string Execute(string soapAction, string message, string soapHeader, string[] responseTag, params string[] soapNamespaces)
{
string contetType;
NameValueCollection headers;
//switch (MessageVersion)
//{
// case SoapVersion.Soap11:
// contetType = $"text/xml; charset={CharSet}";
// headers = new NameValueCollection { { "SOAPAction", soapAction } };
// break;

// case SoapVersion.Soap12:
contetType = $"application/soap+xml; charset={CharSet};action={soapAction}";
headers = null;
// break;

// default:
// throw new ArgumentOutOfRangeException();
//}

var envelope = new StringBuilder();
envelope.Append("<soap:Envelope xmlns:nfse=\"nfse\" xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">");

envelope.Append("<soap:Header/>");
envelope.Append("<soap:Body>");
envelope.Append(message);
envelope.Append("</soap:Body>");
envelope.Append("</soap:Envelope>");
EnvelopeEnvio = envelope.ToString();

Execute(contetType, "POST", headers);

var xmlDocument = XDocument.Parse(EnvelopeRetorno);
var body = xmlDocument.ElementAnyNs("Envelope");
var EnvelopeBody = xmlDocument.ElementAnyNs("Envelope")?.ElementAnyNs("Body")?.ElementAnyNs("Nfse.ExecuteResponse");
if (EnvelopeBody != null)
body = EnvelopeBody;

var retorno = TratarRetorno(body, responseTag);
if (retorno.IsValidXml()) return retorno;

throw new OpenDFeCommunicationException(retorno);
}

protected override string TratarRetorno(XElement xmlDocument, string[] responseTag)
{
var element = xmlDocument?.ElementAnyNs("Fault");
if (element == null)
return xmlDocument?.ElementAnyNs("return")?.Value;

// var exMessage = $"{element.ElementAnyNs("faultcode").GetValue<string>()} - {element.ElementAnyNs("faultstring").GetValue<string>()}";
// throw new OpenDFeCommunicationException(exMessage);
//}
var exMessage = $"{element.ElementAnyNs("faultcode").GetValue<string>()} - {element.ElementAnyNs("faultstring").GetValue<string>()}";
throw new OpenDFeCommunicationException(exMessage);
}

#endregion Methods
}
}
}