Mpociot\VatCalculator\VatCalculator::isValidVATNumber PHP Method

isValidVATNumber() public method

public isValidVATNumber ( $vatNumber ) : boolean
$vatNumber
return boolean
    public function isValidVATNumber($vatNumber)
    {
        $vatNumber = str_replace([' ', '-', '.', ','], '', trim($vatNumber));
        $countryCode = substr($vatNumber, 0, 2);
        $vatNumber = substr($vatNumber, 2);
        $client = $this->soapClient;
        if ($client) {
            try {
                $result = $client->checkVat(['countryCode' => $countryCode, 'vatNumber' => $vatNumber]);
                return $result->valid;
            } catch (SoapFault $e) {
                return false;
            }
        }
        throw new VATCheckUnavailableException('The VAT check service is currently unavailable. Please try again later.');
    }

Usage Example

Example #1
0
 /**
  * Returns the tax rate for the given country.
  *
  * @param string $vat_id
  *
  * @throws \Mpociot\VatCalculator\Exceptions\VATCheckUnavailableException
  *
  * @return \Illuminate\Http\Response
  */
 public function validateVATID($vat_id)
 {
     try {
         $isValid = $this->calculator->isValidVATNumber($vat_id);
         $message = '';
     } catch (VATCheckUnavailableException $e) {
         $isValid = false;
         $message = $e->getMessage();
     }
     return ['vat_id' => $vat_id, 'is_valid' => $isValid, 'message' => $message];
 }