OXID eShop CE  4.10.7
 All Classes Namespaces Files Functions Variables Pages
oxonlinevatidcheck.php
Go to the documentation of this file.
1 <?php
2 
7 {
8 
14  protected $_blServiceIsOn = null;
15 
21  protected static $_aVatCheckCache = array();
22 
27  const BUSY_RETRY_CNT = 1;
28 
33  const BUSY_RETRY_WAITUSEC = 500000;
34 
40  protected $_sWsdl = 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl';
41 
45  public function __construct()
46  {
47  }
48 
56  public function validate(oxCompanyVatIn $oVatIn)
57  {
58  $oCheckVat = new stdClass();
59  $oCheckVat->countryCode = $oVatIn->getCountryCode();
60  $oCheckVat->vatNumber = $oVatIn->getNumbers();
61 
62  $blResult = $this->_checkOnline($oCheckVat);
63  if (!$blResult) {
64  $this->setError('ID_NOT_VALID');
65  }
66 
67  return $blResult;
68  }
69 
79  protected function _parseError($sErrorMsg)
80  {
81  if (!$sErrorMsg || $sErrorMsg == 'INVALID_INPUT') {
83  $oEx = oxNew('oxInputException');
84  $oEx->setMessage('VAT_MESSAGE_' . ($sErrorMsg ? $sErrorMsg : 'ID_NOT_VALID'));
85  throw $oEx;
86  }
87 
89  $oEx = oxNew('oxConnectionException');
90  $oEx->setAdress($this->getWsdlUrl());
91  $oEx->setMessage('VAT_MESSAGE_' . $sErrorMsg);
92  $oEx->debugOut();
93  throw $oEx;
94  }
95 
107  public function checkUid($sCompVatId)
108  {
109  if (isset(self::$_aVatCheckCache[$sCompVatId])) {
110  if (true === self::$_aVatCheckCache[$sCompVatId]) {
111  return true;
112  }
113  if (is_string(self::$_aVatCheckCache[$sCompVatId])) {
114  $this->_parseError(self::$_aVatCheckCache[$sCompVatId]);
115  }
116 
117  return false;
118  }
119 
120  $oCheckVat = new stdClass();
121  $oCheckVat->countryCode = substr($sCompVatId, 0, 2);
122  $oCheckVat->vatNumber = substr($sCompVatId, 2);
123 
124  if (!$this->_checkOnline($oCheckVat)) {
125  $sErrorMsg = $this->_getError();
126  self::$_aVatCheckCache[$sCompVatId] = $sErrorMsg;
127  $this->_parseError($sErrorMsg);
128  }
129 
130  return self::$_aVatCheckCache[$sCompVatId] = true;
131  }
132 
144  public function catchWarning($iErrNo, $sErrStr, $sErrFile, $iErrLine)
145  {
146  // message to write to exception log
147  $sLogMessage = "Warning: $sErrStr in $sErrFile on line $iErrLine";
148 
149  // fetching exception log file name
150  $oEx = oxNew("oxException");
151  $sLogFileName = $oEx->getLogFileName();
152 
153  // logs error message
154  return oxRegistry::getUtils()->writeToLog($sLogMessage, $sLogFileName);
155  }
156 
165  protected function _isServiceAvailable()
166  {
167  if ($this->_blServiceIsOn === null) {
168  $this->_blServiceIsOn = class_exists('SoapClient') ? true : false;
169  if ($this->_blServiceIsOn) {
170  $rFp = @fopen($this->getWsdlUrl(), 'r');
171  $this->_blServiceIsOn = $rFp !== false;
172  if ($this->_blServiceIsOn) {
173  $sWsdl = '';
174  while (!feof($rFp)) {
175  $sWsdl .= fread($rFp, 8192);
176  }
177  fclose($rFp);
178 
179  // validating wsdl file
180  try {
181  $oDomDocument = new DOMDocument();
182  $oDomDocument->loadXML($sWsdl);
183  } catch (Exception $oExcp) {
184  // invalid xml
185  $this->_blServiceIsOn = false;
186  }
187  }
188  }
189  }
190 
191  return $this->_blServiceIsOn;
192  }
193 
202  protected function _checkOnline($oCheckVat)
203  {
204  if ($this->_isServiceAvailable()) {
205  $iTryMoreCnt = self::BUSY_RETRY_CNT;
206 
207  //T2009-07-02
208  //how long socket should wait for server RESPONSE
209  ini_set('default_socket_timeout', 5);
210 
211  // setting local error handler to catch possible soap errors
212  set_error_handler(array($this, 'catchWarning'), E_WARNING);
213 
214  do {
215  try {
216  //connection_timeout = how long we should wait to CONNECT to wsdl server
217  $oSoapClient = new SoapClient($this->getWsdlUrl(), array("connection_timeout" => 5));
218  $this->setError('');
219  $oRes = $oSoapClient->checkVat($oCheckVat);
220  $iTryMoreCnt = 0;
221  } catch (SoapFault $e) {
222  $this->setError($e->faultstring);
223  if ($this->getError() == "SERVER_BUSY") {
224  usleep(self::BUSY_RETRY_WAITUSEC);
225  } else {
226  $iTryMoreCnt = 0;
227  }
228  }
229  } while (0 < $iTryMoreCnt--);
230 
231  // restoring previous error handler
232  restore_error_handler();
233 
234  return (bool) $oRes->valid;
235  } else {
236  $this->setError("SERVICE_UNREACHABLE");
237 
238  return false;
239  }
240  }
241 
249  protected function _getError()
250  {
251  $sError = '';
252  if ($this->_sError) {
253  $sRegex = '/\{ \'([A-Z_]*)\' \}/';
254  $n = preg_match($sRegex, $this->_sError, $aMatches);
255  $sError = ($aMatches[1]) ? $aMatches[1] : 'SERVICE_UNAVAILABLE';
256  }
257 
258  return $sError;
259  }
260 
266  public function getWsdlUrl()
267  {
268  // overriding wsdl url
269  if (($sWsdl = oxRegistry::getConfig()->getConfigParam("sVatIdCheckInterfaceWsdl"))) {
270  $this->_sWsdl = $sWsdl;
271  }
272 
273  return $this->_sWsdl;
274  }
275 
283  public function isDisabled()
284  {
285  return (bool) oxRegistry::getConfig()->getConfigParam("blVatIdCheckDisabled");
286  }
287 }