PHP Class for WA States Destination Based Tax

Updated: 01/11/13

We all know states aren't happy with what they consider to be a loophole where if a customer buys something online from a company without any locations in their state, they don't have to pay the state sales tax. Well some states are adopting a new sales tax policy to attempt to circumvent this, called "Destination Based Sales Tax".

In this system, an online business much pay the amount of tax that is effective at the exact home address of the customer, regardless of the location of the business. Of course this law doesn't actually apply to any out of state businesses, because the federal law still dictates that any interstate commerce is void of sales tax from either state.

Therefore, other than continuing to collect taxes from major online retailers such as Amazon, Walmart & Staples (all of which who were already collecting sales tax on Washington residents online because they operated a physical location in Washington), this law has made it exceedingly difficult for local small businesses with an online storefront to collect sales tax on transactions.

It is a burden on small businesses because almost no online payment tools, such as PayPal or Google Checkout, support this type of tax. The most you can hope for is tax based on zip codes, but even this is inadequate, as the tax rate can differ even within the same zip code.

One thing you can do with PayPal, is ask users to enter their address prior to checking out, and pass the tax information to PayPal. The problem with this is that it is likely the majority of users wont be Washington residents, and even those who are, each step you add to the checkout process lowers conversion rates dramatically.

Regardless, I have developed a PHP Class that allows you to pass it the address, and returns with the correct tax rate to apply to the order. On my project, as my past orders are listed I check if the "Ship To" address is in WA, and if so I add an "Actual Tax" line to the order, under the "Tax Collected". Making it just a little easier to settle up the sales tax situation with the state.

/**
 * @author SmallDog
 * @contact dustin@smalldo.gs
 * @created 01-27-2011
 * @updated 01-11-2013
 *
 * destinationTax class
 * Developed to use the Washington State Department of Revenue's tax API
 * to find the correct amount of tax owed at a specific address
 *
 * --> Exmple Use Use
 *      $dor = new destinationTax;
 *      $tax = $dor->get_tax("123 Main Street", "Kirkland", "98033")
 *      echo $tax->attributes()->rate;
 *
 * --> Returns
        XML Object ->
        attributes()
            [loccode] => The four digit DOR location code
            [localrate] => The local tax rate
            [rate] => The combined local and state tax rate
            [code] => 0:  The address was found.
                      1:  The address was not found, but the ZIP+4 was  located.
                      2:  Neither the address or ZIP+4 was found, but  the 5-digit ZIP was located.
                      3:  The address, ZIP+4, and ZIP could not be  found.
                      4:  Invalid arguements.
                      5:  Internal error.
        addressline
            attributes()
                [houselow] => Standardized address low house number
                [househigh] => Standardized address high house number
                [evenodd] => 'O' or 'E'
                [street] => Standardized address street
                [state] => "WA"
                [zip] => Standardized address ZIP.
                [plus4] => Standardized address + four of Zip
                [period] => Tax period in Q[1-4]YYYY format
                [code] => The four digit DOR location code
                [rta] => 'Y' or 'N'
                [ptba] => Public transportation benefit area  name
                [cez] => Community empowerment zone name
        rate
            @attributes()
                [name] => DOR location code name
                [code] => The four digit DOR location code
                [staterate] => The state portion of the sales tax  rate
                [localrate] => The local portion of the sales tax  rate
        msg => The human readable errors and warnings, including not processed XML
 */

class destinationTax
{
  private $dor_url = "http://dor.wa.gov/";

  function build_url($addr, $city, $zip) {
    $url = $this->dor_url ."AddressRates.aspx?output=xml&addr=". urlencode($addr) ."&city=". urlencode($city) ."&zip=". urlencode($zip);
    return $url;
  }

  function get_tax($addr, $city, $zip)
  {
    $request = $this->build_url($addr, $city, $zip);
    $response = $this->_make_request($request);

    return $response;
  }

  private function _make_request($url)
  {
    if($xml = simplexml_load_file($url))
    {
      switch($xml->attributes()->code)
      {
        case 0:
          // Code 0 means address was perfect
          break;
        case 1:
          $xml->msg = "Warning: The address was not found, but the ZIP+4 was located.";
          break;
        case 2:
          $xml->msg = "Warning: Neither the address or ZIP+4 was found, but  the 5-digit ZIP was located.";
          break;
        case 3:
          $xml->msg = "Error: The address, ZIP+4, and ZIP could not be found.";
          break;
        case 4:
          $xml->msg = "Error: Invalid arguements.";
          break;
        case 5:
          $xml->msg = "Error: Internal error.";
      }

      $xml->attributes()->rate = (float) $xml->attributes()->rate;
    }
    else
      $xml = "Error: Could not load XML.
". $url; return $xml; } }
Small dog web development studio

View All Our Posts

Web application developers in western Washington, creating user centric web solutions for businesses of all shapes and sizes.

Our projects include ecommerce, event organization, user driven web applications and more.


Small Dog Studios, LLC. Small Dog Studios News