Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.9 kB
1
Indexable
Never
// check vat
	try {
		$client = new SoapClient("http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl");

		$country_code = substr($vat_number_inputted, 0, 2); // take first 2 chars
		$vat_number = substr($vat_number_inputted, 2); // remove first 2 chars

		$validation = $client->checkVat(array(
		  'countryCode' => $country_code,
		  'vatNumber' => $vat_number
		));
		$error_details = '';

		// check country is same as VAT country
		if (trim(strtolower($country_inputted)) !== trim(strtolower($country_code))){
			// check exception Greece (GR) has EL VAT code
			if( (trim(strtolower($country_inputted)) === 'gr') && (trim(strtolower($country_code)) === 'el')){
				// if indeed the VAT number is EL and country is GR, do nothing
			} else {
				$validation->valid=0;
			}
		}

	} catch (Exception $e) {
		$error = $e->getMessage();
		$error_array = array(
		    'INVALID_INPUT'       => esc_html__('CountryCode is invalid or the VAT number is empty', 'b2bking'),
		    'SERVICE_UNAVAILABLE' => esc_html__('VIES VAT Service is unavailable. Try again later.', 'b2bking'),
		    'MS_UNAVAILABLE'      => esc_html__('VIES VAT Member State Service is unavailable.', 'b2bking'),
		    'TIMEOUT'             => esc_html__('Service timeout. Try again later.', 'b2bking'),
		    'SERVER_BUSY'         => esc_html__('VAT Server is too busy. Try again later.', 'b2bking'),
		    'MS_MAX_CONCURRENT_REQ' => esc_html__('Too many requests. The VIES server is too busy. Try again later.', 'b2bking'),
		);

		if ( array_key_exists( $error , $error_array ) ) {
		    $error_details .= $error_array[ $error ];
		} else {
			$error_details .= $error;
		}

		$validation->valid=0;

		// if error is independent of the user (unavailable service, timeout, etc), allow it, but notify the website admin
		if (apply_filters('b2bking_allow_vat_timeouts_unavailable_errors', true)){
			if ($error !== 'INVALID_INPUT'){ // except the invalid format error
				$validation = new stdClass();
				$validation -> valid = 1;

				// mail the website admin about the issue and that this number needs to be checked
				$recipient = get_option( 'admin_email' );
				$recipient = apply_filters('b2bking_invalid_vat_number_email', $recipient, 0);

			    $message = 'A customer registered or ordered on your shop, but the VIES validation encountered an issue which is not the user\'s fault. The request was accepted, but you should manually check this VAT number and customer.';
			    $message .= '<br><br>Error details: '.$error;
			    if ( array_key_exists( $error , $error_array ) ) {
			        $message .= ' ('.$error_array[ $error ].')';
			    }
			    $message .= '<br><br>The VAT number is: '.$country_code.$vat_number;
			    $message .= '<br><br>The email of the user is: '.$email;

			    do_action( 'b2bking_new_message', $recipient, $message, 'Quoteemail:1', 0 );
				
			}
		}
	}
} else {
	$validation->valid=0;
}
Leave a Comment