Skip to content

Commit

Permalink
Fix query by uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
eclipxe13 committed Sep 30, 2022
1 parent 91fce41 commit 6d49775
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 45 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ Puede que los cambios del ejemplo no sean lógicos, es solo para ilustrar cómo
- Filtrando por únicamente documentos vigentes (excluye cancelados).
- Filtrando por el RFC a cuenta de terceros `XXX01010199A`.
- Filtrando por el RFC contraparte `MAG041126GT8`. Como se solicitan recibidos, entonces son los emidos por ese RFC.
- Filtrando por el UUID `96623061-61fe-49de-b298-c7156476aa8b`
- Filtrando por el UUID `96623061-61fe-49de-b298-c7156476aa8b`.

```php
<?php
Expand Down Expand Up @@ -301,6 +301,23 @@ $query = QueryParameters::create()
;
```

#### Ejemplo de consulta por UUID

En este caso se especifica solamente el UUID a consultar, en el ejemplo es `96623061-61fe-49de-b298-c7156476aa8b`.

Nota: **Todos los demás argumentos de la consulta son ignorados**.

```php
<?php

use PhpCfdi\SatWsDescargaMasiva\Services\Query\QueryParameters;
use PhpCfdi\SatWsDescargaMasiva\Shared\Uuid;

$query = QueryParameters::create()
->withUuid(Uuid::create('96623061-61fe-49de-b298-c7156476aa8b'))
;
```

### Verificar una consulta

La verificación depende de que la consulta haya sido aceptada.
Expand Down
14 changes: 14 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ que nombraremos así: ` Breaking . Feature . Fix `, donde:
**Importante:** Las reglas de SEMVER no aplican si estás usando una rama (por ejemplo `main-dev`)
o estás usando una versión cero (por ejemplo `0.18.4`).

## Versión 0.5.2 2022-09-30

#### Consulta por UUID

Gracias a la solicitud de cambios en [`luisiturrios1/python-cfdiclient#42`](https://github.com/luisiturrios1/python-cfdiclient/pull/42)
por `@alan196`, hemos podido verificar que la documentación del servicio con respecto a la consulta por UUID está incorrecta.

- El campo no se llama `UUID`, se llama `Folio`.
- El campo `RfcSolicitante` no se debe omitir.
- El campo `TipoSolicitud` no se debe omitir.
- Los demás campos no deben existir.

Por lo tanto, se han hecho las correcciones necesarias para hacer la consulta por `UUID`.

## Versión 0.5.1 2022-09-28

### Se corrigen XML mal formados
Expand Down
11 changes: 9 additions & 2 deletions docs/problema-filtros-no-aplicados.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,24 @@ En esta versión se agregan nuevos filtros, permitiendo hacer solicitudes más e

Los siguientes filtros no están funcionando en la consulta de CFDI Regulares:

- Filtro por UUID.
- Filtro de documentos cancelados cuando se solicita un paquete de documentos XML.
El filtro funciona correctamente para paquetes de Metadata.

Los siguientes filtros no están funcionando en la consulta de CFDI de retenciones e información de pagos:

- Filtro por complemento.
- Filtro por UUID.

## Actualizaciones

### 2022-09-30

Hemos notado que la documentación del SAT en relación con la consulta por UUID está incorrecta:

- El campo no se llama `UUID`, se llama `Folio`.
- El campo `RfcSolicitante` se debe especificar.
- El campo `TipoSolicitud` se debe especificar.
- Los demás campos no deben existir.

### 2022-03-04

El SAT actualiza el servicio a la versión 1.2, no resuelve las solicitudes ingresadas.
Expand Down
73 changes: 41 additions & 32 deletions src/RequestBuilder/FielRequestBuilder/FielRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,34 +74,57 @@ public function authorization(DateTime $created, DateTime $expires, string $secu

public function query(QueryParameters $queryParameters): string
{
// normalize input
$start = $queryParameters->getPeriod()->getStart()->format('Y-m-d\TH:i:s');
$end = $queryParameters->getPeriod()->getEnd()->format('Y-m-d\TH:i:s');
$queryByUuid = ! $queryParameters->getUuid()->isEmpty();
$xmlRfcReceived = '';
$requestType = $queryParameters->getRequestType()->getQueryAttributeValue($queryParameters->getServiceType());
$rfcSigner = mb_strtoupper($this->getFiel()->getRfc());
if ($queryParameters->getDownloadType()->isIssued()) {
// issued documents, counterparts are receivers
$rfcIssuer = $rfcSigner;
$rfcReceivers = $queryParameters->getRfcMatches();
} else {
// received documents, counterpart is issuer
$rfcIssuer = $queryParameters->getRfcMatches()->getFirst()->getValue();
$rfcReceivers = RfcMatches::createFromValues($rfcSigner);
}

$solicitudAttributes = array_filter(
[
'RfcSolicitante' => $rfcSigner,
$solicitudAttributes = [
'RfcSolicitante' => $rfcSigner,
'TipoSolicitud' => $requestType,
];

if ($queryByUuid) {
$solicitudAttributes = $solicitudAttributes + [
'Folio' => $queryParameters->getUuid()->getValue(),
];
} else {
$start = $queryParameters->getPeriod()->getStart()->format('Y-m-d\TH:i:s');
$end = $queryParameters->getPeriod()->getEnd()->format('Y-m-d\TH:i:s');
if ($queryParameters->getDownloadType()->isIssued()) {
// issued documents, counterparts are receivers
$rfcIssuer = $rfcSigner;
$rfcReceivers = $queryParameters->getRfcMatches();
} else {
// received documents, counterpart is issuer
$rfcIssuer = $queryParameters->getRfcMatches()->getFirst()->getValue();
$rfcReceivers = RfcMatches::createFromValues($rfcSigner);
}
$solicitudAttributes = $solicitudAttributes + [
'FechaInicial' => $start,
'FechaFinal' => $end,
'TipoSolicitud' => $requestType,
'RfcEmisor' => $rfcIssuer,
'TipoComprobante' => $queryParameters->getDocumentType()->value(),
'EstadoComprobante' => $queryParameters->getDocumentStatus()->value(),
'UUID' => $queryParameters->getUuid()->getValue(),
'RfcACuentaTerceros' => $queryParameters->getRfcOnBehalf()->getValue(),
'Complemento' => $queryParameters->getComplement()->value(),
],
];
if (! $rfcReceivers->isEmpty()) {
$xmlRfcReceived = implode('', array_map(
function (RfcMatch $rfcMatch): string {
return sprintf(
'<des:RfcReceptor>%s</des:RfcReceptor>',
$this->parseXml($rfcMatch->getValue())
);
},
iterator_to_array($rfcReceivers)
));
$xmlRfcReceived = "<des:RfcReceptores>{$xmlRfcReceived}</des:RfcReceptores>";
}
}

$solicitudAttributes = array_filter(
$solicitudAttributes,
static function (string $value): bool {
return '' !== $value;
}
Expand All @@ -116,20 +139,6 @@ function (string $name, string $value): string {
$solicitudAttributes,
));

$xmlRfcReceived = '';
if (! $rfcReceivers->isEmpty()) {
$xmlRfcReceived = implode('', array_map(
function (RfcMatch $rfcMatch): string {
return sprintf(
'<des:RfcReceptor>%s</des:RfcReceptor>',
$this->parseXml($rfcMatch->getValue())
);
},
iterator_to_array($rfcReceivers)
));
$xmlRfcReceived = "<des:RfcReceptores>{$xmlRfcReceived}</des:RfcReceptores>";
}

$toDigestXml = <<<EOT
<des:SolicitaDescarga xmlns:des="http://DescargaMasivaTerceros.sat.gob.mx">
<des:solicitud ${solicitudAttributesAsText}>
Expand Down
17 changes: 16 additions & 1 deletion tests/Integration/ConsumeCfdiServicesUsingFakeFielTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public function testQueryChangeAllParameters(): void
->withDocumentType(DocumentType::nomina())
->withComplement(ComplementoCfdi::nomina12())
->withDocumentStatus(DocumentStatus::active())
->withUuid(Uuid::create('96623061-61fe-49de-b298-c7156476aa8b'))
->withRfcOnBehalf(RfcOnBehalf::create('XXX01010199A'))
->withRfcMatch(RfcMatch::create('AAA010101AAA'))
;
Expand All @@ -49,6 +48,22 @@ public function testQueryChangeAllParameters(): void
);
}

public function testQueryUuid(): void
{
$service = $this->createService();

$parameters = QueryParameters::create()
->withUuid(Uuid::create('96623061-61fe-49de-b298-c7156476aa8b'))
;

$result = $service->query($parameters);
$this->assertSame(
305,
$result->getStatus()->getCode(),
'Expected to receive a 305 - Certificado Inválido from SAT since FIEL is for testing'
);
}

public function testServiceEndpointsDifferentThanQueryEndpointsThrowsError(): void
{
$service = $this->createService();
Expand Down
19 changes: 17 additions & 2 deletions tests/Integration/ConsumeRetencionesServicesUsingFakeFielTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected function getServiceEndpoints(): ServiceEndpoints
return ServiceEndpoints::retenciones();
}

public function testQueryChangeAllParameters(): void
public function testQueryChangeFilters(): void
{
$service = $this->createService();

Expand All @@ -37,7 +37,6 @@ public function testQueryChangeAllParameters(): void
->withRequestType(RequestType::xml())
->withComplement(ComplementoRetenciones::undefined())
->withDocumentStatus(DocumentStatus::active())
->withUuid(Uuid::create('96623061-61fe-49de-b298-c7156476aa8b'))
->withRfcOnBehalf(RfcOnBehalf::create('XXX01010199A'))
->withRfcMatch(RfcMatch::create('AAA010101AAA'))
;
Expand All @@ -50,6 +49,22 @@ public function testQueryChangeAllParameters(): void
);
}

public function testQueryByUuid(): void
{
$service = $this->createService();

$parameters = QueryParameters::create()
->withUuid(Uuid::create('96623061-61fe-49de-b298-c7156476aa8b'))
;

$result = $service->query($parameters);
$this->assertSame(
305,
$result->getStatus()->getCode(),
'Expected to receive a 305 - Certificado Inválido from SAT since FIEL is for testing'
);
}

public function testServiceEndpointsDifferentThanQueryEndpointsThrowsError(): void
{
$service = $this->createService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private function extractSecurityTokenFromXml(string $requestBody): string
return $matches['id'] ?? '';
}

public function testQueryReceived(): void
public function testQueryReceivedByFilters(): void
{
$requestBuilder = $this->createFielRequestBuilderUsingTestingFiles();
$parameters = QueryParameters::create()
Expand All @@ -102,14 +102,32 @@ public function testQueryReceived(): void
->withDocumentType(DocumentType::nomina())
->withComplement(ComplementoCfdi::nomina12())
->withDocumentStatus(DocumentStatus::active())
->withUuid(Uuid::create('96623061-61fe-49de-b298-c7156476aa8b'))
->withRfcOnBehalf(RfcOnBehalf::create('XXX01010199A'))
->withRfcMatch(RfcMatch::create('AAA010101AAA'))
;
$requestBody = $requestBuilder->query($parameters);

$this->assertSame(
$this->xmlFormat(Helpers::nospaces($this->fileContents('query/request-received.xml'))),
$this->xmlFormat(Helpers::nospaces($this->fileContents('query/request-received-by-filters.xml'))),
$this->xmlFormat($requestBody)
);

$xmlSecVerification = (new EnvelopSignatureVerifier())
->verify($requestBody, 'http://DescargaMasivaTerceros.sat.gob.mx', 'SolicitaDescarga');
$this->assertTrue($xmlSecVerification, 'The signature cannot be verified using XMLSecLibs');
}

public function testQueryReceivedByUuid(): void
{
$requestBuilder = $this->createFielRequestBuilderUsingTestingFiles();
$parameters = QueryParameters::create()
->withServiceType(ServiceType::cfdi())
->withUuid(Uuid::create('96623061-61fe-49de-b298-c7156476aa8b'))
;
$requestBody = $requestBuilder->query($parameters);

$this->assertSame(
$this->xmlFormat(Helpers::nospaces($this->fileContents('query/request-received-by-uuid.xml'))),
$this->xmlFormat($requestBody)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
<s:Header/>
<s:Body>
<des:SolicitaDescarga>
<des:solicitud Complemento="nomina12" EstadoComprobante="1" FechaFinal="2019-01-01T00:04:00" FechaInicial="2019-01-01T00:00:00" RfcACuentaTerceros="XXX01010199A" RfcEmisor="AAA010101AAA" RfcSolicitante="EKU9003173C9" TipoComprobante="N" TipoSolicitud="CFDI" UUID="96623061-61fe-49de-b298-c7156476aa8b">
<des:RfcReceptores><des:RfcReceptor>EKU9003173C9</des:RfcReceptor></des:RfcReceptores>
<des:solicitud Complemento="nomina12" EstadoComprobante="1" FechaFinal="2019-01-01T00:04:00" FechaInicial="2019-01-01T00:00:00" RfcACuentaTerceros="XXX01010199A" RfcEmisor="AAA010101AAA" RfcSolicitante="EKU9003173C9" TipoComprobante="N" TipoSolicitud="CFDI">
<des:RfcReceptores>
<des:RfcReceptor>EKU9003173C9</des:RfcReceptor>
</des:RfcReceptores>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
Expand All @@ -14,10 +16,10 @@
<Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>lvCqCw1nWeW7biQuSsjD2KltrQ4=</DigestValue>
<DigestValue>urJ4pg32vV2oozJcBBKRcxoAydo=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>KSTQj+KcLjZEw2iLt2YZn/KB7b96ciHZLci9cHAKl/uGkt7XpQnV1X18ZX9spZZZvlkQyH/gDoEgtMA0yb1oO6ACO66hnfQTaY6b3luhIKrIN9VOdBus6ymrKNhj4mI+ev+HIn1jX1vH8KuVm0lcQ87via8RoOulyvR7lmbuSGILJsshGheofV+eibBdqWe8ukGYYu/iXE9xp+NmI2ltNYizn8fM3wV7F7H51WoJL0lokapcnRk3AeQAbwWw9Yh1dCsb1CqhyRQp6D9fOHpPbQSdhdpdHn774DbH/sqG658Hvx343WrwjDzotLPLVKfGnSTcihDSka6nqXb+EGc4UQ==</SignatureValue>
<SignatureValue>U0+zQI1YH5DGtlULhm01lGwoBd1vdbIv/myz/gPshujerNvRzzw9mhGCCq8E7BMsLKefbl4TTOOkL5wvFqczzcKrHS8lFTkw7073CIPVrUyhbajCaqDGRtTvHx84xpi83vknZJhuOu+Pl6gyNIu/g4/MSFtSZ9CKqIu62VBAuepGggY2jsceyMIqNhtLOuR9yGSPR2kTABypkIjvyK9NqM/JivCmHV3FXv6280eHcf40gAtHpTw++ERSRuhW/rkimyJrHLIeMvZpfT9JpORsUCbUHmdEOQae0zXHp0mcGV0woWiRKSBcA2yogl+rrqLc1cbsX5I6jfYTdaegcoe0eQ==</SignatureValue>
<KeyInfo>
<X509Data>
<X509IssuerSerial>
Expand Down
33 changes: 33 additions & 0 deletions tests/_files/query/request-received-by-uuid.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:des="http://DescargaMasivaTerceros.sat.gob.mx" xmlns:xd="http://www.w3.org/2000/09/xmldsig#">
<s:Header/>
<s:Body>
<des:SolicitaDescarga>
<des:solicitud Folio="96623061-61fe-49de-b298-c7156476aa8b" RfcSolicitante="EKU9003173C9" TipoSolicitud="Metadata">
<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="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>A0teWlShxOD/65O111KTBA+TOSk=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>d0KaOIjhmXRKuSmbqP/tFoyEqBiNwab5rd8L9mCnjg0TxtbRgJz9+5I7LP0JwJiVWlzcPFoOwth68vSmFqr+hEtvpYlYPmUrPMMiheIv+6D28DLErBlZSRmnXuu3EJuI7D29JAMB2MYML8N8n8Y/NOLGhO3qQIJDAa8AjQCIxgokipU9iZsHtDcHcHZOocCFMWNOBTDLdd8TOsi6HNgWEDeUL18nJ7W1QOamQAU3fmKSPF3I37pISu/MHExPayqDZEaDkbKl0u9j8GGSlFyOreLjvkyfZWj9azlu/jyMIy/DwlEeIviPrccc/nnDQ1T3wYrB2QNeSeP7Xb8QCOx+ug==</SignatureValue>
<KeyInfo>
<X509Data>
<X509IssuerSerial>
<X509IssuerName>CN=AC UAT,O=SERVICIO DE ADMINISTRACION TRIBUTARIA,OU=SAT-IES Authority,[email protected],street=3ra cerrada de cadiz,postalCode=06370,C=MX,ST=CIUDAD DE MEXICO,L=COYOACAN,x500UniqueIdentifier=2.5.4.45,unstructuredName=responsable: ACDMA-SAT</X509IssuerName>
<X509SerialNumber>292233162870206001759766198444326234574038511927</X509SerialNumber>
</X509IssuerSerial>
<X509Certificate>MIIGBDCCA+ygAwIBAgIUMzAwMDEwMDAwMDA0MDAwMDI0MTcwDQYJKoZIhvcNAQELBQAwggErMQ8wDQYDVQQDDAZBQyBVQVQxLjAsBgNVBAoMJVNFUlZJQ0lPIERFIEFETUlOSVNUUkFDSU9OIFRSSUJVVEFSSUExGjAYBgNVBAsMEVNBVC1JRVMgQXV0aG9yaXR5MSgwJgYJKoZIhvcNAQkBF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9gwEQYJYIZIAYb4QgEBBAQDAgWgMB0GA1UdJQQWMBQGCCsGAQUFBwMEBggrBgEFBQcDAjANBgkqhkiG9w0BAQsFAAOCAgEAr9uInaTMf6UqST5xpEonsbOeqdnyQsG1ZiYLKw7lnjMjkYkrenManFXkpxHUeWw8Y/4y48iNcmrs1AH+Gd7ZdOJ3XIqIEy0C/SM4GemRx+YMjfsif24dxTN1fD8cU86W1Y56e3rDfgsR9yT/sGmxqvkUN3sQElyD2+qhUZUydK7i03bWIG5fyzGIi15YBhzE6ALuX8po2coUlwQV830zRBPGDkcomejsfKPjYKQ+yzUtwO+8Klr1PUHmdlaG7Gv4llWLNvKm21qAgxjMkiKHLp1Cr66W1ahks8I8VqsLarSKDzGf42VstQpO0hLV1cWXk920nl+n4htYgE7KDQwpioZXFeXCd9KiZcEREn/gvHi6nq6awPJS7k6hBPFEAtbmzwykQ30MNdtHwUXRKAf1yru9VYGIs38ElZyU8C6JJ0MPx/f/N56yHYOYKtYD++STWgXjHD0c/RPV5j1nhjPFhRMHZAuxOwQxky28MQak+pd7OF5cDJiGYfDQZ7G+riGkhodIGS+jmexWQn0tpoZt8U+Ay7L8P7fdqcV9P58AMz4Eie3VrPs2LfpbhNpD/26AvgbkE4Iz4HAOdW9AH1im3Ae8+nWuICnbWcmExqcRqykM1U7MXkOV23L3jtdvDKcP+uCudwL+9Iit6K5pCGvqw7e/uCK0/OyhtyxgA0LDS2A=</X509Certificate>
</X509Data>
</KeyInfo>
</Signature>
</des:solicitud>
</des:SolicitaDescarga>
</s:Body>
</s:Envelope>

0 comments on commit 6d49775

Please sign in to comment.