VatCalculator

repository·3.x·Indexed 23 days ago

https://github.com/laravel/vat-calculator

A 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).

Tokens
3.5K
Snippets
13
Records
24
Agent score
79%

What's inside vat-calculator

  1. Upgrade from v1 to v2: Use getTaxRateForLocation

    3.x

    When upgrading from v1 to v2, it is recommended to switch from getTaxRateForCountry to the more precise getTaxRateForLocation method.

    getTaxRateForLocation accepts three arguments:

    1. country code: The country code of the customer.
    2. postal code: The postal code of the customer.
    3. company: A boolean flag indicating if the customer is a company.
  2. Integrate with Laravel Cashier Stripe

    3.x

    To use VatCalculator with Laravel Cashier Stripe, use the BillableWithinTheEU trait on your billable model. This trait overrides the taxPercentage method from the standard Billable trait.

    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 taxPercentage method 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'];
    }
  3. Re-add non-EU countries in v3

    3.x

    In 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.php file.

    return [
        'rules' => [
             'GB' => [
                 'rate' => 0.20,
                 'exceptions' => [
                     'Akrotiri' => 0.19,
                     'Dhekelia' => 0.19,
                 ],
             ],
             'TR' => [
                 'rate' => 0.18,
             ],
             'NO' => [
                 'rate' => 0.25,
             ],
        ],
    ];
  4. Use the new ValidVatNumber rule in v3

    3.x

    The 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 the Mpociot\VatCalculator\Rules\ValidVatNumber class.

    // After v3 upgrade...
    use Mpociot\VatCalculator\Rules\ValidVatNumber;
    
    $validator = Validator::make(
        ['vat_number' => $vatNumber],
        ['vat_number' => ['required', new ValidVatNumber]]
    );
  5. Use VatCalculator in a standalone PHP application

    3.x

    If you are not using Laravel, you must instantiate the VatCalculator class 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');
  6. Configure UK VAT validation

    3.x

    To use UK VAT validation, you must register your application with the HMRC Developer Hub and add the following environment variables to your .env file:

    HMRC_CLIENT_ID="your-client-id"
    HMRC_CLIENT_SECRET="your-client-secret"
  7. Configure VatCalculator in Laravel

    3.x

    To 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): If true, SOAP faults from the VIES API will be thrown as VATCheckUnavailableException instead of returning false.
    • 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.

  8. Register and configure VatCalculator in Laravel

    3.x

    The VatCalculatorServiceProvider automatically 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 config directory using the following Artisan command:

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

    This will create a config/vat_calculator.php file.

    Dependency Injection

    You can resolve the VatCalculator instance from the Laravel Service Container using either the class name or the string alias vatcalculator.

  9. Get EU VAT number details

    3.x

    Use getVATDetails($vatNumber) to retrieve detailed information about a VAT number from the VIES service. This returns an stdClass object containing the country code, VAT number, request date, validity status, company name, and address.

    Note: This method throws VATCheckUnavailableException if 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...
    }
  10. Use ValidVatNumber Laravel validation rule

    3.x

    The package provides a ValidVatNumber rule for use in Laravel Form Requests or manual validation.

    Note: This rule returns false if 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],
    ]);