PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Dave Smith   PHP VAT Number Check   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example Usage
Class: PHP VAT Number Check
Validate and query VAT related information
Author: By
Last change:
Date: 8 years ago
Size: 3,518 bytes
 

Contents

Class file image Download
<?php
/*
usage example
vatLayer 1.0
*/

//instantiate the class
include('vatlayer.class.php');
$vatLayer = new vatLayer();

//validate vat number
echo '<h4>Validate VAT Number</h4>';

//set api end point to validate
$vatLayer->setEndPoint('validate');

//set the vat number
//we use the setParam method to set the parameter and value
//to send to the api
$vatLayer->setParam('vat_number','LU26375245');

//get the api response
$vatLayer->getResponse();

//the response object will be in the class property response
//we need to ensure that the countries vat database was online
//before trusting the validation
if( $vatLayer->response->database == 'ok' ){
   
//manage the booleans
   
$valid = ( $vatLayer->response->valid ) ? 'Yes' : 'No';
   
$formatValid = ( $vatLayer->response->format_valid ) ? 'Yes' : 'No';
   
    echo
'VAT number: '.$vatLayer->response->vat_number.'<br>';
    echo
'Correct format: '.$formatValid.'<br>';
    echo
'Valid: '.$valid.'<br>';
    echo
'Country Code: '.$vatLayer->response->country_code.'<br>';
    echo
'Company: '.$vatLayer->response->company_name.'<br>';
    echo
'Address: '.$vatLayer->response->company_address.'<br>';
}else{
    echo
'sorry, '.$vatLayer->response->country_code.' vat database is offline<br>';
}

//get VAT rates for a specified country
echo '<h4>VAT Rates by country code</h4>';

//set api end point to rate
$vatLayer->setEndPoint('rate');

//reset parameters for new request
$vatLayer->resetParams();

//set the country code
$vatLayer->setParam('country_code','DE');

//get the api response
$vatLayer->getResponse();

echo
'The returned response contains the following, including reduced rates<br>';
var_dump($vatLayer->response);

//calculate vat compliant price
echo '<h4>Calculate VAT compliant price</h4>';

//set api end point to price
$vatLayer->setEndPoint('price');

//reset parameters for new request
$vatLayer->resetParams();

//set the country code
$vatLayer->setParam('country_code','DE');

//set the amount to calculate VAT on
$vatLayer->setParam('amount',150);

//get the api response
$vatLayer->getResponse();

echo
'Item purchased in '.$vatLayer->response->country_name.' for '.$vatLayer->response->price_excl_vat.' costs '.$vatLayer->response->price_incl_vat.' including VAT<br>';

echo
'<br>Items in reduced VAT types also supported<br>';

//set the vat type parameter, in addition to already set parameters
$vatLayer->setParam('type','medical');

//get the api response
$vatLayer->getResponse();

echo
'<br>'.$vatLayer->response->type.' goods cost '.$vatLayer->response->price_incl_vat.' using a VAT rate of '.$vatLayer->response->vat_rate.'<br>';

//reduced vat rate types
//this api call is not charged against your monthtly limit
echo '<h4>List of reduced VAT rate types</h4>';

//set the api end point to types
$vatLayer->setEndPoint('types');

//reset parameters for new request
//there are no parameters sent to the types end point
$vatLayer->resetParams();

//get api response
$vatLayer->getResponse();

//show types
foreach( $vatLayer->response->types as $type ){
    echo
$type.'<br>';
}

//all vat rates by country
//to conserve bandwidth, this end point is not automatically shown
//uncomment the following lines to see it in operation

//echo '<h4>Returned object for all VAT rates by country</h4>';
//$vatLayer->setEndPoint('rate_list');
//$vatLayer->getResponse();
//var_dump($vatLayer->response);

?>