You are here

function uc_usps_quote in Ubercart 5

Same name and namespace in other branches
  1. 6.2 shipping/uc_usps/uc_usps.module \uc_usps_quote()
  2. 7.3 shipping/uc_usps/uc_usps.module \uc_usps_quote()

Callback for retrieving USPS shipping quote.

Parameters

$products: Array of cart contents.

$details: Order details other than product information.

Return value

JSON object containing rate, error, and debugging information.

1 string reference to 'uc_usps_quote'
uc_usps_shipping_method in shipping/uc_usps/uc_usps.module
Implementation of Übercart's hook_shipping_method().

File

shipping/uc_usps/uc_usps.module, line 267
Shipping quote method module that receives quotes from the United States Postal Service via XML web service.

Code

function uc_usps_quote($products, $details) {
  include_once drupal_get_path('module', 'uc_store') . '/includes/simplexml.php';

  //drupal_set_message('<pre>'. print_r($products, true) .'</pre>');

  //drupal_set_message('<pre>'. print_r($details, true) .'</pre>');
  $services = array();
  $addresses = array(
    (array) variable_get('uc_quote_store_default_address', new stdClass()),
  );
  $packages = _uc_usps_package_products($products, $addresses);
  if (!count($packages)) {
    return array();
  }
  $dest = (object) $details;
  $usps_server = 'production.shippingapis.com';
  $api_dll = 'ShippingAPI.dll';
  $connection_url = 'http://' . $usps_server . '/' . $api_dll;
  foreach ($packages as $key => $ship_packages) {
    $orig = (object) $addresses[$key];
    $orig->email = variable_get('uc_store_email', '');
    $request = uc_usps_rate_request($ship_packages, $orig, $dest);
    $result = drupal_http_request($connection_url, array(), 'POST', $request);
    if (user_access('configure quotes') && variable_get('uc_quote_display_debug', false)) {
      $services['data']['debug'] .= htmlentities($result->data) . "<br />\n";
    }
    $response = new JSimpleXML();
    $response
      ->loadString($result->data);
    if (is_array($response->document->package)) {
      foreach ($response->document->package as $package) {
        if (isset($package->error)) {
          $services['data']['error'] .= $package->error[0]->description[0]
            ->data() . '<br />';
        }
        else {
          foreach ($package->postage as $postage) {
            $attr = $postage
              ->attributes();
            if ($attr['classid'] === 0 || $attr['classid'] === '0') {
              if ($postage->mailservice[0]
                ->data() == "First-Class Mail Parcel") {
                $attr['classid'] = 'zeroParcel';
              }
              else {
                if ($postage->mailservice[0]
                  ->data() == "First-Class Mail Flat") {
                  $attr['classid'] = 'zeroFlat';
                }
                else {
                  $attr['classid'] = 'zero';
                }
              }
            }
            $services[$attr['classid']]['label'] = t('U.S.P.S. @service', array(
              '@service' => $postage->mailservice[0]
                ->data(),
            ));
            $services[$attr['classid']]['rate'] += uc_usps_markup($postage->rate[0]
              ->data());
          }
        }
      }
    }
  }
  $usps_services = array_filter(variable_get('uc_usps_services', _uc_usps_services()));
  foreach ($services as $service => $quote) {
    if ($service != 'data' && !in_array($service, $usps_services)) {
      unset($services[$service]);
    }
  }

  /* $transitreq  = 'USERID="' . variable_get('uc_usps_user_id', '') .'">' .
     '<OriginZip>' . $orig->postal_code . '</OriginZip>' .
     '<DestinationZip>' . $dest->postal_code . '</DestinationZip>'; */
  foreach ($services as $key => $quote) {
    if (isset($quote['rate'])) {
      $services[$key]['format'] = uc_currency_format($quote['rate']);
      $services[$key]['option_label'] = $quote['label'];

      /* if (strpos($quote['label'], 'Express') !== false) {
           $transreq = 'API=ExpressMailCommitment&XML='. urlencode(str_replace('Zip', 'ZIP', '<ExpressMailCommitmentRequest ' . $transitreq . '<Date/></ExpressMailCommitmentRequest>'));
         }
         elseif (strpos($quote['label'], 'Priority') !== false) {
           $transreq = 'API=PriorityMail&XML='. urlencode( '<PriorityMailRequest ' . $transitreq . '</PriorityMailRequest>');
         }
         else {
           $transreq = 'API=StandardB&XML='. urlencode( '<StandardBRequest ' . $transitreq . '</StandardBRequest>');
         }
         $result = drupal_http_request($connection_url, array(), 'POST', $transreq);
         if (user_access('configure quotes') && variable_get('uc_quote_display_debug', false)) {
           $services['data']['debug'] .= htmlentities($result->data) ."<br />\n";
         }
         $transresp = new JSimpleXML();
         $transresp->loadString($result->data);
         if (isset($transresp->document)) {
           if ($transresp->document->name() == 'Error') {
             $services[$key]['error'][] = $transresp->document->description[0]->data();
           }
           else if (isset($transresp->document->days)) {
             $services[$key]['transit'] = $transresp->document->days[0]->data();
             $services[$key]['option_label'] .= format_plural($services[$key]['transit'], ', @count day in transit', ', @count days in transit');
           }
           else if (isset($transresp->document->commitment)) {
             $services[$key]['commitment'] = $transresp->document->commitment[0]->commitmentname[0]->data() .' -- '. $transresp->document->commitment[0]->commitmenttime[0]->data();
             $services[$key]['option_label'] .= ', '. $services[$key]['commitment'];
           }
         } */
    }
  }
  uasort($services, 'uc_quote_price_sort');
  return $services;
}