VatCalculator
repository·3.x·Indexed 23 days ago
https://github.com/laravel/vat-calculatorA PHP library for handling EU MOSS tax/VAT regulations. It provides tools to calculate gross and net prices, validate EU VAT numbers via VIES and HMRC, and retrieve tax rates for specific countries. The library integrates with Laravel via a facade and service provider, offers a ValidVatNumber validation rule, and includes a BillableWithinTheEU trait for Laravel Cashier Stripe (compatible with versions below v13).
What's inside vat-calculator
- When upgrading to v3, ensure your environment meets the new minimum requirements. Support for PHP 7.2 and below, as well as Laravel 5.8 and below, has been dropped.
Upgrade from v1 to v2: Use getTaxRateForLocation
3.xWhen upgrading from v1 to v2, it is recommended to switch from
getTaxRateForCountryto the more precisegetTaxRateForLocationmethod.getTaxRateForLocationaccepts three arguments:country code: The country code of the customer.postal code: The postal code of the customer.company: A boolean flag indicating if the customer is a company.
Integrate with Laravel Cashier Stripe
3.xTo use
VatCalculatorwith Laravel Cashier Stripe, use theBillableWithinTheEUtrait on your billable model. This trait overrides thetaxPercentagemethod from the standardBillabletrait.Implementation:
use Laravel\Cashier\Billable; use Mpociot\VatCalculator\Traits\BillableWithinTheEU; use Laravel\Cashier\Contracts\Billable as BillableContract; class User extends Model implements BillableContract { use Billable, BillableWithinTheEU { BillableWithinTheEU::taxPercentage insteadof Billable; } }Usage Workflow: Use the chainable methods to set the tax context before creating a subscription:
useTaxFrom($countryCode): Sets the tax rate for the given country.asIndividual(): Sets the customer as a private person (default).asBusiness(): Sets the customer as a company.
$user = User::find(1); // For individuals $user->useTaxFrom('NL'); // For business customers $user->useTaxFrom('NL')->asBusiness(); $user->subscription('monthly')->create($creditCardToken);Compatibility Warning: This package is currently incompatible with Cashier Stripe v13 or higher because it relies on the
taxPercentagemethod which was removed in v13.use Laravel\Cashier\Billable; use Mpociot\VatCalculator\Traits\BillableWithinTheEU; use Laravel\Cashier\Contracts\Billable as BillableContract; class User extends Model implements BillableContract { use Billable, BillableWithinTheEU { BillableWithinTheEU::taxPercentage insteadof Billable; } protected $dates = ['trial_ends_at', 'subscription_ends_at']; }Install VatCalculator via Composer
3.xInstall the package using Composer to add it to your PHP project:
composer require mpociot/vat-calculatorRequirements:
- PHP 7.3 or higher
- (Optional) Laravel 6.0 or higher
Re-add non-EU countries in v3
3.xIn v3, VAT calculations for GB (United Kingdom), NO (Norway), and TR (Turkey) were removed because they are not EU members. If your application requires support for these countries, you must manually re-add them to your
config.phpfile.return [ 'rules' => [ 'GB' => [ 'rate' => 0.20, 'exceptions' => [ 'Akrotiri' => 0.19, 'Dhekelia' => 0.19, ], ], 'TR' => [ 'rate' => 0.18, ], 'NO' => [ 'rate' => 0.25, ], ], ];Use the new ValidVatNumber rule in v3
3.xThe internal validation rule for VAT numbers has been refactored into a dedicated rule object. In v3, instead of using the string
'vat_number'in your Laravel validation array, you must instantiate theMpociot\VatCalculator\Rules\ValidVatNumberclass.// After v3 upgrade... use Mpociot\VatCalculator\Rules\ValidVatNumber; $validator = Validator::make( ['vat_number' => $vatNumber], ['vat_number' => ['required', new ValidVatNumber]] );Use VatCalculator in a standalone PHP application
3.xIf you are not using Laravel, you must instantiate the
VatCalculatorclass manually. Do not call methods statically as you would with the Laravel Facade.use Mpociot\ VatCalculator vatCalculator = new VatCalculator(); $vatCalculator->setBusinessCountryCode('DE'); $grossPrice = $vatCalculator->calculate(49.99, 'LU');use Mpociot\VatCalculator\VatCalculator; $vatCalculator = new VatCalculator(); $vatCalculator->setBusinessCountryCode('DE'); $grossPrice = $vatCalculator->calculate(49.99, 'LU');Configure UK VAT validation
3.xTo use UK VAT validation, you must register your application with the HMRC Developer Hub and add the following environment variables to your
.envfile:HMRC_CLIENT_ID="your-client-id" HMRC_CLIENT_SECRET="your-client-secret"Configure VatCalculator in Laravel
3.xTo customize VAT rates or settings in Laravel, publish the configuration file:
php artisan vendor:publish --provider="Mpociot\VatCalculator\VatCalculatorServiceProvider"This creates
config/vat_calculator.php. Key options include:forward_soap_faults(bool): Iftrue, SOAP faults from the VIES API will be thrown asVATCheckUnavailableExceptioninstead of returningfalse.soap_timeout(int): The timeout in seconds for the SOAP client (default is 30).
Warning: Ensure you set your business country code in the config to ensure correct calculations when selling to business customers in your own country.
Register and configure VatCalculator in Laravel
3.xThe
VatCalculatorServiceProviderautomatically handles the integration of the package into your Laravel application.Configuration
To customize the package settings, you can publish the configuration file to your application's
configdirectory using the following Artisan command:php artisan vendor:publish --provider="Mpociot\VatCalculator\VatCalculatorServiceProvider"This will create a
config/vat_calculator.phpfile.Dependency Injection
You can resolve the
VatCalculatorinstance from the Laravel Service Container using either the class name or the string aliasvatcalculator.Get EU VAT number details
3.xUse
getVATDetails($vatNumber)to retrieve detailed information about a VAT number from the VIES service. This returns anstdClassobject containing the country code, VAT number, request date, validity status, company name, and address.Note: This method throws
VATCheckUnavailableExceptionif the VIES API is unavailable.try { $vat_details = VatCalculator::getVATDetails('NL 123456789 B01'); // $vat_details->name, $vat_details->address, etc. } catch (VATCheckUnavailableException $e) { // Handle API unavailability }try { $vat_details = VatCalculator::getVATDetails('NL 123456789 B01'); print_r($vat_details); } catch (VATCheckUnavailableException $e) { // The VAT check API is unavailable... }Use ValidVatNumber Laravel validation rule
3.xThe package provides a
ValidVatNumberrule for use in Laravel Form Requests or manual validation.Note: This rule returns
falseif the VAT ID Check SOAP API is unavailable.use Mpociot\VatCalculator\Rules\ValidVatNumber; $validator = Validator::make($input, [ 'company_vat' => ['required', new ValidVatNumber], ]);use Mpociot\VatCalculator\Rules\ValidVatNumber; $validator = Validator::make(Input::all(), [ 'first_name' => 'required', 'last_name' => 'required', 'company_vat' => ['required', new ValidVatNumber], ]);