From e8cfa115da99abd4148038c316f90e0be4dbabc7 Mon Sep 17 00:00:00 2001 From: Roan Buysse Date: Thu, 7 Apr 2022 13:46:00 +0200 Subject: [PATCH] F#19807 Updated the Vat-check with KBO enterprise call + now stores the Resolver that generated the result. --- README.md | 47 +++++++++++++------------ lib/Skeleton/Vat/Check/Check.php | 6 ++-- lib/Skeleton/Vat/Check/Resolver/Kbo.php | 24 ++++++++----- 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 3f2021c..15d33b4 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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' => 'test@tigron.be', - key' => 'test_key' - ]; + \Skeleton\Vat\Check\Config::$kbo_authentication = [ + 'user' => 'test@tigron.be', + key' => 'test_key' + ]; diff --git a/lib/Skeleton/Vat/Check/Check.php b/lib/Skeleton/Vat/Check/Check.php index b41b70e..fe5d45c 100644 --- a/lib/Skeleton/Vat/Check/Check.php +++ b/lib/Skeleton/Vat/Check/Check.php @@ -8,8 +8,6 @@ namespace Skeleton\Vat\Check; -use Skeleton\Core\Skeleton; - class Check { /** * Validate a VAT number @@ -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; } @@ -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; } diff --git a/lib/Skeleton/Vat/Check/Resolver/Kbo.php b/lib/Skeleton/Vat/Check/Resolver/Kbo.php index 59d3f2a..ed21f14 100644 --- a/lib/Skeleton/Vat/Check/Resolver/Kbo.php +++ b/lib/Skeleton/Vat/Check/Resolver/Kbo.php @@ -7,6 +7,8 @@ */ namespace Skeleton\Vat\Check\Resolver; + +use Exception; use Skeleton\Vat\Check\Answer; use GuzzleHttp\Client; @@ -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 @@ -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; } }