Calculating European VAT Rates with PHP – the easy way.
Disclaimer: No legal advice, just a little experience report.
If you sell digital things (services, digital goods etc.) to EU countries, you might have to calculate the VAT at the rate of the customer’s country (and even pay it there afterwards).
The latter is even the more inconvenient part, but at least the first part can be done quite easily with good old PHP.
Originally released as a Laravel plugin (or whatever it’s called in their bubble..), there is this very handy library that does the whole complex calculation starting from the seller’s country:
Even more interesting for my purposes was this fork here:
Works on the one hand as a complete standalone version, and still includes updates and is actually also rather a further development than a fork :thuink:
With this then the VAT can be calculated in a few easy calls and life is exceptionally easy.
Not only the country of the customer is taken into account, but also the postal code, because in Germany alone there are several different VAT rates, depending on the island you happen to find yourself on.
// Easy to use! use Spaze\VatCalculator\VatCalculator; $vatRates = new VatRates(); $vatCalculator = new VatCalculator($vatRates); $vatCalculator->calculate(71.00, 'DE' /* $countryCode */, '41352' /* $postalCode or null */, true /* Whether the customer you're calculating the VAT for is a company */); $vatCalculator->getTaxRateForLocation('NL'); // Check validity of a VAT number $vatCalculator->isValidVatNumber('NL123456789B01');
With the appropriate VAT identification number, the annoyance of the collection is then also accordingly omitted.
So nice one, definitely saved me a lot of worries. Github-starred – Cheers guys!