PHPackages                             mpociot/vat-calculator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mpociot/vat-calculator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mpociot/vat-calculator
======================

EU VAT calculation, the way it should be.

3.25.0(5mo ago)1.3k3.9M—2.5%103[1 issues](https://github.com/laravel/vat-calculator/issues)16MITPHPPHP ^7.3|~8.0.0|~8.1.0|~8.2.0|~8.3.0|~8.4.0|~8.5.0CI passing

Since Sep 1Pushed 5mo ago13 watchersCompare

[ Source](https://github.com/laravel/vat-calculator)[ Packagist](https://packagist.org/packages/mpociot/vat-calculator)[ GitHub Sponsors](https://github.com/sponsors/driesvints)[ Fund](https://www.paypal.com/paypalme/driesvints)[ RSS](/packages/mpociot-vat-calculator/feed)WikiDiscussions 3.x Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (67)Used By (16)

VatCalculator
=============

[](#vatcalculator)

[ ![Tests](https://github.com/laravel/vat-calculator/workflows/tests/badge.svg)](https://github.com/laravel/vat-calculator/actions)[ ![Coding Standards](https://github.com/laravel/vat-calculator/actions/workflows/coding-standards.yml/badge.svg)](https://github.com/laravel/vat-calculator/actions/workflows/coding-standards.yml)[ ![Latest Stable Version](https://camo.githubusercontent.com/7432d2a131171f9ef7f16c44f3ad0ef513c28ac7b455aef4d51fa8abe200d279/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d706f63696f742f7661742d63616c63756c61746f72)](https://packagist.org/packages/mpociot/vat-calculator)[ ![Total Downloads](https://camo.githubusercontent.com/d0b433ffdf4eca6c9a3bed5ea20380b32eb18a06d50bb57e52bfd3e2b09c0bcc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d706f63696f742f7661742d63616c63756c61746f72)](https://packagist.org/packages/mpociot/vat-calculator)Handle all the hard stuff related to EU MOSS tax/vat regulations, the way it should be. Integrates with **Laravel and Cashier** — or in a **standalone** PHP application. Originally created by [Marcel Pociot](https://pociot.dev).

```
// Easy to use!
VatCalculator::calculate(24.00, $countryCode = 'DE');
VatCalculator::calculate(24.00, $countryCode, $postalCode);
VatCalculator::calculate(71.00, 'DE', '41352', $isCompany = true);
VatCalculator::getTaxRateForLocation('NL');

// Check validity of a VAT number
VatCalculator::isValidVATNumber('NL123456789B01');
```

Warning

This package does not provide any promises for correctly calculated taxes. You are still responsible to making sure that any calculated tax is correct for your use case. If you're uncertain if a certain tax is correct or not, it's best that you talk to an accountant.

Requirements
------------

[](#requirements)

- PHP 7.3 or higher
- (optional) Laravel 6.0 or higher

Installation
------------

[](#installation)

Install the package with composer:

```
composer require mpociot/vat-calculator
```

### Standalone

[](#standalone)

You can also use this package without Laravel. Simply create a new instance of the VatCalculator and use it. All documentation examples use the Laravel Facade code, so make sure not to call the methods as if they were static methods.

```
use Mpociot\VatCalculator\VatCalculator;

$vatCalculator = new VatCalculator();
$vatCalculator->setBusinessCountryCode('DE');
$grossPrice = $vatCalculator->calculate(49.99, $countryCode = 'LU');
```

Upgrading
---------

[](#upgrading)

Please refer to [`the upgrade guide`](UPGRADE.md) when upgrading the library.

Usage
-----

[](#usage)

### Calculate the gross price

[](#calculate-the-gross-price)

To calculate the gross price use the `calculate` method with a net price and a country code as parameters.

```
$grossPrice = VatCalculator::calculate(24.00, 'DE');
```

The third parameter is the postal code of the customer.

As a fourth parameter, you can pass in a boolean indicating whether the customer is a company or a private person. If the customer is a company, which you should check by validating the VAT number, the net price gets returned.

```
$grossPrice = VatCalculator::calculate(24.00, 'DE', '12345', $isCompany = true);
```

### Receive more information

[](#receive-more-information)

After calculating the gross price you can extract more information from the VatCalculator.

```
$grossPrice = VatCalculator::calculate(24.00, 'DE'); // 28.56
$taxRate = VatCalculator::getTaxRate(); // 0.19
$netPrice = VatCalculator::getNetPrice(); // 24.00
$taxValue = VatCalculator::getTaxValue(); // 4.56
```

### Receive all tax rates for a given country

[](#receive-all-tax-rates-for-a-given-country)

To receive an array with all available tax rates for a given country, use the `getTaxRatesForCountry` method.

```
VatCalculator::getTaxRatesForCountry('DE'); // ["high" => 0.19, "low" => 0.07]
```

### Validate EU VAT numbers

[](#validate-eu-vat-numbers)

Prior to validating your customers VAT numbers, you can use the `shouldCollectVAT` method to check if the country code requires you to collect VAT in the first place.

```
if (VatCalculator::shouldCollectVAT('DE')) {
    // This country code requires VAT collection...
}
```

To validate your customers VAT numbers, you can use the `isValidVATNumber` method. The VAT number should be in a format specified by the [VIES](http://ec.europa.eu/taxation_customs/vies/faqvies.do#item_11). The given VAT numbers will be truncated and non relevant characters / whitespace will automatically be removed.

This service relies on a third party SOAP API provided by the EU. If, for whatever reason, this API is unavailable a `VATCheckUnavailableException` will be thrown.

```
try {
    $validVAT = VatCalculator::isValidVATNumber('NL 123456789 B01');
} catch (VATCheckUnavailableException $e) {
    // The VAT check API is unavailable...
}
```

Alternatively, it is also possible to validate only the format of the VAT Number specified by [VIES](http://ec.europa.eu/taxation_customs/vies/faqvies.do#item_11). This is useful, if you do not want to wait for a response from the SOAP API.

```
// This check will return false because no connection to VIES could be made...
$validVAT = VatCalculator::isValidVATNumber('NL 123456789 B01');

// This check will return true because only the format is checked...
$validVAT = VatCalculator::isValidVatNumberFormat('NL 123456789 B01');
```

### Get EU VAT number details

[](#get-eu-vat-number-details)

To get the details of a VAT number, you can use the `getVATDetails` method. The VAT number should be in a format specified by the [VIES](http://ec.europa.eu/taxation_customs/vies/faqvies.do#item_11). The given VAT numbers will be truncated and non relevant characters / whitespace will automatically be removed.

This service relies on a third party SOAP API provided by the EU. If, for whatever reason, this API is unavailable a `VATCheckUnavailableException` will be thrown.

```
try {
    $vat_details = VatCalculator::getVATDetails('NL 123456789 B01');
    print_r($vat_details);
    /* Outputs
    stdClass Object
    (
        [countryCode] => NL
        [vatNumber] => 123456789B01
        [requestDate] => 2017-04-06+02:00
        [valid] => false
        [name] => Name of the company
        [address] => Address of the company
    )
    */
} catch (VATCheckUnavailableException $e) {
    // The VAT check API is unavailable...
}
```

#### UK VAT Numbers

[](#uk-vat-numbers)

> Note: Validating UK VAT numbers requires registering your application with the HMRC Developer Hub. Please follow the official [HMRC API Documentation](https://developer.service.hmrc.gov.uk/api-documentation/docs/api/service/vat-registered-companies-api/2.0) for details on authentication and setup.

UK VAT numbers are formatted a little differently:

```
try {
    $vat_details = VatCalculator::getVATDetails('GB 553557881');
    print_r($vat_details);
    /* Outputs
    array(3) {
        ["name"]=>
            string(26) "Credite Sberger Donal Inc."
        ["vatNumber"]=>
            string(9) "553557881"
        ["address"]=>
            array(3) {
                ["line1"]=>
                    string(18) "131B Barton Hamlet"
                ["postcode"]=>
                    string(8) "SW97 5CK"
                ["countryCode"]=>
                    string(2) "GB"
            }
    }
    */
} catch (VATCheckUnavailableException $e) {
    // The VAT check API is unavailable...
}
```

> 🔐 Configuration

To use the UK VAT validation feature, you'll need to register your application with HMRC and set the following environment variables in your .env file:

```
HMRC_CLIENT_ID="your-client-id"
HMRC_CLIENT_SECRET="your-client-secret"

```

Laravel
-------

[](#laravel)

### Configuration

[](#configuration)

By default, the VatCalculator has all EU VAT rules predefined, so that it can easily be updated, if it changes for a specific country.

If you need to define other VAT rates, you can do so by publishing the configuration and add more rules.

> **Warning**
> Be sure to set your business country code in the configuration file, to get correct VAT calculation when selling to business customers in your own country.

To publish the configuration files, run the `vendor:publish` command

```
php artisan vendor:publish --provider="Mpociot\VatCalculator\VatCalculatorServiceProvider"
```

This will create a `vat_calculator.php` in your config directory.

#### Handling SOAP Faults

[](#handling-soap-faults)

If for some reason, SOAP faults happen when the VIES API is faulty, these errors will be handled gracefully and `false` will be returned. However, if you explicitly want to be aware of any SOAP faults you may instruct VatCalculator to throw them as a `VATCheckUnavailableException`. The `VATCheckUnavailableException` will then contain the specific message of the SOAP fault.

Set the option to `true` in your config file:

```
