Last active
October 3, 2022 21:32
-
-
Save sirtimid/5a714fab7754475aed66905381ef2ef9 to your computer and use it in GitHub Desktop.
VIES VAT custom validation for Wordpress Ultimate Members field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This is a custom validation for Ultimate Members Wordpress plugin | |
* Apply custom validation to VAT field with the name vat_number | |
* It requires also a country field | |
* This expects a user to enter a VAT Number in the following format: | |
* EL-123456789 | |
* | |
* Add um_custom_vat_validation on Custom Action field | |
* | |
* @author sirtimid.com | |
*/ | |
if(!function_exists('um_custom_vat_validation')){ | |
function um_custom_vat_validation( $args ) { | |
global $ultimatemember; | |
$eu_countries = array( | |
'Austria','Belgium','Bulgaria','Croatia','Cyprus','Czech Republic','Denmark', | |
'Estonia','Finland','France','Germany','Greece','Hungary','Ireland','Italy', | |
'Latvia','Lithuania','Luxembourg','Malta','Netherlands','Poland','Portugal', | |
'Romania','Slovakia','Slovenia','Spain','Sweden','United Kingdom' | |
); | |
// check if vat_number is set and the country is in Europe | |
if ( isset($args['vat_number']) && isset($args['country']) && in_array($args['country'], $eu_countries) ) { | |
$vat = explode('-', $args['vat_number']); | |
$vies = check_VIES_VAT($countryCode, $vatNumber); | |
if(!$vies['valid']){ | |
$ultimatemember->form->add_error( 'vat_number', 'Your VAT Number is not valid.' ); | |
} | |
} | |
} | |
add_action('um_submit_form_errors_hook_','um_custom_vat_validation', 999, 1); | |
} | |
/** | |
* VIES VAT number validation | |
* Based on http://stackoverflow.com/a/29955950 | |
* | |
* @author Eugen Mihailescu | |
* | |
* @param string $countryCode | |
* @param string $vatNumber | |
* @param int $timeout | |
* | |
* @return array | |
* | |
* Array | |
* ( | |
* [countryCode] => EL | |
* [vatNumber] => 111111111 | |
* [requestDate] => 2017-01-01+00:00 | |
* [valid] => true | |
* [name] => JOHN DOE | |
* [address] => STREET No POSTAL_CODE - CITY | |
* ) | |
*/ | |
if(!function_exists('check_VIES_VAT')){ | |
function check_VIES_VAT($countryCode, $vatNumber, $timeout = 30) { | |
$response = array (); | |
$pattern = '/<(%s).*?>([\s\S]*)<\/\1/'; | |
$keys = array ( | |
'countryCode', | |
'vatNumber', | |
'requestDate', | |
'valid', | |
'name', | |
'address' | |
); | |
$content = "<s11:Envelope xmlns:s11='http://schemas.xmlsoap.org/soap/envelope/'> | |
<s11:Body> | |
<tns1:checkVat xmlns:tns1='urn:ec.europa.eu:taxud:vies:services:checkVat:types'> | |
<tns1:countryCode>%s</tns1:countryCode> | |
<tns1:vatNumber>%s</tns1:vatNumber> | |
</tns1:checkVat> | |
</s11:Body> | |
</s11:Envelope>"; | |
$opts = array ( | |
'http' => array ( | |
'method' => 'POST', | |
'header' => "Content-Type: text/xml; charset=utf-8; SOAPAction: checkVatService", | |
'content' => sprintf ( $content, $countryCode, $vatNumber ), | |
'timeout' => $timeout | |
) | |
); | |
$ctx = stream_context_create($opts); | |
$result = file_get_contents('http://ec.europa.eu/taxation_customs/vies/services/checkVatService', false, $ctx ); | |
if(preg_match ( sprintf ( $pattern, 'checkVatResponse' ), $result, $matches )) { | |
foreach ($keys as $key){ | |
preg_match ( sprintf ( $pattern, $key ), $matches [2], $value ) && $response [$key] = $value [2]; | |
} | |
} | |
return $response; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment