Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Vat Class Reference

Public Member Functions

 __construct (ScopeConfigInterface $scopeConfig, PsrLogger $logger)
 
 getMerchantCountryCode ($store=null)
 
 getMerchantVatNumber ($store=null)
 
 getCustomerGroupIdBasedOnVatNumber ($customerCountryCode, $vatValidationResult, $store=null)
 
 checkVatNumber ($countryCode, $vatNumber, $requesterCountryCode='', $requesterVatNumber='')
 
 canCheckVatNumber ($countryCode, $vatNumber, $requesterCountryCode, $requesterVatNumber)
 
 getCustomerVatClass ($customerCountryCode, $vatValidationResult, $store=null)
 
 isCountryInEU ($countryCode, $storeId=null)
 

Data Fields

const XML_PATH_CUSTOMER_VIV_INTRA_UNION_GROUP = 'customer/create_account/viv_intra_union_group'
 
const XML_PATH_CUSTOMER_VIV_DOMESTIC_GROUP = 'customer/create_account/viv_domestic_group'
 
const XML_PATH_CUSTOMER_VIV_INVALID_GROUP = 'customer/create_account/viv_invalid_group'
 
const XML_PATH_CUSTOMER_VIV_ERROR_GROUP = 'customer/create_account/viv_error_group'
 
const VAT_CLASS_DOMESTIC = 'domestic'
 
const VAT_CLASS_INTRA_UNION = 'intra_union'
 
const VAT_CLASS_INVALID = 'invalid'
 
const VAT_CLASS_ERROR = 'error'
 
const VAT_VALIDATION_WSDL_URL = 'http://ec.europa.eu/taxation_customs/vies/services/checkVatService?wsdl'
 
const XML_PATH_CUSTOMER_GROUP_AUTO_ASSIGN = 'customer/create_account/auto_group_assign'
 
const XML_PATH_EU_COUNTRIES_LIST = 'general/country/eu_countries'
 

Protected Member Functions

 createVatNumberValidationSoapClient ($trace=false)
 

Protected Attributes

 $scopeConfig
 
 $logger
 

Detailed Description

Customer VAT model

Definition at line 19 of file Vat.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( ScopeConfigInterface  $scopeConfig,
PsrLogger  $logger 
)
Parameters
ScopeConfigInterface$scopeConfig
PsrLogger$logger

Definition at line 73 of file Vat.php.

76  {
77  $this->scopeConfig = $scopeConfig;
78  $this->logger = $logger;
79  }

Member Function Documentation

◆ canCheckVatNumber()

canCheckVatNumber (   $countryCode,
  $vatNumber,
  $requesterCountryCode,
  $requesterVatNumber 
)

Check if parameters are valid to send to VAT validation service

Parameters
string$countryCode
string$vatNumber
string$requesterCountryCode
string$requesterVatNumber
Returns
boolean

@SuppressWarnings(PHPMD.CyclomaticComplexity)

Definition at line 241 of file Vat.php.

242  {
243  return !(!is_string($countryCode)
244  || !is_string($vatNumber)
245  || !is_string($requesterCountryCode)
246  || !is_string($requesterVatNumber)
247  || empty($countryCode)
248  || !$this->isCountryInEU($countryCode)
249  || empty($vatNumber)
250  || empty($requesterCountryCode) && !empty($requesterVatNumber)
251  || !empty($requesterCountryCode) && empty($requesterVatNumber)
252  || !empty($requesterCountryCode) && !$this->isCountryInEU($requesterCountryCode)
253  );
254  }
isCountryInEU($countryCode, $storeId=null)
Definition: Vat.php:296

◆ checkVatNumber()

checkVatNumber (   $countryCode,
  $vatNumber,
  $requesterCountryCode = '',
  $requesterVatNumber = '' 
)

Send request to VAT validation service and return validation result

Parameters
string$countryCode
string$vatNumber
string$requesterCountryCode
string$requesterVatNumber
Returns
DataObject

Definition at line 162 of file Vat.php.

163  {
164  // Default response
165  $gatewayResponse = new DataObject([
166  'is_valid' => false,
167  'request_date' => '',
168  'request_identifier' => '',
169  'request_success' => false,
170  'request_message' => __('Error during VAT Number verification.'),
171  ]);
172 
173  if (!extension_loaded('soap')) {
174  $this->logger->critical(new LocalizedException(__('PHP SOAP extension is required.')));
175  return $gatewayResponse;
176  }
177 
178  if (!$this->canCheckVatNumber($countryCode, $vatNumber, $requesterCountryCode, $requesterVatNumber)) {
179  return $gatewayResponse;
180  }
181 
182  try {
183  $soapClient = $this->createVatNumberValidationSoapClient();
184 
185  $requestParams = [];
186  $requestParams['countryCode'] = $countryCode;
187  $vatNumberSanitized = $this->isCountryInEU($countryCode)
188  ? str_replace([' ', '-', $countryCode], ['', '', ''], $vatNumber)
189  : str_replace([' ', '-'], ['', ''], $vatNumber);
190  $requestParams['vatNumber'] = $vatNumberSanitized;
191  $requestParams['requesterCountryCode'] = $requesterCountryCode;
192  $reqVatNumSanitized = $this->isCountryInEU($requesterCountryCode)
193  ? str_replace([' ', '-', $requesterCountryCode], ['', '', ''], $requesterVatNumber)
194  : str_replace([' ', '-'], ['', ''], $requesterVatNumber);
195  $requestParams['requesterVatNumber'] = $reqVatNumSanitized;
196  // Send request to service
197  $result = $soapClient->checkVatApprox($requestParams);
198 
199  $gatewayResponse->setIsValid((bool)$result->valid);
200  $gatewayResponse->setRequestDate((string)$result->requestDate);
201  $gatewayResponse->setRequestIdentifier((string)$result->requestIdentifier);
202  $gatewayResponse->setRequestSuccess(true);
203 
204  if ($gatewayResponse->getIsValid()) {
205  $gatewayResponse->setRequestMessage(__('VAT Number is valid.'));
206  } else {
207  $gatewayResponse->setRequestMessage(__('Please enter a valid VAT number.'));
208  }
209  } catch (\Exception $exception) {
210  $gatewayResponse->setIsValid(false);
211  $gatewayResponse->setRequestDate('');
212  $gatewayResponse->setRequestIdentifier('');
213  }
214 
215  return $gatewayResponse;
216  }
createVatNumberValidationSoapClient($trace=false)
Definition: Vat.php:224
canCheckVatNumber($countryCode, $vatNumber, $requesterCountryCode, $requesterVatNumber)
Definition: Vat.php:241
__()
Definition: __.php:13
isCountryInEU($countryCode, $storeId=null)
Definition: Vat.php:296

◆ createVatNumberValidationSoapClient()

createVatNumberValidationSoapClient (   $trace = false)
protected

Create SOAP client based on VAT validation service WSDL

Parameters
boolean$trace
Returns
\SoapClient

Definition at line 224 of file Vat.php.

225  {
226  return new \SoapClient(self::VAT_VALIDATION_WSDL_URL, ['trace' => $trace]);
227  }

◆ getCustomerGroupIdBasedOnVatNumber()

getCustomerGroupIdBasedOnVatNumber (   $customerCountryCode,
  $vatValidationResult,
  $store = null 
)

Retrieve customer group ID based on his VAT number

Parameters
string$customerCountryCode
DataObject$vatValidationResult
\Magento\Store\Model\Store | string | int$store
Returns
null|int

Definition at line 119 of file Vat.php.

120  {
121  $groupId = null;
122 
123  $isAutoGroupAssign = $this->scopeConfig->isSetFlag(
124  self::XML_PATH_CUSTOMER_GROUP_AUTO_ASSIGN,
125  ScopeInterface::SCOPE_STORE,
126  $store
127  );
128  if (!$isAutoGroupAssign) {
129  return $groupId;
130  }
131 
132  $vatClass = $this->getCustomerVatClass($customerCountryCode, $vatValidationResult, $store);
133 
134  $vatClassToGroupXmlPathMap = [
135  self::VAT_CLASS_DOMESTIC => self::XML_PATH_CUSTOMER_VIV_DOMESTIC_GROUP,
136  self::VAT_CLASS_INTRA_UNION => self::XML_PATH_CUSTOMER_VIV_INTRA_UNION_GROUP,
137  self::VAT_CLASS_INVALID => self::XML_PATH_CUSTOMER_VIV_INVALID_GROUP,
138  self::VAT_CLASS_ERROR => self::XML_PATH_CUSTOMER_VIV_ERROR_GROUP,
139  ];
140 
141  if (isset($vatClassToGroupXmlPathMap[$vatClass])) {
142  $groupId = (int)$this->scopeConfig->getValue(
143  $vatClassToGroupXmlPathMap[$vatClass],
144  ScopeInterface::SCOPE_STORE,
145  $store
146  );
147  }
148 
149  return $groupId;
150  }
getCustomerVatClass($customerCountryCode, $vatValidationResult, $store=null)
Definition: Vat.php:264
const XML_PATH_CUSTOMER_VIV_DOMESTIC_GROUP
Definition: Vat.php:26
const XML_PATH_CUSTOMER_VIV_INTRA_UNION_GROUP
Definition: Vat.php:24
const XML_PATH_CUSTOMER_VIV_ERROR_GROUP
Definition: Vat.php:30
const XML_PATH_CUSTOMER_VIV_INVALID_GROUP
Definition: Vat.php:28

◆ getCustomerVatClass()

getCustomerVatClass (   $customerCountryCode,
  $vatValidationResult,
  $store = null 
)

Get VAT class

Parameters
string$customerCountryCode
DataObject$vatValidationResult
Store | string | int | null$store
Returns
null|string

Definition at line 264 of file Vat.php.

265  {
266  $vatClass = null;
267 
268  $isVatNumberValid = $vatValidationResult->getIsValid();
269 
270  if (is_string($customerCountryCode)
271  && !empty($customerCountryCode)
272  && $customerCountryCode === $this->getMerchantCountryCode($store)
273  && $isVatNumberValid
274  ) {
275  $vatClass = self::VAT_CLASS_DOMESTIC;
276  } elseif ($isVatNumberValid) {
277  $vatClass = self::VAT_CLASS_INTRA_UNION;
278  } else {
279  $vatClass = self::VAT_CLASS_INVALID;
280  }
281 
282  if (!$vatValidationResult->getRequestSuccess()) {
283  $vatClass = self::VAT_CLASS_ERROR;
284  }
285 
286  return $vatClass;
287  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
const VAT_CLASS_INTRA_UNION
Definition: Vat.php:37
getMerchantCountryCode($store=null)
Definition: Vat.php:87

◆ getMerchantCountryCode()

getMerchantCountryCode (   $store = null)

Retrieve merchant country code

Parameters
Store | string | int | null$store
Returns
string

Definition at line 87 of file Vat.php.

88  {
89  return (string)$this->scopeConfig->getValue(
90  StoreInformation::XML_PATH_STORE_INFO_COUNTRY_CODE,
91  ScopeInterface::SCOPE_STORE,
92  $store
93  );
94  }

◆ getMerchantVatNumber()

getMerchantVatNumber (   $store = null)

Retrieve merchant VAT number

Parameters
Store | string | int | null$store
Returns
string

Definition at line 102 of file Vat.php.

103  {
104  return (string)$this->scopeConfig->getValue(
105  StoreInformation::XML_PATH_STORE_INFO_VAT_NUMBER,
106  ScopeInterface::SCOPE_STORE,
107  $store
108  );
109  }

◆ isCountryInEU()

isCountryInEU (   $countryCode,
  $storeId = null 
)

Check whether specified country is in EU countries list

Parameters
string$countryCode
null | int$storeId
Returns
bool

Definition at line 296 of file Vat.php.

297  {
298  $euCountries = explode(
299  ',',
300  $this->scopeConfig->getValue(self::XML_PATH_EU_COUNTRIES_LIST, ScopeInterface::SCOPE_STORE, $storeId)
301  );
302  return in_array($countryCode, $euCountries);
303  }

Field Documentation

◆ $logger

$logger
protected

Definition at line 67 of file Vat.php.

◆ $scopeConfig

$scopeConfig
protected

Definition at line 62 of file Vat.php.

◆ VAT_CLASS_DOMESTIC

const VAT_CLASS_DOMESTIC = 'domestic'

VAT class constants

Definition at line 35 of file Vat.php.

◆ VAT_CLASS_ERROR

const VAT_CLASS_ERROR = 'error'

Definition at line 41 of file Vat.php.

◆ VAT_CLASS_INTRA_UNION

const VAT_CLASS_INTRA_UNION = 'intra_union'

Definition at line 37 of file Vat.php.

◆ VAT_CLASS_INVALID

const VAT_CLASS_INVALID = 'invalid'

Definition at line 39 of file Vat.php.

◆ VAT_VALIDATION_WSDL_URL

const VAT_VALIDATION_WSDL_URL = 'http://ec.europa.eu/taxation_customs/vies/services/checkVatService?wsdl'

WSDL of VAT validation service

Definition at line 47 of file Vat.php.

◆ XML_PATH_CUSTOMER_GROUP_AUTO_ASSIGN

const XML_PATH_CUSTOMER_GROUP_AUTO_ASSIGN = 'customer/create_account/auto_group_assign'

Config path to option that enables/disables automatic group assignment based on VAT

Definition at line 52 of file Vat.php.

◆ XML_PATH_CUSTOMER_VIV_DOMESTIC_GROUP

const XML_PATH_CUSTOMER_VIV_DOMESTIC_GROUP = 'customer/create_account/viv_domestic_group'

Definition at line 26 of file Vat.php.

◆ XML_PATH_CUSTOMER_VIV_ERROR_GROUP

const XML_PATH_CUSTOMER_VIV_ERROR_GROUP = 'customer/create_account/viv_error_group'

Definition at line 30 of file Vat.php.

◆ XML_PATH_CUSTOMER_VIV_INTRA_UNION_GROUP

const XML_PATH_CUSTOMER_VIV_INTRA_UNION_GROUP = 'customer/create_account/viv_intra_union_group'

Config paths to VAT related customer groups

Definition at line 24 of file Vat.php.

◆ XML_PATH_CUSTOMER_VIV_INVALID_GROUP

const XML_PATH_CUSTOMER_VIV_INVALID_GROUP = 'customer/create_account/viv_invalid_group'

Definition at line 28 of file Vat.php.

◆ XML_PATH_EU_COUNTRIES_LIST

const XML_PATH_EU_COUNTRIES_LIST = 'general/country/eu_countries'

Config path to UE country list

Definition at line 57 of file Vat.php.


The documentation for this class was generated from the following file: