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
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
if($response->successful()) {
|
2023-05-15 06:38:11 +02:00
|
|
|
return $this->parseResponse($response->json());
|
|
|
|
}
|
2023-03-21 21:30:37 +01:00
|
|
|
|
2023-04-25 23:56:47 +02:00
|
|
|
if(isset($this->address['postal_code'])) {
|
2023-10-26 04:57:44 +02:00
|
|
|
$response = $this->callApi(['key' => $this->api_key, 'address' => $this->address['postal_code']]);
|
2023-03-21 21:30:37 +01:00
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
if($response->successful()) {
|
2023-05-15 06:38:11 +02:00
|
|
|
return $this->parseResponse($response->json());
|
2023-10-26 04:57:44 +02:00
|
|
|
}
|
2023-03-21 21:30:37 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-06-02 07:53:33 +02:00
|
|
|
return null;
|
2023-03-21 21:30:37 +01:00
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
2023-06-02 07:53:33 +02:00
|
|
|
return Http::retry(3, 1000)->withHeaders([])->get($this->endpoint, $parameters);
|
2023-03-21 21:30:37 +01:00
|
|
|
|
|
|
|
}
|
2023-05-15 06:38:11 +02:00
|
|
|
|
|
|
|
private function parseResponse($response)
|
|
|
|
{
|
2023-06-02 07:53:33 +02:00
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
if(isset($response['rCode']) && $response['rCode'] == 100 && isset($response['results']['0'])) {
|
2023-05-15 13:49:38 +02:00
|
|
|
return $response['results']['0'];
|
2023-10-26 04:57:44 +02:00
|
|
|
}
|
2023-05-15 06:38:11 +02:00
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
if(isset($response['rCode']) && class_exists(\Modules\Admin\Events\TaxProviderException::class)) {
|
2023-06-02 07:53:33 +02:00
|
|
|
event(new \Modules\Admin\Events\TaxProviderException($response['rCode']));
|
2023-10-26 04:57:44 +02:00
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-05-17 12:55:27 +02:00
|
|
|
return null;
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-05-15 06:38:11 +02:00
|
|
|
}
|
2023-03-21 21:30:37 +01:00
|
|
|
}
|