Skip to content

Instantly share code, notes, and snippets.

@mrhaw
Created July 26, 2018 08:23
Show Gist options
  • Save mrhaw/20a3dfd818fa5aeb15b433b44969547f to your computer and use it in GitHub Desktop.
Save mrhaw/20a3dfd818fa5aeb15b433b44969547f to your computer and use it in GitHub Desktop.

Revisions

  1. mrhaw created this gist Jul 26, 2018.
    50 changes: 50 additions & 0 deletions WA Sales Tax Rate Lookup MODX Snippet
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    <?php
    $output = '';

    function isValidZipCode($zipCode) {
    return (preg_match('/^[0-9]{5}(-[0-9]{4})?$/', $zipCode)) ? true : false;
    }

    //$format = !empty($format) ? '?output='.urlencode(trim($format)) : '?output=text';
    $address = !empty($address) ? '&addr='.urlencode(trim($address)) : '&addr=';
    $city = !empty($city) ? '&city='.urlencode(trim($city)) : '&city=';
    $zip2 = isValidZipCode($zip) ? '&zip='.$zip : '';
    $url = 'https://webgis.dor.wa.gov/webapi/AddressRates.aspx?output=xml'.$address.$city.$zip2;

    if ($zip2 !== '' && $zip2 !== '&zip=') {
    if (curl_init() === false) {
    $modx->log(modX::LOG_LEVEL_ERROR, 'Curl init failed');
    } else {
    $curl = curl_init();
    curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_HTTPHEADER => array('Content-Type: text/xml'),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPGET => true
    ));
    $data = curl_exec($curl);
    if(!curl_exec($curl)){
    $modx->log(modX::LOG_LEVEL_ERROR, curl_error($curl));
    }
    curl_close($curl);

    $xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
    if ((int)$xml->attributes()->code->__toString() <= 5){
    // http://php.net/manual/en/simplexmlelement.attributes.php
    $rate = $xml->attributes()->rate->__toString();
    $loccode = $xml->attributes()->loccode->__toString();
    $localrate = $xml->attributes()->localrate->__toString();
    $staterate = $xml->rate["staterate"]->__toString();
    $name = $xml->rate["name"]->__toString();
    $period = $xml->addressline["period"]->__toString();
    $ptba = $xml->addressline["ptba"]->__toString();

    $output = '<span data-toggle="tooltip" title="DOR: ' . $loccode . ', Local Rate: ' . $localrate . ', State Rate: ' . $staterate . ', Combined: ' . $rate . ', PTBA: ' . $ptba . ', TAX PERIOD: ' . $period . '">' .$name .' WA: <b>' . ($rate*100) . '%</b></span><br>';
    } else {
    $output = '<a data-toggle="modal" style="cursor:pointer" data-remote="[[~43]]" data-target="#taxMap">&raquo; State Tax Rates</a>';
    }
    }
    } else {
    $output = '<a data-toggle="modal" style="cursor:pointer" data-remote="[[~43]]" data-target="#taxMap">&raquo; State Tax Rates</a>';
    }
    return $output;