Created
April 27, 2019 14:53
-
-
Save wpfullstripe/e79f0e20e81a95f90a88e0f355e1a7f3 to your computer and use it in GitHub Desktop.
This code can be used as a template for international businesses to create a custom VAT filter for WP Full Stripe. The example code takes into account the supplier’s and the buyer’s country to determine the VAT rate for e-goods.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Determine the EU VAT rate based on country code | |
* | |
* @param string $country 2-letter ISO country code in uppercase | |
* @return float The VAT rate (percent) for the country | |
*/ | |
function lookup_vat_rate_by_country($country) { | |
switch ($country) { | |
case 'LU': | |
$vatPercent = 17.00; | |
break; | |
case 'MT': | |
$vatPercent = 18.00; | |
break; | |
case 'CY': | |
case 'DE': | |
$vatPercent = 19.00; | |
break; | |
case 'GB': | |
case 'AT': | |
case 'EE': | |
case 'FR': | |
case 'RO': | |
case 'SK': | |
case 'SE': | |
$vatPercent = 20.00; | |
break; | |
case 'BE': | |
case 'BG': | |
case 'CZ': | |
case 'ES': | |
case 'NL': | |
case 'LT': | |
case 'LV': | |
$vatPercent = 21.00; | |
break; | |
case 'IT': | |
case 'PT': | |
case 'SI': | |
$vatPercent = 22.00; | |
break; | |
case 'IE': | |
case 'PL': | |
$vatPercent = 23.00; | |
break; | |
case 'FI': | |
case 'GR': | |
$vatPercent = 24.00; | |
break; | |
case 'HR': | |
case 'DK': | |
$vatPercent = 25.00; | |
break; | |
case 'HU': | |
$vatPercent = 27.00; | |
break; | |
default: | |
$vatPercent = 0; | |
break; | |
} | |
return $vatPercent; | |
} | |
/** | |
* Determine the VAT rate based on default country, billing address, and custom fields | |
* | |
* @param float $initialValue Currently not used, always 0. | |
* @param string $fromCountry The default billing country, also the supplier's billing country | |
* @param string $toCountry The country of the billing address | |
* @param array $additionalArguments A lot of additional info about the form, see below: | |
* The associative array has the following indexes: | |
* form_id | |
* form_type | |
* plan_id | |
* plan_currency | |
* plan_amount | |
* plan_setup_fee | |
* billing_address | |
* custom_inputs | |
* @return float The VAT rate, returned as a percent [0.0 .. 100.0] | |
*/ | |
function determine_custom_vat_percent($initialValue, $fromCountry, $toCountry, $additionalArguments) { | |
$euCountries = array('GB', 'LU', 'MT', 'CY', 'DE', 'RO', 'AT', 'EE', 'FR', 'SK', 'SE', 'BE', 'BG', 'CZ', 'ES', 'NL', | |
'LT', 'LV', 'IT', 'PT', 'SI', 'IE', 'PL', 'FI', 'GR', 'HR', 'DK', 'HU'); | |
$customInputValues = array_key_exists('custom_inputs', $additionalArguments) ? | |
$additionalArguments['custom_inputs'] : null; | |
$b2bCustomer = isset($customInputValues) && is_array($customInputValues) && | |
(!empty($customInputValues['Company Name']) || !empty($customInputValues['VAT ID'])); | |
if (in_array($fromCountry, $euCountries)) { | |
//-- Supplier is in the EU | |
if (in_array($toCountry, $euCountries)) { | |
// EU countries | |
$domestic = $fromCountry === $toCountry; | |
if ($b2bCustomer && !$domestic) { | |
//-- B2B customer in EU | |
$vatPercent = 0; | |
} else { | |
// B2C customer or domestic B2B in EU | |
$vatPercent = lookup_vat_rate_by_country($toCountry); | |
} | |
} else { | |
// Countries outside EU | |
// B2B and B2C customers outside EU | |
$vatPercent = 0; | |
} | |
} else { | |
//-- Supplier is not in the EU | |
if (in_array($toCountry, $euCountries)) { | |
if ($b2bCustomer) { | |
//-- B2B customer in EU | |
$vatPercent = 0; | |
} else { | |
//-- B2C customer in EU | |
$vatPercent = lookup_vat_rate_by_country($toCountry); | |
} | |
} else { | |
//-- Outside EU, both B2B and B2C are 0% | |
$vatPercent = 0; | |
} | |
} | |
error_log("VAT percent: $vatPercent, from country: $fromCountry, to country: $toCountry"); | |
return $vatPercent; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment