using System;
using System.Security.Cryptography.X509Certificates;
using System.Xml;
namespace LibraryDescargaXmlSatWS
{
public class Solicitud : SoapRequestBase
{
public Solicitud(string url, string action)
: base(url, action)
{
}
#region Crea el XML para enviar
public string Generate(X509Certificate2 certificate, string rfcEmisor, string rfcReceptor, string rfcSolicitante, string fechaInicial = "", string fechaFinal = "", string tipoSolicitud = "CFDI")
{
// 2025-06-02
String Sentido = "Emitidos";
if (rfcReceptor.Length > 10)
Sentido = "Recibidos";
//String EstadoComprobante = "0";
FixFecha(fechaInicial, fechaFinal, out fechaInicial, out fechaFinal);
#region Codigo en desuso version 1.2
// string canonicalTimestamp = "<SolicitaDescarga xmlns:des=\"http://DescargaMasivaTerceros.sat.gob.mx\">"
// + "<solicitud RfcEmisor=\"" + rfcEmisor
// + "\" RfcSolicitante=\"" + rfcSolicitante
// + "\" FechaInicial=\"" + fechaInicial
// + "\" FechaFinal=\"" + fechaFinal
// + "\" TipoSolicitud=\"" + tipoSolicitud
//// + "\" EstadoComprobanteSpecified=\"" + EstadoComprobante
// + "\">"
// + (string.IsNullOrEmpty(rfcReceptor) ? "" : "<RfcReceptores><RfcReceptor>" + rfcReceptor + @"</RfcReceptor></RfcReceptores>") + "</solicitud>"
// + "</SolicitaDescarga>";
// string digest = CreateDigest(canonicalTimestamp);
// string canonicalSignedInfo = @"<SignedInfo xmlns=""http://www.w3.org/2000/09/xmldsig#"">" +
// @"<CanonicalizationMethod Algorithm=""http://www.w3.org/2001/10/xml-exc-c14n#""></CanonicalizationMethod>" +
// @"<SignatureMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#rsa-sha1""></SignatureMethod>" +
// @"<Reference URI=""#_0"">" +
// "<Transforms>" +
// @"<Transform Algorithm=""http://www.w3.org/2001/10/xml-exc-c14n#""></Transform>" +
// "</Transforms>" +
// @"<DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1""></DigestMethod>" +
// "<DigestValue>" + digest + "</DigestValue>" +
// "</Reference>" +
// "</SignedInfo>";
// string signature = Sign(canonicalSignedInfo, certificate);
// string soap_request = @"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/""
// xmlns:xd=""http://www.w3.org/2000/09/xmldsig#"">" +
// @"<s:Header/>" +
// @"<s:Body>" +
// @"<SolicitaDescarga xmlns=""http://DescargaMasivaTerceros.sat.gob.mx"" >" +
// @"<solicitud RfcEmisor=""" + rfcEmisor +
// @""" RfcSolicitante=""" + rfcSolicitante +
// @""" FechaInicial=""" + fechaInicial +
// @""" FechaFinal =""" + fechaFinal +
// @""" TipoSolicitud=""" + tipoSolicitud +
// // @""" EstadoComprobanteSpecified=""" + EstadoComprobante +
// @""" > " + (string.IsNullOrEmpty(rfcReceptor) ? "" : "<RfcReceptores><RfcReceptor>" + rfcReceptor + @"</RfcReceptor></RfcReceptores>") +
// @"<Signature xmlns=""http://www.w3.org/2000/09/xmldsig#"">" +
// @"<SignedInfo>" +
// @"<CanonicalizationMethod Algorithm=""http://www.w3.org/2001/10/xml-exc-c14n#""/>" +
// @"<SignatureMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#rsa-sha1""/>" +
// @"<Reference URI=""#_0"">" +
// @"<Transforms>" +
// @"<Transform Algorithm=""http://www.w3.org/2001/10/xml-exc-c14n#""/>" +
// @"</Transforms>" +
// @"<DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1""/>" +
// @"<DigestValue>" + digest + @"</DigestValue>" +
// @"</Reference>" +
// @"</SignedInfo>" +
// @"<SignatureValue>" + signature + "</SignatureValue>" +
// @"<KeyInfo>" +
// @"<X509Data>" +
// @"<X509IssuerSerial>" +
// @"<X509IssuerName>" + certificate.Issuer +
// @"</X509IssuerName>" +
// @"<X509SerialNumber>" + certificate.SerialNumber +
// @"</X509SerialNumber>" +
// @"</X509IssuerSerial>" +
// @"<X509Certificate>" + Convert.ToBase64String(certificate.RawData) + "</X509Certificate>" +
// @"</X509Data>" +
// @"</KeyInfo>" +
// @"</Signature>" +
// @"</solicitud>" +
// @"</SolicitaDescarga>" +
// @"</s:Body>" +
// @"</s:Envelope>";
//
// String signature = Sign(canonicalSignedInfo, certificate);
#endregion
// Version 1.5 2025-06-02
String EstadoComprobante = "Vigente";
String FechaInicial = fechaInicial; // "2025-05-01T21:27:14";
String FechaFinal = fechaFinal; // "2025-05-02T21:27:14";
String TipoSolicitud = tipoSolicitud; // "CFDI";
String RfcReceptor = rfcReceptor; //"";
String RfcEmisor = rfcEmisor; //"";
String SerialNumber = certificate.SerialNumber;
String Certificate = Convert.ToBase64String(certificate.RawData);
string canonicalTimestamp = "";
if (Sentido == "Emitidos")
{
canonicalTimestamp =
"<des:SolicitaDescargaEmitidos xmlns:des=\"http://DescargaMasivaTerceros.sat.gob.mx\">" +
"<des:solicitud" +
" EstadoComprobante=\"" + EstadoComprobante + "\"" +
" FechaInicial=\"" + fechaInicial + "\"" +
" FechaFinal=\"" + fechaFinal + "\"" +
" TipoSolicitud=\"" + tipoSolicitud + "\"" +
" RfcEmisor=\"" + rfcEmisor + "\">" +
"</des:solicitud>" +
"</des:SolicitaDescargaEmitidos>";
}
else
{
// Recibidos
canonicalTimestamp =
"<des:SolicitaDescargaRecibidos xmlns:des=\"http://DescargaMasivaTerceros.sat.gob.mx\">" +
"<des:solicitud" +
" EstadoComprobante=\"" + EstadoComprobante + "\"" +
" FechaInicial=\"" + fechaInicial + "\"" +
" FechaFinal=\"" + fechaFinal + "\"" +
" TipoSolicitud=\"" + tipoSolicitud + "\"" +
" RfcReceptor=\"" + rfcReceptor + "\">" +
"</des:solicitud>" +
"</des:SolicitaDescargaRecibidos>";
}
string digest = CreateDigest(canonicalTimestamp);
string canonicalSignedInfo = @"<SignedInfo xmlns=""http://www.w3.org/2000/09/xmldsig#"">" +
@"<CanonicalizationMethod Algorithm=""http://www.w3.org/TR/2001/REC-xml-c14n-20010315"" />" +
@"<SignatureMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#rsa-sha1"" />" +
@"<Reference URI="""">" +
"<Transforms>" +
@"<Transform Algorithm=""http://www.w3.org/2000/09/xmldsig#enveloped-signature"" />" +
"</Transforms>" +
@"<DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1"" />" +
"<DigestValue>" + digest + "</DigestValue>" +
"</Reference>" +
"</SignedInfo>";
string signature = Sign(canonicalSignedInfo, certificate);
string soap_request = "";
if (Sentido == "Emitidos")
{
// EMITIDOS
soap_request = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:des=""http://DescargaMasivaTerceros.sat.gob.mx"" xmlns:xd=""http://www.w3.org/2000/09/xmldsig#"">
<soapenv:Header/>
<soapenv:Body>
<des:SolicitaDescargaEmitidos>
<des:solicitud EstadoComprobante=""#EstadoComprobante"" FechaInicial=""#FechaInicial"" FechaFinal=""#FechaFinal"" TipoSolicitud=""#TipoSolicitud"" RfcEmisor=""#RfcEmisor"">
<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 URI="""">
<Transforms>
<Transform Algorithm=""http://www.w3.org/2000/09/xmldsig#enveloped-signature""/>
</Transforms>
<DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1""/>
<DigestValue>#DigestValue</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>#SignatureValue</SignatureValue>
<KeyInfo>
<X509Data>
<X509IssuerSerial>
<X509IssuerName>OID.1.2.840.113549.1.9.2=responsable: ACDMA-SAT, OID.2.5.4.45=2.5.4.45, L=COYOACAN, S=CIUDAD DE MEXICO, C=MX, PostalCode=06370, STREET=3ra cerrada de caliz, E=oscar.martinez@sat.gob.mx, OU=SAT-IES Authority, O=SERVICIO DE ADMINISTRACION TRIBUTARIA, CN=AC UAT</X509IssuerName>
<X509SerialNumber>#SerialNumber</X509SerialNumber>
</X509IssuerSerial>
<X509Certificate>#Certificate</X509Certificate>
</X509Data>
</KeyInfo>
</Signature>
</des:solicitud>
</des:SolicitaDescargaEmitidos>
</soapenv:Body>
</soapenv:Envelope>";
}
else
{
// RECIBIDOS
soap_request = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:des=""http://DescargaMasivaTerceros.sat.gob.mx"" xmlns:xd=""http://www.w3.org/2000/09/xmldsig#"">
<soapenv:Header/>
<soapenv:Body>
<des:SolicitaDescargaRecibidos>
<des:solicitud EstadoComprobante=""#EstadoComprobante"" FechaInicial=""#FechaInicial"" FechaFinal=""#FechaFinal"" TipoSolicitud=""#TipoSolicitud"" RfcReceptor=""#RfcReceptor"">
<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 URI="""">
<Transforms>
<Transform Algorithm=""http://www.w3.org/2000/09/xmldsig#enveloped-signature""/>
</Transforms>
<DigestMethod Algorithm=""http://www.w3.org/2000/09/xmldsig#sha1""/>
<DigestValue>#DigestValue</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>#SignatureValue</SignatureValue>
<KeyInfo>
<X509Data>
<X509IssuerSerial>
<X509IssuerName>OID.1.2.840.113549.1.9.2=responsable: ACDMA-SAT, OID.2.5.4.45=2.5.4.45, L=COYOACAN, S=CIUDAD DE MEXICO, C=MX, PostalCode=06370, STREET=3ra cerrada de caliz, E=oscar.martinez@sat.gob.mx, OU=SAT-IES Authority, O=SERVICIO DE ADMINISTRACION TRIBUTARIA, CN=AC UAT</X509IssuerName>
<X509SerialNumber>#SerialNumber</X509SerialNumber>
</X509IssuerSerial>
<X509Certificate>#Certificate</X509Certificate>
</X509Data>
</KeyInfo>
</Signature>
</des:solicitud>
</des:SolicitaDescargaRecibidos>
</soapenv:Body>
</soapenv:Envelope>";
}
soap_request = soap_request.Replace("#EstadoComprobante", EstadoComprobante);
soap_request = soap_request.Replace("#FechaInicial", FechaInicial);
soap_request = soap_request.Replace("#FechaFinal", FechaFinal);
soap_request = soap_request.Replace("#TipoSolicitud", TipoSolicitud);
soap_request = soap_request.Replace("#RfcReceptor", RfcReceptor);
soap_request = soap_request.Replace("#RfcEmisor", RfcEmisor);
soap_request = soap_request.Replace("#DigestValue", digest);
soap_request = soap_request.Replace("#SignatureValue", signature);
soap_request = soap_request.Replace("#SerialNumber", SerialNumber);
soap_request = soap_request.Replace("#Certificate", Certificate);
xml = soap_request;
return soap_request;
}
#endregion
private void FixFecha(string fechaInicial1, string fechaFinal1, out string fechaInicial, out string fechaFinal)
{
fechaInicial = fechaInicial1+ "T00:00:00";
fechaFinal = fechaFinal1 + "T23:59:59";
}
public override string GetResult(XmlDocument xmlDoc)
{
// 2025-06-02
//string CodEstatus = xmlDoc.GetElementsByTagName("SolicitaDescargaResult")[0].Attributes["CodEstatus"].Value;
//string Mensaje = xmlDoc.GetElementsByTagName("SolicitaDescargaResult")[0].Attributes["Mensaje"].Value;
//string IdSolicituds = xmlDoc.GetElementsByTagName("SolicitaDescargaResult")[0].Attributes["IdSolicitud"].Value;
string CodEstatus = "";
string Mensaje = "";
string IdSolicituds = "";
try
{
CodEstatus = xmlDoc.GetElementsByTagName("SolicitaDescargaRecibidosResult")[0].Attributes["CodEstatus"].Value;
Mensaje = xmlDoc.GetElementsByTagName("SolicitaDescargaRecibidosResult")[0].Attributes["Mensaje"].Value;
IdSolicituds = xmlDoc.GetElementsByTagName("SolicitaDescargaRecibidosResult")[0].Attributes["IdSolicitud"].Value;
}
catch
{
CodEstatus = xmlDoc.GetElementsByTagName("SolicitaDescargaEmitidosResult")[0].Attributes["CodEstatus"].Value;
Mensaje = xmlDoc.GetElementsByTagName("SolicitaDescargaEmitidosResult")[0].Attributes["Mensaje"].Value;
IdSolicituds = xmlDoc.GetElementsByTagName("SolicitaDescargaEmitidosResult")[0].Attributes["IdSolicitud"].Value;
}
return CodEstatus + "|" + Mensaje + "|" + IdSolicituds;
}
}
}