Skip to content

Instantly share code, notes, and snippets.

@dingo-d
Created October 20, 2016 07:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dingo-d/3238ff76c27a148622ebba3c60a2a47f to your computer and use it in GitHub Desktop.
Save dingo-d/3238ff76c27a148622ebba3c60a2a47f to your computer and use it in GitHub Desktop.
AJAX function for checking the VAT number based on country and VAT id
<?php
add_action( 'wp_ajax_simple_vies_check', 'simple_vies_check' );
add_action( 'wp_ajax_nopriv_simple_vies_check', 'simple_vies_check' );
if ( ! function_exists( 'simple_vies_check' ) ) {
/**
* AJAX VIES check function for WooCommerce checkout
*
* @since 1.0.0
*/
function simple_vies_check() {
if ( isset( $_POST['tax_id'] ) && isset( $_POST['billing_country'] ) ) { // Input var okay.
$tax_id = isset( $_POST['tax_id'] ) ? sanitize_text_field( wp_unslash( $_POST['tax_id'] ) ) : ''; // Input var okay.
$billing_country = isset( $_POST['billing_country'] ) ? sanitize_text_field( wp_unslash( $_POST['billing_country'] ) ) : ''; // Input var okay.
try {
$client = new SoapClient( 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl' );
$response = $client->checkVat(array(
'countryCode' => $billing_country,
'vatNumber' => $tax_id,
));
if ( true === $response->valid ) {
wp_die( 'vies ok' );
} else {
wp_die( 'vies null' );
}
} catch (Exception $e) {
$error = $e->getMessage();
$error_array = array(
'INVALID_INPUT' => 'The provided CountryCode is invalid or the VAT number is empty',
'SERVICE_UNAVAILABLE' => 'The SOAP service is unavailable, try again later',
'MS_UNAVAILABLE' => 'The Member State service is unavailable, try again later or with another Member State',
'TIMEOUT' => 'The Member State service could not be reach in time, try again later or with another Member State',
'SERVER_BUSY' => 'The service can\'t process your request. Try again latter',
);
if ( array_key_exists( $error , $error_array ) ) {
wp_die( wp_json_encode( $error_array[ $error ] ) );
}
}
}
}
}
@ya-well
Copy link

ya-well commented Jan 7, 2018

Doesn't seem to be working in Woo 3.2.6. Is this lookup routine still valid?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment