−Table of Contents
VAT ID Validation using Vatlayer
Currently the Tax ID field on the checkout doesn't provide any validation that the value is correct - just that a value is entered. Native validation is planned as a future enhancement, but in the meantime - you can use a service like Vatlayer to validate the entered VAT ID using our pre-payment webhook.
Signup with Vatlayer
An account is required with Vatlayer for the validation to work, so you can signup from their website here. Vatlayer has a free plan that supports up to 100 requests per month, and then paid plans that support more.
Set up your pre-payment webhook
Next, you'll need to create an endpoint on your server that the checkout can send a validation request to when a customer attempts to complete the checkout. The following endpoint is written in PHP, but should be easily ported to other languages too:
<?php $rawPost = file_get_contents('php://input'); $cart_details = json_decode($rawPost, true); $response = array( 'ok' => true, 'details' => '' ); // Your Vatlayer Access Key $access_key = 'YOUR_ACCESS_KEY'; $vat_number = $cart_details['_embedded']['fx:customer']['tax_id']; if ($vat_number != "") { $ch = curl_init('http://apilayer.net/api/validate?access_key='.$access_key.'&vat_number='.$vat_number.''); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $json = curl_exec($ch); curl_close($ch); $validationResult = json_decode($json, true); if ($validationResult['valid'] == false) { $response['ok'] = false; $response['details'] = "Your VAT ID does not appear to be valid. Please review your VAT ID and try again."; } } header('Content-Type: application/json'); print json_encode($response);
Copy the above code to a file on your computer, and update the string YOUR_ACCESS_KEY
with the access key provided to you from Vatlayer. You may also want to edit the error that is returned to the customer if the validation does not pass, found towards the bottom of the script.
The validation fails if Vatlayer does not confirm that the VAT ID is valid. If you just want to check if the VAT ID is the right format, but not if it's valid, you can make the following change. Look for this line in the script:
if ($validationResult['valid'] == false) {
And update it to instead look like this:
if ($validationResult['format_valid'] == false) {
Once you've completed editing of the code, upload the file to your server in a publicly accessible location.
Enable the Pre-Payment Webhook
Once you have your endpoint uploaded, you then need to enable it in your store. Proceed to the “payments” section, and for each payment set it's required for, look for the “CUSTOM WEBHOOKS” section.
Within that, check the box for the “enable custom pre-payment hook” option, and enter the URL to the script you uploaded previously. It should be a full secured URL, beginning with https://
.
You can also set how the checkout should behave if your endpoint fails to load correctly - whether to approve or deny the transaction. For this set up, unless you specifically need to always know that the VAT is valid, you'll probably want to leave it as approve.
Test the integration
At this point - you will have basic VAT validation enabled on your checkout. You can test that it's working by attempting to place an order on your store, and entering an invalid VAT ID on the checkout. If your store is live, you should be ok to enter a fake credit card number (like 4242 4242 4242 4242
) as with an invalid VAT ID the gateway request won't ever happen because the pre-payment webhook happens before that point.