less than a minute read • Updated 9 minutes ago
Validate VAT IDs at checkout
How to validate customer VAT IDs at checkout using the Pre-Payment Webhook and 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-checkout 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-checkout 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
<?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:
php
if ($validationResult['valid'] == false) {And update it to instead look like this:
php
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-checkout webhook
Once your endpoint is uploaded, enable it in your store. In the Foxy admin (https://admin.foxy.io), go to Settings > Payments, open your payment method set, and open Fraud protection. Enable the Pre-checkout webhook and enter the URL to the script you uploaded earlier in the URL field. It should be a full secure 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-checkout webhook happens before that point.