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

Add function to detect is country is valid or not. #15

Closed
ortegafernando opened this issue Jun 22, 2024 · 1 comment
Closed

Add function to detect is country is valid or not. #15

ortegafernando opened this issue Jun 22, 2024 · 1 comment

Comments

@ortegafernando
Copy link

ortegafernando commented Jun 22, 2024

Hi, about my last PR I have added some useful code. Again, not pretty:

EuropaTIN class:

    protected const TIN_EU_COUNTRY_LIST = ['AT','BE','BG','CY','CZ','DE','DK','EE','EL','ES','FI','FR','HR','HU','IE','IT','LU','LV','LT','MT','NL','PL','PT','RO','SE','SI','SK'];
    private const COUNTRY_NOT_VALID = 'Country not valid in Europa TIN Service: %s';

    public function getResource($sTinNumber, string $sCountryCode): stdClass
    {
        if (!in_array(strtoupper($sCountryCode), self::TIN_EU_COUNTRY_LIST)) {
            throw new Exception(
                sprintf(self::COUNTRY_NOT_VALID, strtoupper($sCountryCode))
            );
        }
        try {
            $aDetails = [
                'countryCode' => strtoupper($sCountryCode),
                'tinNumber' => $sTinNumber
            ];
            return $this->oClient->checkTin($aDetails);
        } catch (SoapFault $oExcept) {
            throw new Exception(
                sprintf(self::IMPOSSIBLE_RETRIEVE_DATA_MESSAGE, $oExcept->faultstring)
            );
        }
    }

And EuropaVAT class (it also has Northern Ireland):

    protected const VAT_EU_COUNTRY_LIST = ['AT','BE','BG','CY','CZ','DE','DK','EE','EL','ES','FI','FR','HR','HU','IE','IT','LU','LV','LT','MT','NL','PL','PT','RO','SE','SI','SK','XI'];
    private const COUNTRY_NOT_VALID = 'Country not valid in Europa VAT Service: %s';

    public function getResource($sVatNumber, string $sCountryCode): stdClass
    {
        if (!in_array(strtoupper($sCountryCode), self::VAT_EU_COUNTRY_LIST)) {
            throw new Exception(
                sprintf(self::COUNTRY_NOT_VALID, strtoupper($sCountryCode))
            );
        }        
        try {
            $aDetails = [
                'countryCode' => strtoupper($sCountryCode),
                'vatNumber' => $sVatNumber
            ];
            return $this->oClient->checkVat($aDetails);
        } catch (SoapFault $oExcept) {
            throw new Exception(
                sprintf(self::IMPOSSIBLE_RETRIEVE_DATA_MESSAGE, $oExcept->faultstring)
            );
        }
    }

May you can implement this any other way or as you think is better.

Thanks a lot

@ortegafernando
Copy link
Author

Added to PR #14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant