1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 13:42:49 +01:00
invoiceninja/app/Services/Tax/VatNumberCheck.php

77 lines
1.8 KiB
PHP
Raw Normal View History

2023-03-19 05:09:50 +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;
class VatNumberCheck
{
2023-03-19 06:14:04 +01:00
private array $response = [];
2023-03-19 05:09:50 +01:00
2023-05-25 09:19:52 +02:00
public function __construct(protected ?string $vat_number, protected string $country_code)
2023-03-19 05:09:50 +01:00
{
}
public function run()
{
return $this->checkvat_number();
}
2023-03-19 06:14:04 +01:00
private function checkvat_number(): self
2023-03-19 05:09:50 +01:00
{
$wsdl = "https://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl";
2023-03-19 06:14:04 +01:00
2023-03-19 05:09:50 +01:00
try {
$client = new \SoapClient($wsdl);
$params = [
'countryCode' => $this->country_code,
2023-05-25 09:19:52 +02:00
'vatNumber' => $this->vat_number ?? ''
2023-03-19 05:09:50 +01:00
];
$response = $client->checkVat($params);
if ($response->valid) {
2023-03-19 06:14:04 +01:00
$this->response = [
2023-03-19 05:09:50 +01:00
'valid' => true,
'name' => $response->name,
'address' => $response->address
];
} else {
2023-03-19 06:14:04 +01:00
$this->response = ['valid' => false];
2023-03-19 05:09:50 +01:00
}
} catch (\SoapFault $e) {
2023-03-19 06:14:04 +01:00
2023-10-26 04:57:44 +02:00
$this->response = ['valid' => false, 'error' => $e->getMessage()];
2023-03-19 05:09:50 +01:00
}
2023-03-19 06:14:04 +01:00
return $this;
2023-03-19 05:09:50 +01:00
}
2023-03-19 06:14:04 +01:00
public function getResponse()
{
return $this->response;
}
public function isValid(): bool
{
return $this->response['valid'];
}
2023-05-25 09:19:52 +02:00
public function getName()
{
return isset($this->response['name']) ? $this->response['name'] : '';
}
public function getAddress()
{
return isset($this->response['address']) ? $this->response['address'] : '';
}
2023-03-19 05:09:50 +01:00
}