示例#1
0
        $vat = $this->prepareVat($country, $number);
        if ($vat) {
            $this->response = array('is_valid' => $this->soap->checkVat($vat)->valid);
        }
        return json_encode($this->response);
    }
    /**
     * Checks that there are all needed params ( Code Country and number );
     */
    protected function prepareVat($country, $number)
    {
        try {
            if (empty($country) || empty($number)) {
                throw new Exception("Both 'country' and 'number' params are mandatory");
            }
            if (!in_array($country, self::$european_countries)) {
                throw new Exception("Invalid country");
            }
            $vat = array('vatNumber' => $number, 'countryCode' => $country);
            return $vat;
        } catch (Exception $e) {
            $this->response = array('error_message' => $e->getMessage());
            return false;
        }
    }
}
// API Call
$vies = new VatValidator();
$vies->checkVat(strtoupper($_GET['country']), $_GET['number']);
$response = json_encode($vies->response);
echo $response;