/administrator/components/com_virtuemart2/classes/euvatcheck.class.php

https://github.com/shafiqissani/Jewelery-Ecommerce- · PHP · 155 lines · 82 code · 22 blank · 51 comment · 24 complexity · cf89593e3f51267539430c09e63c38b8 MD5 · raw file

  1. <?php
  2. /**
  3. * VAT ID Checker
  4. *
  5. * @link http://ec.europa.eu/taxation_customs/vies/faqvies.do
  6. * @link http://www.csvimproved.com/
  7. * @author RolandD Cyber Produksi
  8. */
  9. /**
  10. * Validates an EU VAT number against the VIES
  11. */
  12. class VmEUVatCheck {
  13. /** @var string the URL to the VIES */
  14. var $viesurl = 'http://ec.europa.eu/taxation_customs/vies/api/checkVatPort?wsdl';
  15. /** @var array contains the data to pass to the VIES */
  16. var $param = array('countryCode' => '', 'vatNumber' => '');
  17. /** @var array contains the data to pass to the VIES */
  18. var $validvatid = false;
  19. /** @var boolean whether or not the field should be processed */
  20. var $output = false;
  21. function VmEUVatCheck($uservatid) {
  22. global $vmLogger;
  23. /* Load the SOAP library */
  24. require_once('nusoap/nusoap.php');
  25. /* Check for proxy settings */
  26. if( trim( @VM_PROXY_URL ) != '') {
  27. if( !stristr(VM_PROXY_URL, 'http')) {
  28. $proxyURL['host'] = VM_PROXY_URL;
  29. $proxyURL['scheme'] = 'http';
  30. }
  31. else $proxyURL = parse_url(VM_PROXY_URL);
  32. }
  33. else $proxyURL = '';
  34. /* Use the proxy and initialise the client */
  35. if( !empty($proxyURL) ) {
  36. $vmLogger->debug( 'Setting up proxy: '.$proxyURL['host'].':'.VM_PROXY_PORT );
  37. /* Proxy without authentication */
  38. $this->client = new nusoap_client($this->viesurl, true, $proxyURL['host'], VM_PROXY_PORT);
  39. /* proxy with authentication */
  40. if( trim( @VM_PROXY_USER ) != '') {
  41. $vmLogger->debug( 'Using proxy authentication!' );
  42. $this->client = new nusoap_client($this->viesurl, true, $proxyURL['host'], VM_PROXY_PORT, VM_PROXY_USER, VM_PROXY_PASS);
  43. }
  44. }
  45. /* Do not use the proxy and initialise the client */
  46. else {
  47. $this->client = new nusoap_client($this->viesurl, true);
  48. }
  49. /* Check if there is no error on initialisation */
  50. $err = $this->client->getError();
  51. /* We have an error, return false since we can't check the VAT ID */
  52. if ($err) {
  53. return false;
  54. }
  55. else {
  56. /* See if we can use cURL */
  57. if (function_exists( 'curl_init' ) && function_exists( 'curl_exec' )) $this->client->setUseCurl(true);
  58. /* Set the parameters to pass to VIES */
  59. $countrycode = substr($uservatid, 0, 2);
  60. $vatnumber = substr($uservatid, 2);
  61. $param = array('countryCode' => $countrycode, 'vatNumber' => $vatnumber);
  62. /* Call the VIES to check the VAT ID */
  63. $this->client->call('checkVat', $param);
  64. /* Check if anything has gone wrong */
  65. if ($this->client->fault) {
  66. $vmLogger->debug( 'There was a problem with the VAT ID Check!' );
  67. return false;
  68. }
  69. else {
  70. /* See if we received an error */
  71. if ($this->client->getError()) {
  72. // There was an error, return false as we cannot check the VAT ID
  73. $vmLogger->debug( $this->client->getError() );
  74. return false;
  75. }
  76. /* We have a valid response, process the response */
  77. else {
  78. $vmLogger->debug( 'EU VAT ID Check completed.' );
  79. /* Strip all garbage before the actual XML content */
  80. $xmltxt = trim(substr($this->client->response, strpos ($this->client->response, '<?xml')));
  81. /* Create an XML parser */
  82. $this->parser = xml_parser_create();
  83. xml_set_object($this->parser,$this);
  84. xml_set_element_handler($this->parser,"startElement","endElement");
  85. xml_set_character_data_handler($this->parser, "characterData");
  86. xml_parse($this->parser, $xmltxt);
  87. xml_parser_free($this->parser);
  88. /* Data is processed, return the outcome */
  89. // return $this->validvatid;
  90. }
  91. }
  92. }
  93. }
  94. /**
  95. * What to do when the parser finds a start element
  96. *
  97. * @param $parser object
  98. * @param $element_name string name of the element found
  99. */
  100. function startElement($parser, $element_name, $attributes) {
  101. switch($element_name) {
  102. case "VALID" : $this->output = true;
  103. break;
  104. default:
  105. $this->output = false;
  106. break;
  107. }
  108. }
  109. /**
  110. * What to do when the parser finds a closing element
  111. *
  112. * @param $parser object
  113. * @param $element_name string name of the element found
  114. */
  115. function endElement($parser, $element_name) {
  116. }
  117. /**
  118. * What to do when the parser finds data in the element
  119. *
  120. * @param $parser object
  121. * @param $xml_data string data found in between tags
  122. */
  123. function characterData($parser, $xml_data) {
  124. if ($this->output) {
  125. if ($xml_data) {
  126. if ($xml_data == "false") $this->validvatid = false;
  127. else if ($xml_data == "true") $this->validvatid = true;
  128. }
  129. $this->output = false;
  130. }
  131. }
  132. }
  133. ?>