You are here

protected function EuropeanUnionVat::doVerify in Commerce Core 8.2

Performs the tax number verification.

Parameters

string $tax_number: The tax number.

Return value

\Drupal\commerce_tax\Plugin\Commerce\TaxNumberType\VerificationResult The verification result.

Overrides TaxNumberTypeWithVerificationBase::doVerify

File

modules/tax/src/Plugin/Commerce/TaxNumberType/EuropeanUnionVat.php, line 52

Class

EuropeanUnionVat
Provides the European Union VAT tax number type.

Namespace

Drupal\commerce_tax\Plugin\Commerce\TaxNumberType

Code

protected function doVerify($tax_number) {
  $time = $this->time
    ->getRequestTime();

  // The SOAP extension is not a Commerce requirement, since only this
  // plugin needs it. The check is skipped in test environments because
  // a mock client will be used instead.
  if (!extension_loaded('soap') && !drupal_valid_test_ua()) {
    return VerificationResult::failure($time, [
      'error' => 'no_extension',
    ]);
  }
  $patterns = $this
    ->getValidationPatterns();
  $prefix = substr($tax_number, 0, 2);
  if (!isset($patterns[$prefix])) {
    return VerificationResult::failure($time, [
      'error' => 'invalid_number',
    ]);
  }
  $number = substr($tax_number, 2);
  try {
    $parameters = [
      'countryCode' => $prefix,
      'vatNumber' => $number,
    ];
    $soap_client = $this
      ->getSoapClient();
    $result = $soap_client
      ->__soapCall('checkVat', [
      $parameters,
    ]);
  } catch (\SoapFault $e) {
    return VerificationResult::unknown($time, [
      'error' => $e->faultstring,
    ]);
  }
  if ($result->valid) {
    return VerificationResult::success($time, [
      'name' => $result->name,
      'address' => $result->address,
    ]);
  }
  else {
    return VerificationResult::failure($time);
  }
}