public function __construct(uspsShipping $plugin, array $params)
 {
     $service = uspsServices::getServiceByCode(!empty($params['service']) ? $params['service'] : '');
     if (!$service) {
         throw new waException($plugin->_w("Unknown service"));
     }
     $supported = $this->getSupportedServices();
     if (is_array($supported) && !in_array($service['code'], $supported)) {
         throw new waException($plugin->_w("Unsupported service: ") . $service['code'] . $plugin->_w(". Supported services by this API are: ") . implode(", ", $supported));
     }
     $this->service = $service;
     parent::__construct($plugin, $params);
 }
 public function getPrintForms(waOrder $order = null)
 {
     $all_forms = array('usps_tracking' => array('name' => 'USPS Tracking™', 'description' => 'Generate USPS Tracking barcoded labels for Priority Mail®, First-Class Mail® parcels, and package services parcels, including Standard Post™, Media Mail®, and Library Mail. '), 'express_mail' => array('name' => 'Express Mail®', 'description' => 'Generate a single-ply Express Mail shipping label complete with return and delivery addresses, a barcode, and a mailing record for your use.'), 'signature_confirmation' => array('name' => 'Signature Confirmation™ Labels', 'description' => 'Generate a Signature Confirmation barcoded label for Priority Mail, First-Class Mail parcels, Standard Post, Media Mail, and Library Mail services, and we’ll provide the complete address label, including the Signature Confirmation Service barcode.'), 'international_shipping' => array('name' => 'International Shipping Labels', 'description' => 'Send documents and packages globally. USPS® offers reliable, affordable shipping to more than 180 countries. Generate Express Mail International®, Priority Mail International®, First-Class Mail International®, or First-Class Package International Service shipping labels complete with addresses, barcode, customs form, and mailing receipt.'));
     $forms = array();
     foreach ($all_forms as $name => $form) {
         $query_name = implode('', array_map("ucfirst", explode('_', $name)));
         $query = $this->getQuery('labels' . $query_name, true);
         $service = $this->getServiceCodeByOrder($order);
         $type = uspsServices::getServiceType($service['id']);
         if ($name == 'international_shipping') {
             if ($type == uspsServices::TYPE_INTERNATIONAL) {
                 $forms[$name] = $form;
             }
         } else {
             if ($type == uspsServices::TYPE_DOMESTIC && call_user_func_array(array($query, 'isSupportedService'), array($service['code']))) {
                 $forms[$name] = $form;
             }
         }
     }
     return $forms;
 }
 protected function parseResponse($response)
 {
     // fix unclosed br tags
     $response = preg_replace("/<br\\s*?>/i", '', $response);
     $errors = array();
     $rates = array();
     try {
         $xml = new SimpleXMLElement($response);
     } catch (Exception $ex) {
         throw new waException($this->plugin->_w("Xml isn't well-formed"));
     }
     switch ($xml->getName()) {
         case 'RateV4Response':
             $rates = $this->parseRateV4Response($xml, $errors);
             break;
         case 'IntlRateV2Response':
             $rates = $this->parseIntlRateResponse($xml, $errors);
             break;
         case 'Error':
             throw new waException((string) $xml->Description, (int) $xml->Number);
             break;
     }
     $services = uspsServices::getServices();
     foreach ($errors as $error) {
         if (isset($services[$error['id']])) {
             $error['comment'] = $services[$error['id']]['name'] . ': ' . $error['name'];
         }
         $rates[$error['id']] = $error;
     }
     foreach ($rates as &$rate) {
         $rate['est_delivery'] = isset($rate['est_delivery']) ? $rate['est_delivery'] : '';
         $rate['currency'] = 'USD';
     }
     unset($rate);
     return $rates;
 }