VAT_old.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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_old {
  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. "Niepoprawny Numer Identyfikacji Podatkowej" => [
  85. "active" => false,
  86. "shortMessage" => "Niepoprawny NIP"
  87. ],
  88. ];
  89. private function getHeaders($ch, $header) {
  90. $len = strlen($header);
  91. $header = explode(':', $header, 2);
  92. if (count($header) < 2) return $len;
  93. $this->headers[trim($header[0])] = trim($header[1]);
  94. return $len;
  95. }
  96. private function checkHeaders() {
  97. if (!(isset($this->headers['Fast-Ver-Last']) && $this->headers['Fast-Ver-Last'])) throw new Exception("Błąd danych ze strony Ministerstwa Finansów");
  98. }
  99. public static function validateNIP($nip) {
  100. $prefix = substr($nip, 0, 2);
  101. if (!is_numeric($prefix)) $nip = substr($nip, 2);
  102. else $prefix = null;
  103. if ($prefix && $prefix != "PL") return false;
  104. if (strlen($nip) != 10 || (!is_numeric($nip))) return false;
  105. $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;
  106. return ($control == $nip[9]);
  107. }
  108. private static function paramsUrlEncode($params) {
  109. $array = [];
  110. foreach ($params as $param => $value) $array[] = $param . "=" . urlencode($value);
  111. return implode("&", $array);
  112. }
  113. private function execute() {
  114. $this->headers = [];
  115. $result = curl_exec($this->ch);
  116. if ($result === false) throw new Exception("Błąd połączenia ze stroną Ministerstwa Finansów");
  117. if (curl_getinfo($this->ch)['http_code'] != 200) throw new Exception("Błąd odbierania danych ze strony Ministerstwa Finansów");
  118. return $result;
  119. }
  120. private function initialize() {
  121. $this->ch = curl_init();
  122. # curl_setopt($this->ch, CURLOPT_URL, "http://www.finanse.mf.gov.pl/web/wp/pp/sprawdzanie-statusu-podmiotu-w-vat");
  123. curl_setopt($this->ch, CURLOPT_URL, "https://ppuslugi.mf.gov.pl/?link=NIP&;");
  124. curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
  125. curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
  126. curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
  127. curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
  128. curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 5);
  129. curl_setopt($this->ch, CURLOPT_TIMEOUT, 5);
  130. curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookies);
  131. curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookies);
  132. curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, "self::getHeaders");
  133. $this->execute();
  134. $this->checkHeaders();
  135. $params = [
  136. "Load" => "1",
  137. "FAST_VERLAST__" => $this->headers['Fast-Ver-Last'],
  138. ];
  139. curl_setopt($this->ch, CURLOPT_URL, "https://ppuslugi.mf.gov.pl/_/?" . self::paramsUrlEncode($params));
  140. $this->execute();
  141. curl_setopt($this->ch, CURLOPT_URL, "https://ppuslugi.mf.gov.pl/_/EventOccurred");
  142. $this->initialized = true;
  143. }
  144. private function reinitialize() {
  145. $this->checkHeaders();
  146. $params = [
  147. "DOC_MODAL_ID__" => "0",
  148. "EVENT__" => "b-9",
  149. "FAST_VERLAST__" => $this->headers['Fast-Ver-Last'],
  150. ];
  151. curl_setopt($this->ch, CURLOPT_POSTFIELDS, self::paramsUrlEncode($params));
  152. $this->execute();
  153. }
  154. private function getResult($result) {
  155. $search = ['/\<br(\s*)?\/?\>/i', '/[[:blank:]]{2,}/', '/[\x00-\x1F\x80-\xFF]/'];
  156. $replace = ['|', ' ', ''];
  157. $json = preg_replace($search, $replace, $result);
  158. $array = json_decode($json, true);
  159. if (!($array && isset($array['html']))) throw new Exception("Błąd danych ze strony Ministerstwa Finansów");
  160. $html = $array['html'];
  161. $this->dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
  162. $this->message = array_map("trim", array_values(array_diff(explode("|", $this->dom->getElementById("caption2_b-3")->textContent), [''])));
  163. if (!$this->message) {
  164. $this->message = array_map("trim", array_values(array_diff(explode("\n", $this->dom->getElementById("indicator_b-7")->getAttribute("title")), [''])));
  165. if ($this->message) {
  166. // throw new Exception($this->message[0]);
  167. }
  168. else throw new Exception("Błąd danych ze strony Ministerstwa Finansów");
  169. }
  170. if (isset($this->messages[$this->message[0]])) return $this->messages[$this->message[0]]['active'];
  171. else throw new Exception("Nieznany rezultat");
  172. }
  173. public function getMessage($whole = false) {
  174. if (!$this->message) $this->message = ["Brak wyniku"];
  175. if ($whole) return $this->message;
  176. else return $this->message[0];
  177. }
  178. public function getShortMessage() {
  179. if (!$this->message) return "Brak wyniku";
  180. if (!isset($this->messages[$this->message[0]])) return "Nieznany rezultat";
  181. return $this->messages[$this->message[0]]['shortMessage'];
  182. }
  183. public function isActiveVatPayer($nip) {
  184. $this->message = [];
  185. $nip = str_replace("-", "", trim($nip));
  186. if (!$this->validateNIP($nip)) {
  187. $this->message = ["Niepoprawny Numer Identyfikacji Podatkowej"];
  188. return false;
  189. // throw new Exception("Błędny NIP");
  190. }
  191. if (!$this->initialized) $this->initialize();
  192. else $this->reinitialize();
  193. $this->checkHeaders();
  194. $params = [
  195. "b-7" => $nip,
  196. "DOC_MODAL_ID__" => "0",
  197. "EVENT__" => "b-8",
  198. "FAST_VERLAST__" => $this->headers['Fast-Ver-Last'],
  199. ];
  200. curl_setopt($this->ch, CURLOPT_POSTFIELDS, self::paramsUrlEncode($params));
  201. return $this->getResult($this->execute());
  202. }
  203. function __construct() {
  204. $this->dom = new DOMDocument();
  205. }
  206. function __destruct() {
  207. if ($this->ch) curl_close($this->ch);
  208. }
  209. }