1. Go to this page and download the library: Download sevaske/zatca-api library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sevaske / zatca-api example snippets
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use Sevaske\ZatcaApi\ZatcaClient;
$httpClient = new Client();
$factory = new HttpFactory();
// Initialize ZatcaClient with sandbox environment
$client = new ZatcaClient(
$httpClient,
$factory, // RequestFactoryInterface
$factory, // StreamFactoryInterface
'sandbox' // environment: sandbox | simulation | production
);
use Sevaske\ZatcaApi\ZatcaAuth;
use Sevaske\ZatcaApi\Exceptions\ZatcaRequestException;
use Sevaske\ZatcaApi\Exceptions\ZatcaResponseException;
/**
* @var Sevaske\ZatcaApi\ZatcaClient $client
* @var Sevaske\ZatcaApi\Responses\ProductionCertificateResponse $productionCertificateResponse
*/
$productionClient = $client->withEnvironment('production');
$productionAuth = new ZatcaAuth($productionCertificateResponse->certificate(), $productionCertificateResponse->secret());
$productionClient->setAuthToken($productionAuth);
try {
// submitting production invoices
$responseReporting = $productionClient->reportingInvoice('my real B2P invoice xml', 'hash', 'uuid');
$responseClearance = $productionClient->clearanceInvoice('my real B2B invoice xml', 'hash', 'uuid');
} catch (ZatcaRequestException|ZatcaResponseException $e) {
// handle
}
print_r($responseReporting->toArray());
print_r($responseClearance->toArray());
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
interface MiddlewareInterface
{
/**
* @param RequestInterface $request The incoming request
* @param callable $next Callable to forward the request to the next middleware or the HTTP client
* @return ResponseInterface
*/
public function handle(RequestInterface $request, callable $next): ResponseInterface;
}
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Sevaske\ZatcaApi\Interfaces\MiddlewareInterface;
// Attach a custom middleware to inspect requests and responses
$client = $client->withMiddleware(new class implements MiddlewareInterface
{
public function handle(RequestInterface $request, callable $next): ResponseInterface
{
// request
$this->info('URL: ');
$this->info((string) $request->getUri());
$this->info('Body: ');
$this->info($this->safeStreamContents($request->getBody()));
/**
* @var $response \Psr\Http\Message\ResponseInterface
*/
$response = $next($request);
// response
$this->info('Response:');
$this->info($this->safeStreamContents($response->getBody()));
return $response;
}
private function safeStreamContents(\Psr\Http\Message\StreamInterface $stream): string
{
if (! $stream->isSeekable()) {
return '[unseekable stream]';
}
// Save original cursor position
$pos = $stream->tell();
// Read from beginning
$stream->rewind();
$content = $stream->getContents();
// Restore original cursor
$stream->seek($pos);
return $content;
}
private function info(string $text): void
{
echo "\n\r".$text;
}
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.