DEV Community

Alexander Nitrovich
Alexander Nitrovich

Posted on Originally published at blog.eurovalidate.com

Add EU VAT validation to WooCommerce

Add EU VAT Validation to WooCommerce

Integrating EU VAT validation into your WooCommerce store is crucial for compliance with European tax regulations. By validating VAT numbers in real-time, you can reduce compliance risk and enhance customer trust. This guide will walk you through integrating EU VAT validation into WooCommerce using EuroValidate API, a developer-first solution offering a straightforward and reliable method for managing EU VAT compliance.

Why You Need EU VAT Validation in WooCommerce

EU-based e-commerce businesses must adhere to VAT compliance, which involves correctly validating customers' VAT numbers. Real-time VAT validation helps ensure compliance with tax authorities, prevents fraud, and maintains accurate billing. Additionally, it enhances customer experience by preventing checkout errors related to invalid VAT numbers.

Introducing Our Developer-First VAT Validation API

Our API is designed with developers in mind, simplifying the integration process through comprehensive documentation and reliable response times. Key features include:

  • Ease of Integration: Quick setup with minimal changes to your existing WooCommerce setup.
  • Accurate Validation: Ensures that you only process valid VAT numbers, reducing compliance risk.
  • Enhanced User Experience: Real-time feedback for customers, improving checkout efficiency.

Explore the API documentation to get started.

Preparing for Integration

Before integrating, ensure you have:

  • API credentials from EuroValidate.
  • A WooCommerce store set up and running.
  • Appropriate server environment and version compatibility with WooCommerce.

Prepare your development environment to avoid disruptions on live sites.

Step-by-Step Guide to Integration

Step 1: Obtaining Your API Key and Setting Up

Sign up at EuroValidate to get your API key. This key is crucial for authenticating requests to the VAT validation service.

Step 2: Installing the Plugin or Custom Code

You can integrate VAT validation via a plugin or custom code snippet in your functions.php:

add_action('woocommerce_checkout_process', 'validate_eu_vat_number');
function validate_eu_vat_number() {
    if ( !empty( $_POST['vat_number'] ) ) {
        $vat_number = sanitize_text_field( $_POST['vat_number'] );

        // Prepare API endpoint and parameters
        $api_url = 'https://api.eurovalidate.com/v1/validate-vat';
        $api_key = 'YOUR_API_KEY';

        $response = wp_remote_post($api_url, array(
            'headers' => array(
                'Authorization' => 'Bearer ' . $api_key,
                'Content-Type'  => 'application/json'
            ),
            'body'    => json_encode( array('vat_number' => $vat_number) ),
            'timeout' => 15,
        ));

        if (is_wp_error($response)) {
            wc_add_notice( __( 'VAT validation service is currently unavailable. Please try again later.', 'your-text-domain' ), 'error' );
            return;
        }

        $result = json_decode( wp_remote_retrieve_body( $response ), true );

        if (empty($result['valid']) || $result['valid'] !== true) {
            wc_add_notice( __( 'The provided VAT number is invalid. Please check and try again.', 'your-text-domain' ), 'error' );
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Implementing VAT Validation in the Checkout Process

Use the WooCommerce checkout hook to intercept and validate the VAT number. This ensures you validate the number even before payment is processed.

Code Examples

PHP Example

// See step 2's code snippet above.
Enter fullscreen mode Exit fullscreen mode

Curl, Python, and Node.js Examples

Curl

curl -X POST "https://api.eurovalidate.com/v1/validate" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"vat_number": "NL820646660B01"}'
Enter fullscreen mode Exit fullscreen mode

Python

import requests

url = 'https://api.eurovalidate.com/v1/validate'
headers = { 'Authorization': 'Bearer YOUR_API_KEY', 'Content-Type': 'application/json' }
data = { 'vat_number': 'FR40303265045' }

response = requests.post(url, headers=headers, json=data)
print(response.json())
Enter fullscreen mode Exit fullscreen mode

Node.js

const axios = require('axios');

axios.post('https://api.eurovalidate.com/v1/validate', {
    vat_number: 'DE89370400440532013000'
}, {
    headers: {
        'Authorization': `Bearer YOUR_API_KEY`,
        'Content-Type': 'application/json'
    }
})
.then(response => {
    console.log(response.data);
})
.catch(error => {
    console.log(error);
});
Enter fullscreen mode Exit fullscreen mode

Testing Responses

  • Valid Response Example:
  {
    "vat_number": "NL820646660B01",
    "country_code": "NL",
    "status": "valid",
    "company_name": "Example BV",
    "company_address": "Street 123, City, NL",
    "request_id": "12345",
    "meta": {
      "confidence": 0.95,
      "source": "official_database",
      "cached": false,
      "response_time_ms": 120
    }
  }
Enter fullscreen mode Exit fullscreen mode
  • Invalid Response Example:
  {
    "vat_number": "FR40303265045",
    "country_code": "FR",
    "status": "invalid",
    "request_id": "67890",
    "meta": {
      "confidence": 0.99,
      "source": "official_database",
      "cached": false,
      "response_time_ms": 150
    }
  }
Enter fullscreen mode Exit fullscreen mode

Testing and Troubleshooting

Verify integration in a development environment. Common issues may include incorrect API keys or network errors. Ensure all values are correctly sanitized and validated before executing API requests.

Best Practices for Maintaining Compliance

Stay updated with EU VAT changes by regularly checking official sources and updating your API integration settings as needed. Periodically verify the API's response times to ensure optimal performance.

Conclusion and Additional Resources

Integrating VAT validation into WooCommerce using the EuroValidate API enhances compliance while simplifying management. Explore our API documentation for deeper insights and support.

Ready to simplify your VAT compliance? Get started with EuroValidate API today and subscribe to a plan that fits your needs!

Top comments (0)