<p>Had a look in the source code. There's a <a href="https://craftcms.com/docs/commerce/4.x/config-settings.html#validatebusinesstaxidasvatid" rel="nofollow noreferrer"><code>validateBusinessTaxIdAsVatId</code> config setting</a> that will validate tax IDs on addresses in Commerce. If this option is active, Commerce attaches a behavior (<a href="https://docs.craftcms.com/commerce/api/v4/craft-commerce-behaviors-validateorganizationtaxidbehavior.html" rel="nofollow noreferrer"><code>ValidateOrganizationTaxIdBehavior</code></a>) to the <code>Address</code> class. The validator uses the <a href="https://github.com/ibericode/vat" rel="nofollow noreferrer">library <code>ibericode/vat</code></a> to validate tax IDs (still on <a href="https://github.com/ibericode/vat/tree/1d24382454481384bbc987dcbc959cfef17d6baa" rel="nofollow noreferrer">version 1.x</a>, not the current 2.x branch, note that the public API has changed). You should be able to use the same code to validate tax IDs manually:</p>
<pre class="lang-php prettyprint-override"><code>use DvK\Vat\Validator;

$validator = new Validator();
$taxId = $order-&gt;billingAddress-&gt;businessTaxId;
if ($validator-&gt;validate($taxId)) {
    // tax ID was valid
}
</code></pre>

CC BY-SA 4.0
2024-01-05T00:56:17.110Z
This works for validating on the spot, but doesn't help if you want to know if the ID was valid at order completion date (which is unfortunately what I need). Sounds like there's no recorded flag or data for it on the order.
cherrykoda
CC BY-SA 4.0
2024-01-05T08:30:45.310Z
@cherrykoda Can a tax ID be valid at completion but not later? The library that is used by Commerce [only validates the format](https://github.com/ibericode/vat/blob/1d24382454481384bbc987dcbc959cfef17d6baa/src/Validator.php#L19-L48) as far as I can tell, so I don't see how that could change?
MoritzLost
CC BY-SA 4.0
2024-01-05T18:50:26.353Z
ah ok, I thought the VAT library it hit did more than just format validation. Taxes are weird, people's businesses are weird, it's all weird. lol.
cherrykoda
CC BY-SA 4.0
2024-01-06T14:54:45.277Z
@cherrykoda I don't think you can get farther than format validation, you would need some kind of backing service to determine whether a tax ID actually exists and corresponds to a real business/person … and do that for every country in which you operate. Not sure if that data is even publicly available in most countries …
MoritzLost
CC BY-SA 4.0
2024-01-08T09:41:39.460Z
Ive used public VAT ID checking, but you're correct. I wouldn't have been surprised if there was a public lookup API.
cherrykoda
CC BY-SA 4.0
2024-01-12T17:27:49.050Z
Hey Moritz - as a heads up the validator lib tests format AND if the number exists in VIES. 

I'm unsure if a company goes out of business if the id is removed from VIES (the eu official VAT ID lookup). But felt it's worth mentioning here what the validator does :)
cherrykoda
CC BY-SA 4.0
2024-01-13T15:44:38.997Z
@cherrykoda Interesting, thanks! So does it perform an API call or is the list embedded in the library? Curious why I didn't find anything about that in the source code ...
MoritzLost
CC BY-SA 4.0
2024-01-14T05:20:56.137Z
Looks like in https://github.com/ibericode/vat/blob/1.2.1/src/Vies/Client.php there's a defined wsdl file url (so must be SOAP) for the VIES lookup on the ec.europa.eu site (the official EU tool).
cherrykoda
CC BY-SA 4.0
2024-01-04T08:32:31.383Z
2024-01-04T14:04:48.113Z
2024-01-04T14:04:48.113Z
MoritzLost
<p>The only method I can think of currently is:</p>
<pre><code>    foreach ($orders as $order) {
        if($order-&gt;billingAddress-&gt;businessTaxId &amp;&amp; $order-&gt;totalTax == 0){
         ......
        }
    }
</code></pre>
<p>Not sure if it's the best way.</p>

CC BY-SA 4.0
2024-01-04T06:19:16.827Z
2024-01-04T06:19:16.827Z
cherrykoda
<p>Is there a way to check on an order if the Business Tax ID was valid? Craft validates this in the order process, but Im in need of being able to narrow down some logic to orders that have a valid value in this field, as opposed to just a populated one. Thanks!</p>
<p><em>Not quite what I need:</em></p>
<pre><code> foreach ($orders as $order) {
     if($order-&gt;billingAddress-&gt;businessTaxId){
         ......
     }
 }
</code></pre>

<plugin-craftcommerce><plugin-craftcommerce3><tax>
Valid Business Tax ID
# Valid Business Tax ID

(Asked by: username_0 on 2024-01-04)

Is there a way to check on an order if the Business Tax ID was valid? Craft validates this in the order process, but Im in need of being able to narrow down some logic to orders that have a valid value in this field, as opposed to just a populated one. Thanks!
Not quite what I need:
 foreach ($orders as $order) {
     if($order->billingAddress->businessTaxId){
         ......
     }
 }

**Tags:** plugin-craftcommerce, plugin-craftcommerce3, tax


---

## 2 answers

### Answer by username_1 on 2024-01-04. Score: 2 | Accepted answer

Had a look in the source code. There's a validateBusinessTaxIdAsVatId config setting that will validate tax IDs on addresses in Commerce. If this option is active, Commerce attaches a behavior (ValidateOrganizationTaxIdBehavior) to the Address class. The validator uses the library ibericode/vat to validate tax IDs (still on version 1.x, not the current 2.x branch, note that the public API has changed). You should be able to use the same code to validate tax IDs manually:
use DvK\Vat\Validator;

$validator = new Validator();
$taxId = $order->billingAddress->businessTaxId;
if ($validator->validate($taxId)) {
    // tax ID was valid
}

> **Comments:**
> **username_0**: This works for validating on the spot, but doesn't help if you want to know if the ID was valid at order completion date (which is unfortunately what I need). Sounds like there's no recorded flag or data for it on the order.  
> **username_1**: @username_0 Can a tax ID be valid at completion but not later? The library that is used by Commerce [only validates the format](https://github.com/ibericode/vat/blob/1d24382454481384bbc987dcbc959cfef17d6baa/src/Validator.php#L19-L48) as far as I can tell, so I don't see how that could change?  
> **username_0**: ah ok, I thought the VAT library it hit did more than just format validation. Taxes are weird, people's businesses are weird, it's all weird. lol.  
> **username_1**: @username_0 I don't think you can get farther than format validation, you would need some kind of backing service to determine whether a tax ID actually exists and corresponds to a real business/person … and do that for every country in which you operate. Not sure if that data is even publicly available in most countries …  
> **username_0**: Ive used public VAT ID checking, but you're correct. I wouldn't have been surprised if there was a public lookup API.  
> **username_0**: Hey Moritz - as a heads up the validator lib tests format AND if the number exists in VIES.   I'm unsure if a company goes out of business if the id is removed from VIES (the eu official VAT ID lookup). But felt it's worth mentioning here what the validator does :)  
> **username_1**: @username_0 Interesting, thanks! So does it perform an API call or is the list embedded in the library? Curious why I didn't find anything about that in the source code ...  
> **username_0**: Looks like in https://github.com/ibericode/vat/blob/1.2.1/src/Vies/Client.php there's a defined wsdl file url (so must be SOAP) for the VIES lookup on the ec.europa.eu site (the official EU tool).  

---

### Answer by username_0 on 2024-01-04. Score: 1

The only method I can think of currently is:
    foreach ($orders as $order) {
        if($order->billingAddress->businessTaxId && $order->totalTax == 0){
         ......
        }
    }

Not sure if it's the best way.


---

