2023-03-21 21:30:37 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Tax\Providers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Client\Response;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
|
2023-03-26 22:14:10 +02:00
|
|
|
class ZipTax implements TaxProviderInterface
|
2023-03-21 21:30:37 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
private string $endpoint = 'https://api.zip-tax.com/request/v40';
|
|
|
|
|
2023-03-26 22:14:10 +02:00
|
|
|
private string $api_key = '';
|
|
|
|
|
|
|
|
public function __construct(protected array $address)
|
2023-03-21 21:30:37 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
2023-05-15 05:57:00 +02:00
|
|
|
$string_address = implode(" ", $this->address);
|
2023-03-21 21:30:37 +01:00
|
|
|
|
2023-05-15 05:57:00 +02:00
|
|
|
$response = $this->callApi(['key' => $this->api_key, 'address' => $string_address]);
|
2023-03-21 21:30:37 +01:00
|
|
|
|
|
|
|
if($response->successful())
|
|
|
|
return $response->json();
|
|
|
|
|
2023-04-25 23:56:47 +02:00
|
|
|
if(isset($this->address['postal_code'])) {
|
|
|
|
$response = $this->callApi(['key' => $this->api_key, 'address' => $this->address['postal_code']]);
|
2023-03-21 21:30:37 +01:00
|
|
|
|
2023-05-15 05:57:00 +02:00
|
|
|
nlog($response->json());
|
|
|
|
nlog($response->body());
|
|
|
|
|
2023-03-21 21:30:37 +01:00
|
|
|
if($response->successful())
|
|
|
|
return $response->json();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->throw();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-26 22:14:10 +02:00
|
|
|
public function setApiCredentials($api_key): self
|
|
|
|
{
|
|
|
|
$this->api_key = $api_key;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-03-21 21:30:37 +01:00
|
|
|
/**
|
|
|
|
* callApi
|
|
|
|
*
|
|
|
|
* @param array $parameters
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
private function callApi(array $parameters): Response
|
|
|
|
{
|
|
|
|
$response = Http::retry(3, 1000)->withHeaders([])->get($this->endpoint, $parameters);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|