Skip to content

Commit

Permalink
F#19807 Updated the Vat-check with KBO enterprise call + now stores t…
Browse files Browse the repository at this point in the history
…he Resolver that generated the result.
  • Loading branch information
RoanB committed Apr 7, 2022
1 parent 3150fa1 commit e8cfa11
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 35 deletions.
47 changes: 24 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

## Description

Check Vat number in 4 steps:
1. syntax via a predefined regex array (list available in config)
2. is european
3. caching table (prevent denial of VIES service)
4. VIES service
Check Vat number with separate resolvers (these are the default steps):
1. Is european (always performed not configurable)
2. Syntax via a predefined regex array (list available in config)
3. Caching table (to check if there were recent results for KBO and VIES + avoid taxing the API when not needed)
4. VIES service check

## Installation

Expand Down Expand Up @@ -44,41 +44,42 @@ Run the initial migration or executed the following queries
$country = $your_country_object;

/**
* A string for the fail reason. This will be passed by reference and the
* error can be retrieved after calling the validate method
* The resolver used to give you the result, passed by reference and the
* resolver_used can be retrieved after calling the validate method
*/
$reason = '';

/**
* A flag to ignore a cache lookup
*/
$ignore_cache = false;
$resolver_used = '';

/**
* Perform the call
*/
\Skeleton\Vat\Check\Check::validate($vat_number, $country, $reason, $ignore_cache)
\Skeleton\Vat\Check\Check::validate($vat_number, $country, $resolver_used)

## Optional config

By default the validator will do these steps: Check Syntax, check recent cache (for API results) and Check against Vies.
By default the validator will do these steps:
1. Is european (always performed not configurable)
2. Syntax via a predefined regex array (list available in config)
3. Caching table (to check if there were recent results for KBO and VIES + avoid taxing the API when not needed)
4. VIES service checkCheck Syntax, check recent cache (for API results) and Check against Vies.

It is now possible to change the order of these steps and it is possible to do an aditional step against the KBO (requires authentication).

/**
* Example config
*/
\Skeleton\Vat\Check\Config::set_resolvers([
new \Skeleton\Vat\Check\Resolver\Syntax(),
new \Skeleton\Vat\Check\Resolver\Cache(),
new \Skeleton\Vat\Check\Resolver\Kbo(), // Not used by default
new \Skeleton\Vat\Check\Resolver\Vies()
]);
new \Skeleton\Vat\Check\Resolver\Syntax(),
new \Skeleton\Vat\Check\Resolver\Cache(),
new \Skeleton\Vat\Check\Resolver\Kbo(), // Not used by default
new \Skeleton\Vat\Check\Resolver\Vies()
]);

/**
* Example auhtentication
*/
\Skeleton\Vat\Check\Config::$kbo_authentication = ['user' => '[email protected]',
key' => 'test_key'
];
\Skeleton\Vat\Check\Config::$kbo_authentication = [
'user' => '[email protected]',
key' => 'test_key'
];


6 changes: 2 additions & 4 deletions lib/Skeleton/Vat/Check/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

namespace Skeleton\Vat\Check;

use Skeleton\Core\Skeleton;

class Check {
/**
* Validate a VAT number
Expand All @@ -20,11 +18,10 @@ class Check {
* @param \Country $country
* @return boolean $valid
*/
public static function validate($vat_number, \Country $country, &$reason = '') {
public static function validate($vat_number, \Country $country, &$resolver_used = null) {
// Default check
$vat_config = \Skeleton\Vat\Check\Config::$vat_config;
if (!isset($vat_config[ $country->get_iso2() ])) {
$reason = 'not_european';
return true;
}

Expand All @@ -43,6 +40,7 @@ public static function validate($vat_number, \Country $country, &$reason = '') {
foreach ($resolvers as $resolver) {
try {
$result = $resolver->resolve($vat_number, $country);
$resolver_used = $resolver;
} catch (Answer\Exception $e) {
continue;
}
Expand Down
24 changes: 16 additions & 8 deletions lib/Skeleton/Vat/Check/Resolver/Kbo.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/

namespace Skeleton\Vat\Check\Resolver;

use Exception;
use Skeleton\Vat\Check\Answer;
use GuzzleHttp\Client;

Expand Down Expand Up @@ -47,14 +49,15 @@ public static function resolve($vat_number, \Country $country) {
} catch (\Exception $e) {
$retry--;
if ($retry === 0) {
throw new Answer\Exception('Multiple exceptions from the API');
throw new Answer\Exception($e);
}
}
}
}


/**
* Try to check the VAT number online against the KBO database.
* Try to check the enterprise number online against the KBO database.
* This call can only be used for BE numbers.
*
* @access public
Expand All @@ -65,14 +68,19 @@ public static function resolve($vat_number, \Country $country) {
*/
public static function validate_call($vat_number, $kbo_authentication) {
$client = new Client();
$result = $client->get("https://api.kbodata.app/v2/vat/BE" . trim($vat_number), [
try{
// We only need to check if it exists
$client->get("https://api.kbodata.app/v2/enterprise/" . trim($vat_number), [
'auth' => [$kbo_authentication['user'], $kbo_authentication['key']]
]);
$vat = json_decode($result->getBody())->Vat;
if ($vat->isValid == 1) {
return true;
} else {
return false;
} catch (\GuzzleHttp\Exception\ClientException $e) {
// 404 Not found is the expected response
if($e->getResponse()->getStatusCode() == '404') {
return false;
} else {
throw $e;
}
}
return true;
}
}

0 comments on commit e8cfa11

Please sign in to comment.