You are here

function _vat_number_validate_vat in VAT Number 7

Check a VAT number. Return error message on error, FALSE on success.

1 call to _vat_number_validate_vat()
vat_number_field_widget_element_validate in ./vat_number.module
Custom validation callback for vat_number_field_widget_form()

File

./vat_number.inc, line 414
vat_number.inc

Code

function _vat_number_validate_vat($vat_number, $skip_validation_on_service_failure = FALSE) {
  $vat_infos = _vat_number_components($vat_number);
  $client = _vat_number_connect_vies_database();
  if ($client) {
    $params = array(
      'countryCode' => $vat_infos['country_code'],
      'vatNumber' => $vat_infos['vat_number'],
    );
    try {
      $r = $client
        ->checkVat($params);
      return $r->valid == TRUE ? TRUE : FALSE;
    } catch (SoapFault $e) {
      watchdog('vat_number', $e->faultstring, NULL, WATCHDOG_ERROR);
      if ($skip_validation_on_service_failure) {

        // Something is wrong, but that's fine. Pass the validation..
        return TRUE;
      }
    }
  }
  elseif ($skip_validation_on_service_failure) {

    // We couldn't connect but we are allowed to pass the validation.
    return TRUE;
  }
  else {

    // No $client and can't skip.
    drupal_set_message(t('The connection to the European VAT database server failed and the VAT could not be validated, please contact us about this problem.'), 'error');
    return FALSE;
  }
}