debug("USPS ERROR: User ID or Password was empty. Please make sure this has been added correctly to the admin side of your store."); return false; } else { $this->user_id = $user_id; $this->password = $password; $this->api = $api; $this->request_xml = '<' . $api . 'Request USERID="' . $user_id . '" PASSWORD="' . $password . '">'; } } function reset() { $this->api = ''; $this->current_result = ''; $this->request_xml = ''; $this->package_index = 0; } function add_package($attribs = '') { if(!is_array($attribs)) { $this->debug("USPS ERROR: Package array was empty."); return false; } //Check to make sure array has required values for API if($this->api == 'RateV3') { if(empty($attribs['service']) || empty($attribs['ziporigin']) || empty($attribs['zip_dest']) || (empty($attribs['size']) && strtolower($attribs['service'])!=="first class")) { echo "USPS ERROR: One of the following variables was empty. service = '".$attribs['service']."', ziporigin = '".$attribs['ziporigin']."', zip_dest = '".$attribs['zip_dest']."', size = '".$attribs['size']."'"; return false; } //Check service type if(empty($attribs['service'])) { $this->debug("USPS ERROR: Service variable was empty."); return false; } else { switch(strtolower($attribs['service'])){ case 'express': case 'first class': case 'priority': case 'parcel': case 'bpm': case 'library': case 'media': case 'all': break; default: $this->debug("USPS ERROR: Service variable was not recognised."); return false; } } //Check ZIP codes if(!isset($attribs['ziporigin'])) { $this->debug("USPS ERROR: Zip Origin was not set."); return false; } if(!isset($attribs['zip_dest'])) { $this->debug("USPS ERROR: Zip Destination was not set."); return false; } //Check weight if($attribs['pounds'] + $attribs['ounces'] == 0) { $this->debug("USPS ERROR: No weight set."); return false; } //Check container for Express and Priority if(strtolower($attribs['service']) == 'express' || strtolower($attribs['service']) == 'priority') { if(!isset($attribs['container'])) { $this->debug("USPS ERROR: Container for express or priority post was not set."); return false; } /*else { switch(strtoupper($attribs['container'])) { case 'VARIABLE': case 'FLAT RATE BOX': case 'FLAT RATE ENVELOPE': case 'RECTANGULAR': case 'NONRECTANGULAR': break; default: $this->debug("USPS ERROR: Container not recognised for ".$attribs['service']."."); return false; } } */ } //Check size //if(!$attribs['size']) { if(!$attribs['size'] && strtolower($attribs['service'])!=='first class') { $this->debug("USPS ERROR: Size empty."); return false; } elseif (strtolower($attribs['service'])!=='first class') { switch(strtolower($attribs['size'])){ case 'regular': case 'large': case 'oversize': break; default: $this->debug("USPS ERROR: Size not recognised."); return false; } } //Add the package to the XML request $this->request_xml .= ''; $this->package_index++; $this->request_xml .= '' . strtoupper($attribs['service']) . ''; if(strtolower($attribs['service']) == 'first class') { $this->request_xml .= '' . $attribs['first_class_mail_type'] . ''; } $this->request_xml .= '' . substr(trim($attribs['ziporigin']),0,5) . ''; $this->request_xml .= '' . substr(trim($attribs['zip_dest']),0,5) . ''; $this->request_xml .= '' . $attribs['pounds'] . ''; $this->request_xml .= '' . $attribs['ounces'] . ''; if(strtoupper($attribs['container'])!=="VARIABLE") { $this->request_xml .= '' . strtoupper($attribs['container']) . ''; } $this->request_xml .= '' . ucfirst(strtolower($attribs['size'])) . ''; if(strtolower($attribs['service']) == 'priority' && strtolower($attribs['size']) == 'large') { $this->request_xml .= '' .$attribs['width']. ''; $this->request_xml .= '' .$attribs['length']. ''; $this->request_xml .= '' .$attribs['height']. ''; if(empty($attribs['width']) || empty($attribs['length']) || empty($attribs['height'])) { $this->debug("USPS ERROR: Width, length & height for nonrectangular or rectangular priority mail not set."); return false; } if($attribs['container'] == "NONRECTANGULAR") { $this->request_xml .= '' .$attribs['girth']. ''; if(empty($attribs['girth'])) { $this->debug("USPS ERROR: Girth for nonrectangular priority mail not set."); return false; } } } if(strtolower($attribs['service']) == 'all' || strtolower($attribs['service']) == 'parcel' || (strtolower($attribs['service']) == 'first class' && $attribs['first_class_mail_type'] == 'LETTER') || strtolower($attribs['service']) == 'first class' && $attribs['first_class_mail_type'] == 'FLAT') { if(empty($attribs['machinable'])) { $this->debug("USPS ERROR: Machinable variable for parcel service not set."); return false; } $this->request_xml .= '' . $attribs['machinable'] . ''; } $this->request_xml .= ''; } elseif($this->api == 'IntlRate') { if(!isset($attribs['pounds'])) { $this->debug("USPS ERROR: International pounds not set."); return false; } if(!isset($attribs['ounces'])) { $this->debug("USPS ERROR: International ounces not set."); return false; } if(!$attribs['mail_type']) { $this->debug("USPS ERROR: International mail type not set."); return false; } else { switch(strtolower($attribs['mail_type'])) { case 'package': case 'postcards or aerogrammes': case 'matter for the blind': case 'envelope': break; default: $this->debug("USPS ERROR: Mail type not recognised."); return false; } } if(!isset($attribs['country'])) { $this->debug("USPS ERROR: Country not set."); return false; } if(!in_array($attribs['country'], $this->country_list)) { $this->debug("USPS ERROR: Country not in list."); return false; } //Add the package to the XML request $this->request_xml .= ''; $this->package_index++; $this->request_xml .= '' . $attribs['pounds'] . ''; $this->request_xml .= '' . $attribs['ounces'] . ''; $this->request_xml .= '' . $attribs['mail_type'] . ''; $this->request_xml .= '' .$attribs['value'] . ''; $this->request_xml .= '' . $attribs['country'] . ''; $this->request_xml .= ''; } return true; } function submit_request() { global $module, $config; $this->request_xml .= 'api . 'Request>'; //Create a cURL instance and retrieve XML response if(!is_callable("curl_exec")) die("USPS::submit_request: curl_exec is uncallable"); /* if($module['test']==1){ //$USPSURL = "http://testing.shippingapis.com/ShippingAPI.dll"; //$USPSURL = "http://stg-production.shippingapis.com/shippingapi.dll"; } else { //$USPSURL = "http://production.shippingapis.com/ShippingAPI.dll"; } */ $USPSURL = "http://production.shippingapis.com/ShippingAPI.dll"; $ch = curl_init($USPSURL); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "API=" . $this->api . "&XML=" . $this->request_xml); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if($config['proxy']==1){ curl_setopt ($ch, CURLOPT_PROXY, $config['proxyHost'].":".$config['proxyPort']); } $return_xml = curl_exec($ch); if (stristr($return_xml, 'Authorization failure')) { echo "Authorization Error connecting to United States Postal Service Server: ".$USPSURL."

Please register at http://www.usps.com/webtools/ and enter the correct username and password in the CubeCart control panel. Please also make sure that USPS has granted your account access to the 'production server'.

Please go back and verify your login information. Remember, you must have your USPS issued username entered and something in the password box.

"; exit; } else if (stristr($return_xml, 'error') && !stristr($return_xml, 'Service not available.')) { preg_match('#(.+)#iu', $return_xml, $matches); echo 'USPS Error: '.$matches[1]; if ($module['debug']) { echo "
Request XML:
".nl2br(htmlspecialchars($this->request_xml)); echo "
Return XML:
".nl2br(htmlspecialchars($return_xml)); } exit; } $xml = new SimpleXMLElement($return_xml); if ($xml->getName() == 'RateV3Response') { if (isset($xml->Package->Error)) { $this->current_result[0]['Error']['Description'] = (string)$xml->Package->Error; } else if (isset($xml->Package->ZipOrigination)) { foreach ($xml->Package->Postage as $key => $service) { $x_mailservice = (string)$service->MailService; $x_rate = (string)$service->Rate; $this->current_result[0]['Postage'][$x_mailservice] = $x_rate; } } } else if ($xml->getName() == 'IntlRateResponse') { if (isset($xml->Package->Error)) { $this->current_result[0]['Error']['Description'] = (string)$xml->Package->Error; } else if (isset($xml->Package->Service)) { foreach ($xml->Package->Service as $key => $service) { $x_mailservice = (string)$service->SvcDescription; $x_rate = (string)$service->Postage; $this->current_result[0]['Postage'][$x_mailservice] = $x_rate; } } } return true; } function get_rates($package_id = 0) { if ($this->current_result[$package_id]['Error']) return $this->current_result[$package_id]['Error']['Description']; # if ($this->api == 'RateV3') { return $this->current_result[$package_id]['Postage']; # } else if ($this->api == 'IntlRate') { //SvcDescription and Postage # $result = array(); # foreach($this->current_result[$package_id]['Service'] as $service){ # $key = $service['SvcDescription']; # $result[$key] = $service['Postage']; # } return $result; # } # return false; } function get_prohibitions($package_id) { if($this->api == 'IntlRate') return $this->current_result[$package_id]['Prohibitions']; else return false; } function get_restrictions($package_id) { if($this->api == 'IntlRate') return $this->current_result[$package_id]['Restrictions']; else return false; } function get_observations($package_id) { if($this->api == 'IntlRate') return $this->current_result[$package_id]['Observations']; else return false; } function get_areas_served($package_id) { if($this->api == 'IntlRate') return $this->current_result[$package_id]['AreasServed']; else return false; } function get_package_error($package_id) { if($this->current_result[$package_id]['Error']) return $this->current_result[$package_id]['Error']; } } ?>