Re: Duda de Como Autentificarse en los WebService de Descarga de los XML
Publicado: Mar Feb 05, 2019 11:59 am
Es correcto, tienes que verificar y verificar y verificar......checa el foro al que te hago referencia
FACTURA ELECTRONICA EN MEXICO
http://validacfd.com/phpbb3/
Código: Seleccionar todo
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<VerificaSolicitudDescargaResponse xmlns="http://DescargaMasivaTerceros.sat.gob.mx">
<VerificaSolicitudDescargaResult CodEstatus="5000" EstadoSolicitud="5" CodigoEstadoSolicitud="5004" NumeroCFDIs="0" Mensaje="Solicitud Aceptada" />
</VerificaSolicitudDescargaResponse>
</s:Body>
</s:Envelope>
Saul_RS escribió:mo.rodrigo escribió:Buenas Tardes.
Estoy realizando el consumo del "WebService" en php, pero no me deja me sale error "An error occurred when verifying security for the message.", intente copiar todo al pie de la letra., inclusive obtener el valor canonizado que ponen de ejemplo con las fechas que ponen de ejemplo ( se hace de la misma manera que para cancelar un cfdi)
Excelente aporte, hice solo unas pocas modificaciones pero la parte de consumir el servicio no me devuelve ningún valor. Si alguien pudiera apoyarme en el uso de CURL se los agradecería, aquí mi código
Código: Seleccionar todo
<?php
/* Obtener fecha con el formato requerido y sumar 5 Horas para coincidir con el horario GMT */
$hora_actual = new DateTime("America/Mexico_City");
$hora_actual->add(new DateInterval('PT6H'));
$actual = $hora_actual->format('Y-m-d\TH:i:s');
/* De la fecha obtenida sumar 5 minutos */
$hora_actual2 = new DateTime($actual);
$hora_actual2->add(new DateInterval('PT5M'));
$expira = $hora_actual2->format('Y-m-d\TH:i:s');
/* Se agregan los milisegundos y la Z para cumplir con el formato */
$actual.= '.000Z';
$expira.= '.000Z';
/* Se arma el nodo Timestamp sustituyendo los valores */
$time = '<u:Timestamp xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" u:Id="_0"><u:Created>' . $actual . '</u:Created><u:Expires>' . $expira . '</u:Expires></u:Timestamp>';
/* Aplicar el HASH y codificar en Base64 el nodo Timestamp */
$dom = new DOMDocument();
$dom->loadXML($time);
$canonicalized = $dom->C14N();
$digest = base64_encode(pack("H*", sha1($canonicalized)));
/* Firma con la Llave privada y codificar en Base64 el Digest del nodo Timestamp */
$path_key = 'D:/key_pruebas.pem';
$cadenafirmada = "";
$fp = fopen($path_key, "r");
$priv_key = fread($fp, filesize($path_key));
fclose($fp);
$pkeyid = openssl_get_privatekey($priv_key);
openssl_sign($digest, $cadenafirmada, $pkeyid, OPENSSL_ALGO_SHA1);
$sello = base64_encode($cadenafirmada);
openssl_free_key($pkeyid);
/* Leer Certificado */
$path_cert = 'D:/cert_pruebas.pem';
$fp = fopen($path_cert, "r");
$public_key = fread($fp, filesize($path_cert));
fclose($fp);
$certificado = substr($public_key, 27, -27);
/* Genrerar UUID */
$uuid = "uuid-" . genUuid() . "-1";
/* Armar el REQUEST */
$autenticacion = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><s:Header><o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><u:Timestamp u:Id="_0"><u:Created>' . $actual . '</u:Created><u:Expires>' . $expira . '</u:Expires></u:Timestamp><o:BinarySecurityToken u:Id="' . $uuid . '" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">' . $certificado . '</o:BinarySecurityToken><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>' . $sello . '</SignatureValue><KeyInfo><o:SecurityTokenReference><o:Reference ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" URI="#' . $uuid . '"/></o:SecurityTokenReference></KeyInfo></Signature></o:Security></s:Header><s:Body><Autentica xmlns="http://DescargaMasivaTerceros.gob.mx"/></s:Body></s:Envelope>';
/* Agregar Headers */
$header = array("Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"SOAPAction: http://DescargaMasivaTerceros.gob.mx/IAutenticacion/Autentica",
"Content-length: " . strlen($autenticacion),
);
/* Inicializar curl */
$soap = curl_init();
curl_setopt($soap, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($soap, CURLOPT_URL, 'https://cfdidescargamasivasolicitud.clouda.sat.gob.mx/Autenticacion/Autenticacion.svc');
curl_setopt($soap, CURLOPT_RETURNTRANSFER, true);
curl_setopt($soap, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($soap, CURLOPT_TIMEOUT, 10);
curl_setopt($soap, CURLOPT_POST, true);
curl_setopt($soap, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($soap, CURLOPT_TIMEOUT_MS, 50000);
curl_setopt($soap, CURLOPT_POSTFIELDS, $autenticacion);
curl_setopt($soap, CURLOPT_HTTPHEADER, $header);
$respuesta = curl_exec($soap);
var_dump($respuesta);
curl_close($soap);
die();
function genUuid() {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
}
estebanchambas escribió:Saul_RS Ya no entendi nada.
Ese error ya te daba antes en tu anterior publicación y en esta pusiste exactamente lo mismo.
¿Cual es la diferencia?
Código: Seleccionar todo
/* Agregar Headers */
$header = array('Content-type: text/xml;charset="utf-8"', 'SOAPAction: "http://DescargaMasivaTerceros.gob.mx/IAutenticacion/Autentica"');
/* Inicializar curl */
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, 'https://cfdidescargamasivasolicitud.clouda.sat.gob.mx/Autenticacion/Autenticacion.svc');
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true);
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $autenticacion);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header);
curl_setopt($soap_do, CURLOPT_FRESH_CONNECT, true);
$respuesta = curl_exec($soap_do);
curl_close($soap_do);
var_dump($respuesta);
die();
Código: Seleccionar todo
/* Agregar Headers */
$header = array("Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"SOAPAction: http://DescargaMasivaTerceros.gob.mx/IAutenticacion/Autentica",
"Content-length: " . strlen($autenticacion),
);
/* Inicializar curl */
$soap = curl_init();
curl_setopt($soap, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($soap, CURLOPT_URL, 'https://cfdidescargamasivasolicitud.clouda.sat.gob.mx/Autenticacion/Autenticacion.svc');
curl_setopt($soap, CURLOPT_RETURNTRANSFER, true);
curl_setopt($soap, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($soap, CURLOPT_TIMEOUT, 10);
curl_setopt($soap, CURLOPT_POST, true);
curl_setopt($soap, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($soap, CURLOPT_TIMEOUT_MS, 50000);
curl_setopt($soap, CURLOPT_POSTFIELDS, $autenticacion);
curl_setopt($soap, CURLOPT_HTTPHEADER, $header);
$respuesta = curl_exec($soap);
var_dump($respuesta);
curl_close($soap);
die();
Axterix escribió:¿Ya que tengo descargado el paquete en base64 solo lo tengo que descifrar y luego volverlo un zip? ¿Alguien lo ha hecho en Java?Saludos .
Código: Seleccionar todo
<SignedInfo xmlns="http://www.w3.org/2000/09/xmldsig#">><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>s9gmL0php/Xy5RYXkJd03oHaVok=</DigestValue></Reference></SignedInfo
Código: Seleccionar todo
xmlns="http://www.w3.org/2000/09/xmldsig#"