PHP Classes
elePHPant
Icontem

File: class.fasttax.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Jonathan Nichols  >  DOTS FastTax  >  class.fasttax.php  >  Download  
File: class.fasttax.php
Role: Class source
Content type: text/plain
Description: FastTAx base class
Class: DOTS FastTax
Get tax rate information from DOTS FastTax
Author: By
Last change: Made available to users without login
Date: 6 years ago
Size: 5,030 bytes
 

Contents

Class file image Download
<?php
class FastTax {
   
/**
     * @author Jonathan Nichols
     * @version 1.0
     * @copyright 2009-06-02
     */
    
    /**
     *
     * Use however you want. I do not expect any credit for this, but if you feel it is necessary... thanks.
     *
     */

    /**
     * Short desc
     * A simple class to submit information to DOTS FastTax
     * Uses the WSDL feed to gather information about the tax
     *
     * Sample usage:
     *
     * $fasttax = new FastTax();
     * $fasttax->license_key = 'XXXX-XXXX-XXXX';
     * $fasttax->setPostalCode("90210");
     * echo "<pre>";
     * if (!$fasttax->SendRequest()) {
     * echo $fasttax->error;
     * } else {
     * print_r($fasttax->outputs);
     * }
     * echo "</pre>";
     */
   
    /**
     * Short Desc
     *
     * @TODO Create language files
     * Define constant error messages
     */
   
const POSTAL_CODE_ERR_MSG = "A valid postal code must be supplied.<br />";
    const
LICENSE_INVALID = "A valid license key must be supplied.<br />";
    const
REQUEST_ERR = "There was an error processing this request.<br />";
   
   
/**
     * Short Desc
     *
     * This is what prepends all functions with the exception of the Canada function.
     * e.g. <b>GetTaxInfoBy</b>CityCountyState
     */
   
const FUNCTION_PREPEND = "GetTaxInfoBy";
   
   
/**#@+
     * @access public
     * @var string
     */
   
public $error = null;
    public
$is_production = false;
   
/**#@-*/
   
    /**#@+
     * @access public
     * @var array
     */
   
public $outputs = array();
   
/**#@-*/
   
    /**#@+
     * @access public
     * @var string
     */
   
public $license_key = null;
    public
$postal_code = null;
    public
$address = null;
    public
$city = null;
    public
$county = null;
    public
$state = null;
    public
$province = null;
    public
$tax_type = "sales";
    public
$lookup_function = "Zip_V2";
   
/**#@-*/
   
    /**#@+
     * @access private
     * @var string
     */
   
private $server_address = "";
   
/**#@-*/
   
    /**#@+
     * @access public
     * @return $this
     */
   
public function __construct() {
       
$this->server_address = $this->is_production ? "" : "http://trial.serviceobjects.com/ft/FastTax.asmx?WSDL";
        return require_once(
"lib/nusoap/nusoap.php");
    }
   
/**#@-*/
   
    /**#@+
     * @access private
     * @return string
     */
   
private function UseValues() {
        if (
$this->lookup_function == "GetCandianTaxInfoByProvince" ) {return array("Province" => $this->province, "TaxType" => $this->tax_type, "LicenseKey" => $this->license_key);}
        if ( (
$this->lookup_function == "Zip_V2") || ($this->lookup_function = "Zip") ) {return array("PostalCode" => $this->postal_code, "TaxType" => $this->tax_type, "LicenseKey" => $this->license_key);}
        if (
$this->lookup_function == "Address" ) {return array("City" => $this->city, "State" => $this->State, "PostalCode" => $this->postal_code, "Address" => $this->Address, "TaxType" => $this->tax_type, "LicenseKey" => $this->license_key);}
        if (
$this->lookup_function == "CityState" ) {return array("City" => $this->city, "State" => $this->state, "TaxType" => $this->tax_type, "LicenseKey" => $this->license_key);}
        if (
$this->lookup_function == "CityCountyState" ) {return array("City" => $this->city, "County" => $this->county, "State" => $this->state, "TaxType" => $this->tax_type, "LicenseKey" => $this->license_key);}
        return
false;
    }
   
/**#@-*/
   
    /**#@+
     * @access private
     * @return boolean
     */
   
private function CheckLicense() {
        if (isset(
$this->license_key)){return true;}
       
       
$this->error = self::LICENSE_INVALID;
        return
false;
    }
   
/**#@-*/
   
    /**#@+
     * @access public
     * @param string 5 or 6 digit postal code
     * @return string
     */
   
public function setPostalCode($postalcode = null) {
        if (!
$this->CheckLicense()){return false;}
       
$this->error = null;
            if ( (!isset(
$postalcode)) || (strlen($postalcode) < 5) || (!preg_match('/^(\d{5}-\d{4}|\d{5}|\d{9})$|^([a-zA-Z]\d[a-zA-Z]( )?\d[a-zA-Z]\d)$/', $postalcode)) ) {
           
$this->error = self::POSTAL_CODE_ERR_MSG;
            return
false;
        }
        if ( (
strlen($postalcode) > 5) && (!preg_match('/^([a-zA-Z]\d[a-zA-Z]( )?\d[a-zA-Z]\d)$/', $postalcode)) ) {
           
$postalcode = substr($postalcode, 0, 5);
        }
       
$this->postal_code = $postalcode;
    }
   
   
/**#@-*/
   
    /**#@+
     * @access public
     * @return boolean
     */
   
public function SendRequest() {
        if (!
$this->CheckLicense()){return false;}
       
$this->error = null;
       
       
$client = new nusoap_client($this->server_address, true);
       
        if (
$client->getError()) {
           
$this->err = REQUEST_ERR;
            return
false;
        }
       
       
$result = $client->call(self::FUNCTION_PREPEND . $this->lookup_function, $this->UseValues());
       
        if (
$client->fault) {
           
$this->err = REQUEST_ERR;
            return
false;
        }
       
        if ( isset(
$result[self::FUNCTION_PREPEND . $this->lookup_function . "Result"]["Error"]) ) {
           
$this->error = $result[self::FUNCTION_PREPEND . $this->lookup_function . "Result"]["Error"]["Desc"];
            return
false;
        }
       
       
$this->outputs = $result[self::FUNCTION_PREPEND . $this->lookup_function . "Result"]["TaxInfo"];
        return
true;
    }
}
?>