Forums

Checking VAT number

InternEd 30 Apr, 2012
For a order form I want to check is someone has filled in a correct VAT number. I found a site and some code to check this. The site returns a true of a false.

The code I have written, never gives back a 'false' but I can't find out why. Can someome see what is wrong with it? The landcode is the first 2 characters of the filled in 'vatnumber'.

<?php
         $vatnumber = $form->data['vatnumber'];
         $land = substr($vatnumber, 0, 2);
         $btwnummer = substr($vatnumber, 2);

         $result = file_get_contents("http://isvat.appspot.com/".$land."/".$btwnummer."/", "r");
         if($result == "false") {
         $form->validation_errors['vatnumber'] = "Sorry, the VATnumber is invalid. Please try again.";
      return false;
    }
?>



Is there BTW a way to 'debug' in Chronoforms? So that I can see what values the variables get during the process?
GreyHead 30 Apr, 2012
Hi InternEd,

The code looks OK to me. Here's a slightly modified version that seems to work OK on the 'real' UK and French VAT numbers I've been able to find:
<?php
$vatnumber = $form->data['vatnumber'];
$vatnumber = trim($vatnumber);
$vatnumber = str_replace(array(' ', '-', '//'), '', $vatnumber);
$land = substr($vatnumber, 0, 2);
$btwnummer = substr($vatnumber, 2);
$url = "http://isvat.appspot.com/".$land."/".$btwnummer."/";
// $mainframe =& JFactory::getApplication();
// $mainframe->enqueuemessage('$url: '.print_r($url, true).'<hr />');
$result = file_get_contents($url, "r");
// $mainframe->enqueuemessage('$result: '.print_r($result, true).'<hr />');
if ( $result == "false" ) {
  $form->validation_errors['vatnumber'] = "Sorry, the VATnumber is invalid. Please try again.";
  return false;
}
?>

This has some extra code to clean the string and remove spaces, dashes and slashes before sending.

There's also some debug code there commented out as an example. You can also drag a Debugger action into the On Submit event to get more if you need it.

Bob
InternEd 01 May, 2012
Bob, Thanks for the code!

Strange thing is, that no matter what I fill in in the VATnumber field on the form, it always validates.

Any idea what that can be?

Regards, Ed.
GreyHead 01 May, 2012
Hi InterEd,

You don't have an Event Loop action in the On Fail event to re-load the form???

Bob
InternEd 01 May, 2012
Hi,

Yes I do. In the validation check there are already 2 other checks on submit.

Those 2 work, but when I add this one, it doesn't work.
To test, I did remove the other 2, to leave only this one in, but same result.

Thinking of that, does the field have to be set to 'required' to be able to do validation?

Ed.
GreyHead 02 May, 2012
Hi InterEd,

Please copy and paste all the validation code here so I can take a look.

Bob
InternEd 03 May, 2012
Hi,

I deleted one of the two existing validations checks. That one wasn't necessary after all.

The code in the validation item now is this:

<?php
    $email = $form->data['email'];
    if ( !$email ) {
      return true;
    }
    $email = explode('@', $email);
    $domain = explode('.', $email[1]);
    $domain = $domain[0];
    if ( in_array( $domain, array('gmail', 'live', 'hotmail', 'aol', 'yahoo', 'zonnet', 'planet')) ) {
      $form->validation_errors['email'] = "Sorry, the used email domain is invalid. Please use your corporate email address.";
      return false;
    }
?>

<?php
    $vatnumber = $form->data['vatnumber'];
    $vatnumber = trim($vatnumber);
    $vatnumber = str_replace(array(' ', '-', '//'), '', $vatnumber);
    $land = substr($vatnumber, 0, 2);
    $btwnummer = substr($vatnumber, 2);
    $url = "http://isvat.appspot.com/".$land."/".$btwnummer."/";
    // $mainframe =& JFactory::getApplication();
    // $mainframe->enqueuemessage('$url: '.print_r($url, true).'<hr />');
    $result = file_get_contents($url, "r");
    // $mainframe->enqueuemessage('$result: '.print_r($result, true).'<hr />');
    if ( $result == "false" ) {
      $form->validation_errors['vatnumber'] = "Sorry, the VATnumber is invalid. Please try again.";
      return false;
    }
?>

<?php
/** 
 * dynamic subject met vaste tekst
 */

$company = $form->data['company']; 
$subject = "License Request from $company";
$form->data['subject'] = $subject;

?>




The first check (on the email address) works fine.

Thanks for checking!

Ed.
GreyHead 04 May, 2012
Hi InternEd,

I think that the problem is that the return true; in the first part of the code is ending the validation before the VAT check. Here's an amended version of that part of the code which uses $isvalid to allow you to run multiple validation checks before returning to the form:
<?php

$isvalid = true;

$email = $form->data['email'];
if ( $email ) {
  $email = explode('@', $email);
  $domain = explode('.', $email[1]);
  $domain = $domain[0];
  if ( in_array( $domain, array('gmail', 'live', 'hotmail', 'aol', 'yahoo', 'zonnet', 'planet')) ) {
    $form->validation_errors['email'] = "Sorry, the used email domain is invalid. Please use your corporate email address.";
    $isvalid = false;
  }
}

$vatnumber = $form->data['vatnumber'];
$vatnumber = trim($vatnumber);
$vatnumber = str_replace(array(' ', '-', '//'), '', $vatnumber);
$land = substr($vatnumber, 0, 2);
$btwnummer = substr($vatnumber, 2);
$url = "http://isvat.appspot.com/".$land."/".$btwnummer."/";
$mainframe =& JFactory::getApplication();
$mainframe->enqueuemessage('$url: '.print_r($url, true).'<hr />');
$result = file_get_contents($url, "r");
$mainframe->enqueuemessage('$result: '.print_r($result, true).'<hr />');
if ( $result == "false" ) {
  $form->validation_errors['vatnumber'] = "Sorry, the VAT number is invalid. Please try again.";
  $isvalid = false;
}
if ( !$isvalid ) {
  return false;
}
?>

Bob
InternEd 04 May, 2012
Bob, I don't know what happens, but I still get strange results.

When I apply a good email address, it still doesn't matter if I fill in a wrong or a right VAT number. The form just validates.

When I apply a wrong email address and a good VAT number, I get a message on the email address and at the top of the form I see this:



When I apply a wrong email address and a wrong VAT number, I get a message on the email address and at the top of the form I see this:

GreyHead 04 May, 2012
Hi InterEd,

Hmmm it looks as though you aren't connecting to the appspot.com site. If I try the same Vat Number I get this:[attachment=0]04-05-2012 14-35-37.png[/attachment]

What happens if you just type the url into your browser (or click this link)?
http://isvat.appspot.com/nl/095999462B01/

Bob
InternEd 04 May, 2012
If I do it in my browser, I do get a true (or a false if I try a wrong number)

Hmmmmmm, maybe it's a dns problem on the server this website runs on.
GreyHead 04 May, 2012
Hi InternEd,

There's a note in the PHP Manual for file_get_contents() - not sure if it helps:

A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.


Bob
InternEd 04 May, 2012
Bob,

I copied the form to another website that runs by another provider, and there it works!
So I guess it indeed is a DNS problem of some sort. I will contact that provider.
InternEd 04 May, 2012
When I check the php info on both sites, I do see a difference in this parameter: allow_url_fopen
On one site it is set to ON on the 'problemsite' it is set to OFF.
So I guess that when I ask the provider if they can change this parameter, it will work.
InternEd 08 May, 2012
Bob,

The issue is solved. It turned out that the redirection in the code was blocked by the host of the website. They opened the ip-adresses in the firewall and now it works.

Thanks for the support. Let me buy you a beer 😀

Ed
GreyHead 12 May, 2012
Hi InternEd,

Great - good to hear that you got it working :-)

Bob
This topic is locked and no more replies can be posted.