isActiveVatPayer($nip); * if ($result) echo "is active VAT payer"; * else echo "is not active VAT payer"; * echo " (" . $vat->getMessage() . ")\n"; * } * catch (Exception $e) * { * echo "An error occurred (" . $e->getMessage() . ")\n"; * } * } * \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ Class VAT_old { private $ch, $dom, $headers; private $cookies = "php://memory"; private $message = []; private $initialized = false; private $messages = [ "Podmiot o podanym identyfikatorze podatkowym NIP jest zarejestrowany jako podatnik VAT czynny" => [ "active" => true, "shortMessage" => "Czynny", ], "Podmiot o podanym identyfikatorze podatkowym NIP nie jest zarejestrowany jako podatnik VAT" => [ "active" => false, "shortMessage" => "Niezarejestrowany", ], "Podmiot o podanym identyfikatorze podatkowym NIP jest zarejestrowany jako podatnik VAT zwolniony" => [ "active" => true, "shortMessage" => "Zwolniony" ], "Niepoprawny Numer Identyfikacji Podatkowej" => [ "active" => false, "shortMessage" => "Niepoprawny NIP" ], ]; private function getHeaders($ch, $header) { $len = strlen($header); $header = explode(':', $header, 2); if (count($header) < 2) return $len; $this->headers[trim($header[0])] = trim($header[1]); return $len; } private function checkHeaders() { if (!(isset($this->headers['Fast-Ver-Last']) && $this->headers['Fast-Ver-Last'])) throw new Exception("Błąd danych ze strony Ministerstwa Finansów"); } public static function validateNIP($nip) { $prefix = substr($nip, 0, 2); if (!is_numeric($prefix)) $nip = substr($nip, 2); else $prefix = null; if ($prefix && $prefix != "PL") return false; if (strlen($nip) != 10 || (!is_numeric($nip))) return false; $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; return ($control == $nip[9]); } private static function paramsUrlEncode($params) { $array = []; foreach ($params as $param => $value) $array[] = $param . "=" . urlencode($value); return implode("&", $array); } private function execute() { $this->headers = []; $result = curl_exec($this->ch); if ($result === false) throw new Exception("Błąd połączenia ze stroną Ministerstwa Finansów"); if (curl_getinfo($this->ch)['http_code'] != 200) throw new Exception("Błąd odbierania danych ze strony Ministerstwa Finansów"); return $result; } private function initialize() { $this->ch = curl_init(); # curl_setopt($this->ch, CURLOPT_URL, "http://www.finanse.mf.gov.pl/web/wp/pp/sprawdzanie-statusu-podmiotu-w-vat"); curl_setopt($this->ch, CURLOPT_URL, "https://ppuslugi.mf.gov.pl/?link=NIP&;"); curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($this->ch, CURLOPT_TIMEOUT, 5); curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookies); curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookies); curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, "self::getHeaders"); $this->execute(); $this->checkHeaders(); $params = [ "Load" => "1", "FAST_VERLAST__" => $this->headers['Fast-Ver-Last'], ]; curl_setopt($this->ch, CURLOPT_URL, "https://ppuslugi.mf.gov.pl/_/?" . self::paramsUrlEncode($params)); $this->execute(); curl_setopt($this->ch, CURLOPT_URL, "https://ppuslugi.mf.gov.pl/_/EventOccurred"); $this->initialized = true; } private function reinitialize() { $this->checkHeaders(); $params = [ "DOC_MODAL_ID__" => "0", "EVENT__" => "b-9", "FAST_VERLAST__" => $this->headers['Fast-Ver-Last'], ]; curl_setopt($this->ch, CURLOPT_POSTFIELDS, self::paramsUrlEncode($params)); $this->execute(); } private function getResult($result) { $search = ['/\/i', '/[[:blank:]]{2,}/', '/[\x00-\x1F\x80-\xFF]/']; $replace = ['|', ' ', '']; $json = preg_replace($search, $replace, $result); $array = json_decode($json, true); if (!($array && isset($array['html']))) throw new Exception("Błąd danych ze strony Ministerstwa Finansów"); $html = $array['html']; $this->dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8')); $this->message = array_map("trim", array_values(array_diff(explode("|", $this->dom->getElementById("caption2_b-3")->textContent), ['']))); if (!$this->message) { $this->message = array_map("trim", array_values(array_diff(explode("\n", $this->dom->getElementById("indicator_b-7")->getAttribute("title")), ['']))); if ($this->message) { // throw new Exception($this->message[0]); } else throw new Exception("Błąd danych ze strony Ministerstwa Finansów"); } if (isset($this->messages[$this->message[0]])) return $this->messages[$this->message[0]]['active']; else throw new Exception("Nieznany rezultat"); } public function getMessage($whole = false) { if (!$this->message) $this->message = ["Brak wyniku"]; if ($whole) return $this->message; else return $this->message[0]; } public function getShortMessage() { if (!$this->message) return "Brak wyniku"; if (!isset($this->messages[$this->message[0]])) return "Nieznany rezultat"; return $this->messages[$this->message[0]]['shortMessage']; } public function isActiveVatPayer($nip) { $this->message = []; $nip = str_replace("-", "", trim($nip)); if (!$this->validateNIP($nip)) { $this->message = ["Niepoprawny Numer Identyfikacji Podatkowej"]; return false; // throw new Exception("Błędny NIP"); } if (!$this->initialized) $this->initialize(); else $this->reinitialize(); $this->checkHeaders(); $params = [ "b-7" => $nip, "DOC_MODAL_ID__" => "0", "EVENT__" => "b-8", "FAST_VERLAST__" => $this->headers['Fast-Ver-Last'], ]; curl_setopt($this->ch, CURLOPT_POSTFIELDS, self::paramsUrlEncode($params)); return $this->getResult($this->execute()); } function __construct() { $this->dom = new DOMDocument(); } function __destruct() { if ($this->ch) curl_close($this->ch); } }