You are here

function uc_usps_intl_quote in Ubercart 5

Callback for retrieving USPS shipping quote to other countries.

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_intl_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 381
Shipping quote method module that receives quotes from the United States Postal Service via XML web service.

Code

function uc_usps_intl_quote($products, $details) {
  include_once drupal_get_path('module', 'uc_store') . '/includes/simplexml.php';
  $services = array();
  $addresses = 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_intl_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);

    // drupal_set_message('<pre>'. htmlentities($result->data) .'</pre>');
    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->service as $service) {
            $attr = $service
              ->attributes();
            if ($attr['id'] === 0 || $attr['id'] === '0') {
              $attr['id'] = 'zero';
            }
            $services[$attr['id']]['label'] = t('USPS @service', array(
              '@service' => $service->svcdescription[0]
                ->data(),
            ));
            $services[$attr['id']]['rate'] += uc_usps_markup($service->postage[0]
              ->data());
          }
        }
      }
    }
  }
  $usps_services = variable_get('uc_usps_intl_services', _uc_usps_intl_services());
  foreach ($services as $service => $quote) {
    if (!in_array($service, $usps_services)) {
      unset($services[$service]);
    }
  }
  foreach ($services as $key => $quote) {
    if (isset($quote['rate'])) {
      $services[$key]['format'] = uc_currency_format($quote['rate']);
      $services[$key]['option_label'] = $quote['label'];
    }
  }
  uasort($services, 'uc_quote_price_sort');
  return $services;
}