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

67 lines
1.5 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
public function __construct(protected string $vat_number, protected string $country_code)
{
}
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,
'vatNumber' => $this->vat_number
];
$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
$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-03-19 05:09:50 +01:00
}