0

I'm trying to write a vat number validation function which checks a vat number to see if it's valid.

Currently i've got this

function tep_check_vatnum($countries_id, $vatnum) {
$country = tep_get_countries_with_iso_codes($countries_id);

$iso = $country['countries_iso_code_2'];

if($iso == "GB"){
    return false;
} else {

    $url = "http://isvat.appspot.com/$iso/$vatnum";

      $session = curl_init();

      curl_setopt($session, CURLOPT_URL, $url);
      // curl_setopt($session, CURLOPT_HTTPGET, 1); 
      // curl_setopt($session, CURLOPT_HEADER, false);
      // curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/xml', 'Content-Type: application/xml'));
      curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

      $response = curl_exec($session);
      //echo "<!-- NOTE:" . $response . "-->";
      curl_close($session);
      if($response == "true"){
        return true;
      } else {
        return false;
      }
   }
}

The service i'm using http://isvat.appspot.com is working, But it's not working.

EDIT

Hmm no luck. By not working, it keeps giving me an error saying the vat number isn't valid. On the create account form i've got this code.

<?php echo tep_draw_input_field('vatnum', '', 'class="input"') . '&nbsp;' . (tep_not_null(ENTRY_COMPANY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COMPANY_TEXT . '</span>': ''); ?>


if (is_numeric($country) == false) {
  $error = true;

  $messageStack->add('create_account', ENTRY_COUNTRY_ERROR);
} else {
    if(strlen($vatnum) != 0){
        if(is_numeric($vatnum) == false || tep_check_vatnum($country, $vatnum) == false){
            $error = true;

            $messageStack->add('create_account', "VAT Number is not a valid EU, non-UK VAT no.");
        }
    }
Adam Boustead
  • 143
  • 1
  • 4
  • 12

1 Answers1

0

I guess there might be a problem invoking cURL. The good news is, you probably don't need to. ;) Give this a try:

function tep_check_vatnum($countries_id, $vatnum) {
    $country = tep_get_countries_with_iso_codes($countries_id);
    $iso = $country['countries_iso_code_2'];

    if($iso == "GB"){
        return false;
    } else {
        $url = "http://isvat.appspot.com/$iso/$vatnum";

        $context = stream_context_create(array(
            'http' => array(
                'proxy' => 'http://PROXY_IP:PROXY_PORT'
            ),
        ));

        $fp = fopen($url,'r',$context);
        $response = fgets($fp);
        fclose($fp);

        //echo "<!-- NOTE:" . $response . "-->";

        if($response == "true"){
            return true;
        } else {
            return false;
        }
    }
}

It should work even if you don't have the cURL extension enabled.

Edit: As Michas said, it is difficult to find the problem with your original code without knowing what you mean by "not working". You could try to get some more information about the problem by stating error_reporting(E_ALL) or curl_error($session). Possible causes: cURL is not installed or configured correctly or a proxy or firewall is blocking your request. While the code I suggested would eliminate the first one, the second one might also apply using it, depending on your settings.

Edit 2: Added simple proxy configuration

ahuemmer
  • 1,653
  • 9
  • 22
  • 29
  • Hmmm, for me it works using the code. Tried with `$iso=DE` and `$vatnum=129274202` as well as with the example supplied at the site (when temporarily commenting out the `if($iso == "GB")`check. Might the problem be hidden elsewhere? Can you check the parameters `$countries_id, $vatnum` passed in? Does `$iso` contain something senseful? – ahuemmer Jun 07 '15 at 09:22
  • The $iso contains the two letter county code which i'm guessing is parsed from function tep_get_countries_with_iso_codes($countries_id) { return tep_get_countries($countries_id, true); } – Adam Boustead Jun 07 '15 at 10:57
  • After further investigation I think it may be something to do with a firewall etc. I've replaced the url with a hard coded valid http://isvat.appspot.com/GB/802311782/ and it still rejects it – Adam Boustead Jun 07 '15 at 11:17
  • OK, if you're behind a firewall, there's possible little you can do but asking your admin to allow your request. But often this behaviour is also caused by a mandatory proxy server. Then you could try to find out your proxy settings and configure them as above (I edited my code). If you need some more sophisticated setup (like proxy authentification, ...), you could take a look [here](http://stackoverflow.com/questions/15285546/using-a-proxy-server-with-fopen) or get back to using cURL with proxy enabled, like [here](http://stackoverflow.com/questions/5211887/how-to-use-curl-via-a-proxy). – ahuemmer Jun 07 '15 at 12:34