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

Improve structure and tests #2

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions .auth.json.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"acube": {
"email": "",
"password": "",
"environment": "",
"simplified": ""
},
"aruba": {
"username": "",
"password": ""
},
"effatta": {
"email": "",
"password": "",
"source": "",
"filename": "",
"idMittente": "",
"dataUserId": ""
}
}
14 changes: 0 additions & 14 deletions .github/ISSUE_TEMPLATE/config.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ phpunit.xml
psalm.xml
vendor
.php-cs-fixer.cache

.auth.json
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ Per un più veloce sviluppo, si consiglia di estendere la classe `FatturaElettro

## Testing

Per eseguire alcuni test, è necessario avere le credenziali dei vari adapter. Di default la suite di test marca come `skipped` i test per cui sono necessarie tali credenziali, e non sono state fornite.

Per fornire tali credenziali, copiare il file `.auth.json.dist` in `.auth.json` e popolare le chiavi necessarie.

Per lanciare la suite di test:

```bash
composer test
```
Expand Down
5 changes: 5 additions & 0 deletions src/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public function withConfig(Config $config): static
return $this;
}

public function getConfig(): Config
{
return $this->config;
}

protected function createRequest(string $method, string $uri): RequestInterface
{
return $this->requestFactory()->createRequest($method, $uri);
Expand Down
46 changes: 38 additions & 8 deletions src/Adapter/Acube/AcubeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,33 @@

use FatturaElettronicaPhp\Sender\Adapter\AbstractAdapter;
use FatturaElettronicaPhp\Sender\Adapter\HasEnvironments;
use FatturaElettronicaPhp\Sender\Config;
use FatturaElettronicaPhp\Sender\Contracts\ProvidesConfigurationKeys;
use FatturaElettronicaPhp\Sender\Contracts\SenderAdapterInterface;
use FatturaElettronicaPhp\Sender\Contracts\SupportsDifferentEnvironmentsInterface;
use FatturaElettronicaPhp\Sender\Exceptions\CannotSendDigitalDocumentException;
use FatturaElettronicaPhp\Sender\Exceptions\InvalidCredentialsException;
use FatturaElettronicaPhp\Sender\Result;

class AcubeAdapter extends AbstractAdapter implements SenderAdapterInterface, SupportsDifferentEnvironmentsInterface
class AcubeAdapter extends AbstractAdapter implements SenderAdapterInterface, SupportsDifferentEnvironmentsInterface, ProvidesConfigurationKeys
{
use HasEnvironments;

public const ENV_SANDBOX = 'sandbox';
public const ENV_PRODUCTION = 'production';

private const SANDBOX_AUTH_URL = 'https://api-sandbox.acubeapi.com/login_check';
private const SANDBOX_AUTH_URL = 'https://common-sandbox.api.acubeapi.com/login';
private const SANDBOX_INVOICE_URL = 'https://api-sandbox.acubeapi.com/invoices';
private const INVOICE_URL = 'https://api.acubeapi.com/invoices';
private const AUTH_URL = 'https://api.acubeapi.com/login_check';
private const AUTH_URL = 'https://common.api.acubeapi.com/login';
private const SIMPLIFIED_INVOICE_ADDITIONAL_ENDPOINT = 'simplified';

public function send(string $xml): void
public function send(string $xml, ?Config $config = null): Result
{
if ($config) {
$this->config = $this->config->extend($config->toArray());
}

$request = $this->createRequest('POST', $this->invoiceUrl())
->withHeader('Content-Type', 'application/xml')
->withHeader('Authorization', 'Bearer ' . $this->login())
Expand All @@ -33,14 +41,16 @@ public function send(string $xml): void
$response = $this->sendRequest($request);
$result = json_decode($response, true);

if ($result !== true) {
if (! $result || ! isset($result['uuid'])) {
throw new CannotSendDigitalDocumentException($response);
}

return new Result($result);
}

private function login(): string
{
if ($this->config->get('email') === null || $this->config->get('password')) {
if ($this->config->get('email') === null || $this->config->get('password') === null) {
throw new InvalidCredentialsException("`email` and `password` configuration keys are required");
}

Expand Down Expand Up @@ -72,11 +82,21 @@ private function authUrl(): string

private function invoiceUrl(): string
{
$invoice_url = self::SANDBOX_INVOICE_URL;
if ($this->environment() === self::ENV_PRODUCTION) {
return self::INVOICE_URL;
$invoice_url = self::INVOICE_URL;
}

return self::SANDBOX_INVOICE_URL;
if ($this->simplified()) {
$invoice_url .= '/'. self::SIMPLIFIED_INVOICE_ADDITIONAL_ENDPOINT;
}

return $invoice_url;
}

public function simplified(): bool
{
return $this->config->get('simplified', false);
}

public function environments(): array
Expand All @@ -86,4 +106,14 @@ public function environments(): array
self::ENV_SANDBOX,
];
}

public function configKeys(): array
{
return [
'email',
'password',
'environment',
'simplified',
];
}
}
58 changes: 56 additions & 2 deletions src/Adapter/Acube/tests/AcubeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,71 @@
use FatturaElettronicaPhp\Sender\Exceptions\InvalidCredentialsException;
use FatturaElettronicaPhp\Sender\Exceptions\InvalidEnvironmentException;

/**
* ./vendor/bin/pest --group=acube
*/
it('cannot send without valid login details', function () {
$sender = new AcubeAdapter([
'environment' => AcubeAdapter::ENV_SANDBOX,
]);
$sender->send('SOME XML');
})->throws(InvalidCredentialsException::class);
})->group('acube')->throws(InvalidCredentialsException::class);

it('cannot send without valid environment', function () {
$sender = new AcubeAdapter([
'email' => 'PIPPO',
'password' => 'PLUTO',
]);
$sender->send('SOME XML');
})->throws(InvalidEnvironmentException::class);
})->group('acube')->throws(InvalidEnvironmentException::class);

it('can send sample invoice', function () {
$credentials = (array)$this->credentials->acube ?? null;
if (! $credentials) {
$this->markTestSkipped();
}

$sender = new AcubeAdapter(
$credentials
);

$path_xml_example = __DIR__ . DIRECTORY_SEPARATOR . "samples/invoice_sample.xml";
$xml = file_get_contents($path_xml_example);
$result = $sender->send($xml);

expect($result->get("uuid"))->toBeString();
})->group('acube');

it('can send multiline invoice', function () {
$credentials = (array)$this->credentials->acube ?? null;
if (! $credentials) {
$this->markTestSkipped();
}

$sender = new AcubeAdapter(
$credentials
);
$path_xml_example = __DIR__ . DIRECTORY_SEPARATOR . "samples/invoice_sample_multiline.xml";
$xml = file_get_contents($path_xml_example);
$result = $sender->send($xml);

expect($result->get("uuid"))->toBeString();
})->group('acube');

it('can send simplified invoice', function () {
$credentials = (array)$this->credentials->acube ?? null;
if (! $credentials) {
$this->markTestSkipped();
}

$sender = new AcubeAdapter(
$credentials + [
'simplified' => true,
],
);
$path_xml_example = __DIR__ . DIRECTORY_SEPARATOR . "samples/invoice_simplified_sample.xml";
$xml = file_get_contents($path_xml_example);
$result = $sender->send($xml);

expect($result->get("uuid"))->toBeString();
})->group('acube');
113 changes: 113 additions & 0 deletions src/Adapter/Acube/tests/samples/invoice_sample.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?xml version="1.0" encoding="UTF-8"?>
<p:FatturaElettronica versione="FPR12" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2 http://www.fatturapa.gov.it/export/fatturazione/sdi/fatturapa/v1.2/Schema_del_file_xml_FatturaPA_versione_1.2.xsd">
<FatturaElettronicaHeader>
<DatiTrasmissione>
<IdTrasmittente>
<IdPaese>IT</IdPaese>
<IdCodice>01234567890</IdCodice>
</IdTrasmittente>
<ProgressivoInvio>00001</ProgressivoInvio>
<FormatoTrasmissione>FPR12</FormatoTrasmissione>
<CodiceDestinatario>ABC1234</CodiceDestinatario>
<ContattiTrasmittente/>
</DatiTrasmissione>
<CedentePrestatore>
<DatiAnagrafici>
<IdFiscaleIVA>
<IdPaese>IT</IdPaese>
<IdCodice>01234567890</IdCodice>
</IdFiscaleIVA>
<Anagrafica>
<Denominazione>SOCIETA' ALPHA SRL</Denominazione>
</Anagrafica>
<RegimeFiscale>RF19</RegimeFiscale>
</DatiAnagrafici>
<Sede>
<Indirizzo>VIALE ROMA 543</Indirizzo>
<CAP>07100</CAP>
<Comune>SASSARI</Comune>
<Provincia>SS</Provincia>
<Nazione>IT</Nazione>
</Sede>
</CedentePrestatore>
<CessionarioCommittente>
<DatiAnagrafici>
<CodiceFiscale>09876543210</CodiceFiscale>
<Anagrafica>
<Denominazione>DITTA BETA</Denominazione>
</Anagrafica>
</DatiAnagrafici>
<Sede>
<Indirizzo>VIA TORINO 38-B</Indirizzo>
<CAP>00145</CAP>
<Comune>ROMA</Comune>
<Provincia>RM</Provincia>
<Nazione>IT</Nazione>
</Sede>
</CessionarioCommittente>
</FatturaElettronicaHeader>
<FatturaElettronicaBody>
<DatiGenerali>
<DatiGeneraliDocumento>
<TipoDocumento>TD01</TipoDocumento>
<Divisa>EUR</Divisa>
<Data>2014-12-18</Data>
<Numero>123</Numero>
<Causale>LA FATTURA FA RIFERIMENTO AD UNA OPERAZIONE AAAA BBBBBBBBBBBBBBBBBB CCC DDDDDDDDDDDDDDD E FFFFFFFFFFFFFFFFFFFF GGGGGGGGGG HHHHHHH II LLLLLLLLLLLLLLLLL MMM NNNNN OO PPPPPPPPPPP QQQQ RRRR SSSSSSSSSSSSSS</Causale>
<Causale>SEGUE DESCRIZIONE CAUSALE NEL CASO IN CUI NON SIANO STATI SUFFICIENTI 200 CARATTERI AAAAAAAAAAA BBBBBBBBBBBBBBBBB</Causale>
</DatiGeneraliDocumento>
<DatiOrdineAcquisto>
<RiferimentoNumeroLinea>1</RiferimentoNumeroLinea>
<IdDocumento>66685</IdDocumento>
<NumItem>1</NumItem>
</DatiOrdineAcquisto>
<DatiContratto>
<RiferimentoNumeroLinea>1</RiferimentoNumeroLinea>
<IdDocumento>123</IdDocumento>
<Data>2012-09-01</Data>
<NumItem>5</NumItem>
<CodiceCUP>123abc</CodiceCUP>
<CodiceCIG>456def</CodiceCIG>
</DatiContratto>
<DatiTrasporto>
<DatiAnagraficiVettore>
<IdFiscaleIVA>
<IdPaese>IT</IdPaese>
<IdCodice>24681012141</IdCodice>
</IdFiscaleIVA>
<Anagrafica>
<Denominazione>Trasporto spa</Denominazione>
</Anagrafica>
</DatiAnagraficiVettore>
<DataOraConsegna>2012-10-22T16:46:12.000+02:00</DataOraConsegna>
</DatiTrasporto>
</DatiGenerali>
<DatiBeniServizi>
<DettaglioLinee>
<NumeroLinea>1</NumeroLinea>
<Descrizione>DESCRIZIONE DELLA FORNITURA</Descrizione>
<Quantita>5.00</Quantita>
<PrezzoUnitario>1.00</PrezzoUnitario>
<PrezzoTotale>5.00</PrezzoTotale>
<AliquotaIVA>22.00</AliquotaIVA>
</DettaglioLinee>
<DatiRiepilogo>
<AliquotaIVA>22.00</AliquotaIVA>
<ImponibileImporto>5.00</ImponibileImporto>
<Imposta>1.10</Imposta>
<EsigibilitaIVA>I</EsigibilitaIVA>
</DatiRiepilogo>
</DatiBeniServizi>
<DatiPagamento>
<CondizioniPagamento>TP01</CondizioniPagamento>
<DettaglioPagamento>
<ModalitaPagamento>MP01</ModalitaPagamento>
<DataScadenzaPagamento>2015-01-30</DataScadenzaPagamento>
<ImportoPagamento>6.10</ImportoPagamento>
</DettaglioPagamento>
</DatiPagamento>
</FatturaElettronicaBody>
</p:FatturaElettronica>
Loading