-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add CVR API #2
Draft
bilfeldt
wants to merge
1
commit into
main
Choose a base branch
from
features/cvr-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add CVR API #2
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
|
||
namespace Bilfeldt\VatService\Drivers; | ||
|
||
use Bilfeldt\VatService\Exceptions\DriverUnavailable; | ||
use Bilfeldt\VatService\Exceptions\InvalidVatException; | ||
use Bilfeldt\VatService\Exceptions\UnsupportedCountryException; | ||
use Bilfeldt\VatService\Exceptions\VatNotFoundException; | ||
use Bilfeldt\VatService\VatInformation; | ||
use Bilfeldt\VatService\VatServiceInterface; | ||
use Illuminate\Http\Client\Response; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
class CvrApi implements VatServiceInterface | ||
{ | ||
private static array $formats = [ | ||
'DK' => [ | ||
'########' | ||
], | ||
'NO' => [ | ||
// empty means any format is accepted | ||
], | ||
]; | ||
|
||
public static function supports(string $countryCode): bool | ||
{ | ||
return array_key_exists(strtoupper($countryCode), self::$formats); | ||
} | ||
|
||
public function __construct( | ||
private ?string $accessToken, // This api actually works without an access token, but then rate limiting is applied. | ||
) {} | ||
|
||
/** @inheritDoc */ | ||
public function getFormats(string $countryCode): Collection | ||
{ | ||
if (! self::supports($countryCode)) { | ||
throw UnsupportedCountryException::unsupported($countryCode); | ||
} | ||
|
||
return Collection::make(self::$formats[strtoupper($countryCode)]); | ||
} | ||
|
||
/** @inheritDoc */ | ||
public function isValidFormat(string $countryCode, string $vatNumber): bool | ||
{ | ||
if (! self::supports($countryCode)) { | ||
throw UnsupportedCountryException::unsupported($countryCode); | ||
} | ||
|
||
return $this | ||
->getFormats($countryCode) | ||
->whenEmpty( | ||
fn (Collection $collection) => true, | ||
function (Collection $collection) use ($vatNumber): bool { | ||
return $collection->filter(function (string $format) use ($vatNumber) { | ||
return true; // TODO: Fix this | ||
})->isNotEmpty(); | ||
} | ||
); | ||
} | ||
|
||
/** @inheritDoc */ | ||
public function isValid(string $countryCode, string $vatNumber): bool | ||
{ | ||
if (! $this->isValidFormat($countryCode, $vatNumber)) { | ||
return false; | ||
} | ||
|
||
try { | ||
$this->getInformation($countryCode, $vatNumber); | ||
|
||
return true; | ||
} catch (InvalidVatException $e) { | ||
return false; | ||
} catch (VatNotFoundException $e) { | ||
return false; // The driver is "complete" so any number not found is assumed to be invalid | ||
} | ||
} | ||
|
||
/** @inheritDoc */ | ||
public function validate(string $countryCode, string $vatNumber): void | ||
{ | ||
// TODO: Implement | ||
} | ||
|
||
/** @inheritDoc */ | ||
public function getInformation(string $countryCode, string $vatNumber): VatInformation | ||
{ | ||
$response = $this->searchByVatNumber($countryCode, $vatNumber); | ||
|
||
if ($response->status() === 404) { | ||
throw InvalidVatException::doesNotExist($vatNumber); | ||
} | ||
|
||
if ($response->isSuccessful()) { | ||
throw new DriverUnavailable('CVR API is currently unavailable.'); | ||
} | ||
|
||
return new VatInformation( | ||
company: $response->json('name'), | ||
vatNumber: $response->json('vat'), | ||
contact: null, | ||
address: $response->json('address'), | ||
postalCode: $response->json('zipcode'), | ||
city: $response->json('city'), | ||
state: null, | ||
region: null, | ||
country: $countryCode, | ||
sms: null, | ||
phone: $response->json('phone'), | ||
email: $response->json('email'), | ||
); | ||
} | ||
|
||
private function getUrl(): string | ||
{ | ||
return 'https://cvrapi.dk/api'; | ||
} | ||
|
||
private function getUserAgent(): string | ||
{ | ||
// Required format described here: https://cvrapi.dk/documentation | ||
return 'SmartSend - VatService - Anders Bilfeldt [email protected]'; | ||
} | ||
|
||
private function searchByVatNumber(string $countryCode, string $vatNumber): Response | ||
{ | ||
return Http::withUserAgent($this->getUserAgent()) | ||
->get($this->getUrl(), [ | ||
'vat' => $vatNumber, | ||
'country' => $countryCode, | ||
'version' => 6, | ||
'format' => 'json', | ||
'token' => $this->accessToken, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bilfeldt should we not throw
InvalidVatException
here as it is invalid format instead of returning false? same with other two exception down the line.Should we not throw exception here and let the package consumer handle the exception how they want to response to those exception in their app as there are 3 different scenarios are at play here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No the point here is that if the vat number has an invalid format, then we know that it is wrong, and we should return
false
.This function should only throw an exception if it is not capable of determining if the vat number is valid or not, which can only happen because of two things:
Makes sense?