Skip to content
Merged
Show file tree
Hide file tree
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
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,37 @@ $file = $authorizedClient->efactura()->download([
$file->getContent(); // string - You can save/download the content to a file
```
#### [Validate](https://mfinante.gov.ro/static/10/eFactura/validare.html) Resource
TODO: implement `validate` from [here](https://mfinante.gov.ro/static/10/eFactura/validare.html)

Validate the XML

```php
/*
* $xmlStandard can be one of the following: 'FACT1', 'FCN'.
* The default value is 'FACT1'
*/
$response = $client->efactura()->validateXml($path_to_xml, $xmlStandard);

$response->isValid(); //bool, you can proceed accordingly
$response->status; //string 'ok' or 'nok'
$response->messages; //array of messages, empty if validation successful

$response->toArray();
//Examples
$valid = [
'stare' => 'ok',
'trace_id' => '....',
];

$invalid = [
'stare' => 'nok',
'Messages' => [
[
'message' => "Fisierul transmis nu este valid.....",
]
],
'trace_id' => '....',
];
```

#### [XmlToPdf](https://mfinante.gov.ro/static/10/eFactura/xmltopdf.html) Resource
Convert XML eFactura to PDF. For this endpoint you need to use unauthenticated client
Expand Down
27 changes: 27 additions & 0 deletions src/Resources/Efactura.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

use Anaf\Contracts\FileContract;
use Anaf\Enums\Efactura\UploadStandard;
use Anaf\Exceptions\TransporterException;
use Anaf\Exceptions\UnserializableResponse;
use Anaf\Responses\Efactura\CreateMessagesResponse;
use Anaf\Responses\Efactura\CreatePaginatedMessagesResponse;
use Anaf\Responses\Efactura\CreateUploadResponse;
use Anaf\Responses\Efactura\ValidationResponse;
use Anaf\ValueObjects\Transporter\Payload;
use Anaf\ValueObjects\Transporter\Xml;
use Exception;
Expand Down Expand Up @@ -131,4 +134,28 @@ public function xmlToPdf(string $xml_path, string $standard = 'FACT1', bool $val

return $this->transporter->requestFile($payload);
}

/**
* Validates a given XML file
*
* @throws UnserializableResponse|TransporterException
*/
public function validateXml(string $xml_path, string $standard = 'FACT1'): ValidationResponse
{
if (!in_array($standard, ['FACT1', 'FCN'])) {
throw new RuntimeException("Invalid standard {$standard}");
}

$payload = Payload::upload(
resource: "prod/FCTEL/rest/validare/{$standard}",
body: Xml::from($xml_path)->toString(),
);

/**
* @var array{stare: string, trace_id: string, Messages: list<array{message: string}>} $response
*/
$response = $this->transporter->requestObject($payload);

return ValidationResponse::from($response);
}
}
66 changes: 66 additions & 0 deletions src/Responses/Efactura/ValidationResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Anaf\Responses\Efactura;

use Anaf\Contracts\Response;
use Anaf\Responses\Concerns\ArrayAccessible;

/**
* @implements Response<array{stare: string, trace_id: string, messages: list<array{message: string}>}>
*/
class ValidationResponse implements Response
{
/**
* @use ArrayAccessible<array{stare: string, trace_id: string, messages: list<array{message: string}>}>
*/
use ArrayAccessible;

/**
* Creates a new ValidationResponse instance.
*
* @param string $status
* @param string $traceId
* @param list<array{message: string}> $messages
*/
public function __construct(
public readonly string $status,
public readonly string $traceId,
public readonly array $messages,
){}

/**
* Acts as static factory, and returns a new Response instance.
*
* @param array{stare: string, trace_id: string, Messages: null|list<array{message: string}>} $attributes
*/
public static function from(array $attributes): self
{
return new self(
$attributes['stare'],
$attributes['trace_id'],
$attributes['Messages'] ?? [],
);
}

/**
* {@inheritDoc}
*/
public function toArray(): array
{
return [
'stare' => $this->status,
'trace_id' => $this->traceId,
'messages' => $this->messages,
];
}

/**
* Returns a bool to quick check if the xml was valid or not
*
* @return bool
*/
public function isValid(): bool
{
return $this->status === 'ok';
}
}
19 changes: 19 additions & 0 deletions tests/Fixtures/Efactura.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,22 @@ function getFakeFile(string $content = 'dummy zile file content')
{
return new FileHandler($content);
}

function getXmlValidationMessage($valid = true): array
{
if ($valid)
return [
'stare' => 'ok',
'trace_id' => '.....',
];

return [
'stare' => 'nok',
'Messages' => [
[
'message' => "Fisierul transmis nu este valid. org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 27; cvc-complex-type.2.4.a:",
]
],
'trace_id' => '.....',
];
}
28 changes: 28 additions & 0 deletions tests/Resources/Efactura.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Anaf\Responses\Efactura\CreatePaginatedMessagesResponse;
use Anaf\Responses\Efactura\CreateUploadResponse;
use Anaf\Responses\Efactura\Message;
use Anaf\Responses\Efactura\ValidationResponse;

test('upload', function () {
$authorizedClient = mockAuthorizedClient('POST', '/prod/FCTEL/rest/upload', getUploadMessage());
Expand Down Expand Up @@ -90,3 +91,30 @@

expect($response)->toBeInstanceOf(FileContract::class);
});

test('validate valid xml', function () {
$authorizedClient = mockClient('POST', '/prod/FCTEL/rest/validare/FACT1', getXmlValidationMessage());

$response = $authorizedClient->efactura()->validateXml(__DIR__ . '/../Fixtures/dummyxml.xml');

expect($response)->toBeInstanceOf(ValidationResponse::class)
->toArray()
->toHaveKey('stare', 'ok')
->and($response)
->isValid()
->toBeTrue();
});


test('validate invalid xml', function () {
$authorizedClient = mockClient('POST', '/prod/FCTEL/rest/validare/FACT1', getXmlValidationMessage(false));

$response = $authorizedClient->efactura()->validateXml(__DIR__ . '/../Fixtures/dummyxml.xml');

expect($response)->toBeInstanceOf(ValidationResponse::class)
->toArray()
->toHaveKey('stare', 'nok')
->and($response)
->isValid()
->toBeFalse();
});
Loading