Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate UK VAT numbers #116

Merged
merged 1 commit into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"orchestra/testbench": "~3.5|^4.0|^5.0|^6.0",
"phpunit/phpunit": "^6.5|^7.5|^8.5|^9.5"
},
"suggest": {
"ext-curl": "Required when you need to validate UK VAT numbers."
},
"autoload": {
"classmap": [
"src/controllers/Controller.php"
Expand Down
70 changes: 52 additions & 18 deletions src/Mpociot/VatCalculator/VatCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,11 @@ class VatCalculator
*/
protected $businessCountryCode;

/**
* @var string
*/
protected $ukValidationEndpoint = 'https://api.service.hmrc.gov.uk';

/**
* @param \Illuminate\Contracts\Config\Repository
*/
Expand Down Expand Up @@ -664,10 +669,10 @@ public function isValidVATNumber($vatNumber)
$details = self::getVATDetails($vatNumber);

if ($details) {
return $details->valid;
} else {
return false;
return is_array($details) ? isset($details['vatNumber']) : $details->valid;
}

return false;
}

/**
Expand All @@ -682,25 +687,43 @@ public function getVATDetails($vatNumber)
$vatNumber = str_replace([' ', "\xC2\xA0", "\xA0", '-', '.', ','], '', trim($vatNumber));
$countryCode = substr($vatNumber, 0, 2);
$vatNumber = substr($vatNumber, 2);
$this->initSoapClient();
$client = $this->soapClient;
if ($client) {
try {
$result = $client->checkVat([
'countryCode' => $countryCode,
'vatNumber' => $vatNumber,
]);

return $result;
} catch (SoapFault $e) {
if (isset($this->config) && $this->config->get('vat_calculator.forward_soap_faults')) {
throw new VATCheckUnavailableException($e->getMessage(), $e->getCode(), $e->getPrevious());
}

if (strtoupper($countryCode) === 'GB' && extension_loaded('curl')) {
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$this->ukValidationEndpoint/organisations/vat/check-vat-number/lookup/$vatNumber");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'VatCalculator');
$result = curl_exec($curl_handle);
curl_close($curl_handle);

$response = json_decode($result, true, 512, JSON_OBJECT_AS_ARRAY);

if (isset($response['code']) && $response['code'] === 'NOT_FOUND') {
return false;
}

return $response['target'];
} else {
$this->initSoapClient();
$client = $this->soapClient;

if ($client) {
try {
return $client->checkVat([
'countryCode' => $countryCode,
'vatNumber' => $vatNumber,
]);
} catch (SoapFault $e) {
if (isset($this->config) && $this->config->get('vat_calculator.forward_soap_faults')) {
throw new VATCheckUnavailableException($e->getMessage(), $e->getCode(), $e->getPrevious());
}

return false;
}
}
throw new VATCheckUnavailableException('The VAT check service is currently unavailable. Please try again later.');
}
throw new VATCheckUnavailableException('The VAT check service is currently unavailable. Please try again later.');
}

/**
Expand Down Expand Up @@ -731,4 +754,15 @@ public function setSoapClient($soapClient)
{
$this->soapClient = $soapClient;
}

/**
* @return $this
* @internal This method is not covered by our BC policy.
*/
public function testing()
{
$this->ukValidationEndpoint = 'https://test-api.service.hmrc.gov.uk';

return $this;
}
}
18 changes: 18 additions & 0 deletions tests/VatCalculatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,24 @@ public function testCannotValidateVATNumberWhenServiceIsDown()
$vatCalculator->isValidVATNumber($vatNumber);
}

public function testCanValidateValidUKVATNumber()
{
$config = m::mock('Illuminate\Contracts\Config\Repository');

$config->shouldReceive('has')
->once()
->with('vat_calculator.business_country_code')
->andReturn(false);

$result = new \stdClass();
$result->valid = true;

$vatNumber = 'GB 553557881';
$vatCalculator = new VatCalculator($config);
$result = $vatCalculator->testing()->isValidVATNumber($vatNumber);
$this->assertTrue($result);
}

/**
* @link https://tools.tracemyip.org/search--country/germany
*/
Expand Down