VAT.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  3. *
  4. * Value Add Tax (VAT) library
  5. * Author: Mariusz Muszyński
  6. *
  7. * Public static methods:
  8. *
  9. * public bool VAT::validateNIP ( string $nip )
  10. * Tells whether the given string is a correct polish tax identification number (TIN/NIP)
  11. *
  12. * Parameters:
  13. *
  14. * nip
  15. * Polish tax identification number
  16. *
  17. * Public dynamic methods:
  18. *
  19. * public VAT::__construct ( void )
  20. * Creates a new VAT object.
  21. *
  22. * public bool VAT::isActiveVatPayer ( string $nip )
  23. * Tells whether the given NIP belongs to an active VAT payer
  24. *
  25. * Parameters:
  26. *
  27. * nip
  28. * Polish tax identification number
  29. *
  30. * public mixed VAT::getMessage ( [ bool $whole = false ] )
  31. * Returns last message after using VAT::isActiveVatPayer
  32. *
  33. * Parameters:
  34. *
  35. * whole
  36. * When TRUE, whole message is returned as an array
  37. * When FALSE, only main message is returned as a string
  38. *
  39. * HINT:
  40. * It's faster to create one object and do many executes of VAT::isActiveVatPayer method
  41. * than creating separate objects for each VAT::isActiveVatPayer execute.
  42. *
  43. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  44. *
  45. * An example of usage:
  46. *
  47. * $vat = new VAT();
  48. * $nip_array = ["5932268672", "6040018535"];
  49. * foreach ($nip_array as $nip)
  50. * {
  51. * echo $nip . " - ";
  52. * try
  53. * {
  54. * $result = $vat->isActiveVatPayer($nip);
  55. * if ($result) echo "is active VAT payer";
  56. * else echo "is not active VAT payer";
  57. * echo " (" . $vat->getMessage() . ")\n";
  58. * }
  59. * catch (Exception $e)
  60. * {
  61. * echo "An error occurred (" . $e->getMessage() . ")\n";
  62. * }
  63. * }
  64. *
  65. \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  66. Class VAT {
  67. private $ch, $dom, $headers;
  68. private $cookies = "php://memory";
  69. private $message = [];
  70. private $initialized = false;
  71. private $messages = [
  72. "Podmiot o podanym identyfikatorze podatkowym NIP jest zarejestrowany jako podatnik VAT czynny" => [
  73. "active" => true,
  74. "shortMessage" => "Czynny",
  75. ],
  76. "Podmiot o podanym identyfikatorze podatkowym NIP nie jest zarejestrowany jako podatnik VAT" => [
  77. "active" => false,
  78. "shortMessage" => "Niezarejestrowany",
  79. ],
  80. "Podmiot o podanym identyfikatorze podatkowym NIP jest zarejestrowany jako podatnik VAT zwolniony" => [
  81. "active" => true,
  82. "shortMessage" => "Zwolniony"
  83. ],
  84. ];
  85. private function getHeaders($ch, $header) {
  86. $len = strlen($header);
  87. $header = explode(':', $header, 2);
  88. if (count($header) < 2) return $len;
  89. $this->headers[trim($header[0])] = trim($header[1]);
  90. return $len;
  91. }
  92. private function checkHeaders() {
  93. if (!(isset($this->headers['Fast-Ver-Last']) && $this->headers['Fast-Ver-Last'])) throw new Exception("Błąd danych ze strony Ministerstwa Finansów");
  94. }
  95. public static function validateNIP($nip) {
  96. $prefix = substr($nip, 0, 2);
  97. if (!is_numeric($prefix)) $nip = substr($nip, 2);
  98. else $prefix = null;
  99. if ($prefix && $prefix != "PL") return false;
  100. if (strlen($nip) != 10 || (!is_numeric($nip))) return false;
  101. $control = (($nip[0] * 6 + $nip[1] * 5 + $nip[2] * 7 + $nip[3] * 2 + $nip[4] * 3 + $nip[5] * 4 + $nip[6] * 5 + $nip[7] * 6 + $nip[8] * 7) % 11 ) % 10;
  102. return ($control == $nip[9]);
  103. }
  104. private static function paramsUrlEncode($params) {
  105. $array = [];
  106. foreach ($params as $param => $value) $array[] = $param . "=" . urlencode($value);
  107. return implode("&", $array);
  108. }
  109. private function execute() {
  110. $this->headers = [];
  111. $result = curl_exec($this->ch);
  112. if ($result === false) throw new Exception("Błąd połączenia ze stroną Ministerstwa Finansów");
  113. if (curl_getinfo($this->ch)['http_code'] != 200) throw new Exception("Błąd odbierania danych ze strony Ministerstwa Finansów");
  114. return $result;
  115. }
  116. private function initialize() {
  117. $this->ch = curl_init();
  118. curl_setopt($this->ch, CURLOPT_URL, "http://www.finanse.mf.gov.pl/web/wp/pp/sprawdzanie-statusu-podmiotu-w-vat");
  119. curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
  120. curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
  121. curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
  122. curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
  123. curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 5);
  124. curl_setopt($this->ch, CURLOPT_TIMEOUT, 5);
  125. curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookies);
  126. curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookies);
  127. curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, "self::getHeaders");
  128. $this->execute();
  129. $this->checkHeaders();
  130. $params = [
  131. "Load" => "1",
  132. "FAST_VERLAST__" => $this->headers['Fast-Ver-Last'],
  133. ];
  134. curl_setopt($this->ch, CURLOPT_URL, "https://ppuslugi.mf.gov.pl/_/?" . self::paramsUrlEncode($params));
  135. $this->execute();
  136. curl_setopt($this->ch, CURLOPT_URL, "https://ppuslugi.mf.gov.pl/_/EventOccurred");
  137. $this->initialized = true;
  138. }
  139. private function reinitialize() {
  140. $this->checkHeaders();
  141. $params = [
  142. "DOC_MODAL_ID__" => "0",
  143. "EVENT__" => "b-9",
  144. "FAST_VERLAST__" => $this->headers['Fast-Ver-Last'],
  145. ];
  146. curl_setopt($this->ch, CURLOPT_POSTFIELDS, self::paramsUrlEncode($params));
  147. $this->execute();
  148. }
  149. private function getResult($result) {
  150. $search = ['/\<br(\s*)?\/?\>/i', '/[[:blank:]]{2,}/', '/[\x00-\x1F\x80-\xFF]/'];
  151. $replace = ['|', ' ', ''];
  152. $json = preg_replace($search, $replace, $result);
  153. $array = json_decode($json, true);
  154. if (!($array && isset($array['html']))) throw new Exception("Błąd danych ze strony Ministerstwa Finansów");
  155. $html = $array['html'];
  156. $this->dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
  157. $this->message = array_map("trim", array_values(array_diff(explode("|", $this->dom->getElementById("caption2_b-3")->textContent), [''])));
  158. if (!$this->message) {
  159. $this->message = array_map("trim", array_values(array_diff(explode("\n", $this->dom->getElementById("indicator_b-7")->getAttribute("title")), [''])));
  160. if ($this->message) throw new Exception($this->message[0]);
  161. else throw new Exception("Błąd danych ze strony Ministerstwa Finansów");
  162. }
  163. if (isset($this->messages[$this->message[0]])) return $this->messages[$this->message[0]]['active'];
  164. else throw new Exception("Nieznany rezultat");
  165. }
  166. public function getMessage($whole = false) {
  167. if (!$this->message) $this->message = ["Brak wyniku"];
  168. if ($whole) return $this->message;
  169. else return $this->message[0];
  170. }
  171. public function getShortMessage() {
  172. if (!$this->message) return "Brak wyniku";
  173. if (!isset($this->messages[$this->message[0]])) return "Nieznany rezultat";
  174. return $this->messages[$this->message[0]]['shortMessage'];
  175. }
  176. public function isActiveVatPayer($nip) {
  177. $this->message = [];
  178. $nip = str_replace("-", "", trim($nip));
  179. if (!$this->validateNIP($nip)) throw new Exception("Błędny NIP");
  180. if (!$this->initialized) $this->initialize();
  181. else $this->reinitialize();
  182. $this->checkHeaders();
  183. $params = [
  184. "b-7" => $nip,
  185. "DOC_MODAL_ID__" => "0",
  186. "EVENT__" => "b-8",
  187. "FAST_VERLAST__" => $this->headers['Fast-Ver-Last'],
  188. ];
  189. curl_setopt($this->ch, CURLOPT_POSTFIELDS, self::paramsUrlEncode($params));
  190. return $this->getResult($this->execute());
  191. }
  192. function __construct() {
  193. $this->dom = new DOMDocument();
  194. }
  195. function __destruct() {
  196. if ($this->ch) curl_close($this->ch);
  197. }
  198. }