You are here

function commerce_usps_rate in Commerce USPS 7

Same name and namespace in other branches
  1. 7.2 commerce_usps.module \commerce_usps_rate()

Shipping service callback: returns a base price array for a shipping service calculated for the given order.

Parameters

array $service: An array describing the shipping service.

object $order: The order object.

Return value

array The service rates returned from USPS

1 string reference to 'commerce_usps_rate'
commerce_usps_commerce_shipping_service_info in ./commerce_usps.module
Implements hook_commerce_shipping_service_info().

File

./commerce_usps.module, line 81
Defines the USPS shipping method and services for Drupal Commerce.

Code

function commerce_usps_rate($service, $order) {

  // Attempt to recover cached shipping rates.
  // @todo: Allow the setting of commerce_usps_rates_timeout.
  $rates = commerce_shipping_rates_cache_get('usps', $order, variable_get('commerce_usps_rates_timeout', 0));

  // If no cached rates were found or they have expired.
  if (!$rates) {

    // Load files required for building requests.
    require_once dirname(__FILE__) . '/commerce_usps.xml.inc';
    $rates = array();

    // Build the request.
    $request = commerce_usps_build_rate_request($order);
    if ($request) {

      // Submit the request.
      $response = commerce_usps_api_request($request, t('Requesting shipping rates for Order @order_number', array(
        '@order_number' => $order->order_number,
      )));
      if (!empty($response->Package)) {

        // Parse the response to cache all requested rates for the order.
        foreach ($response->Package as $package) {

          // If the package contains an error.
          if ($package->Error) {

            // Log the error.
            $watchdog_meta = array(
              '@number' => !empty($package->Error->Number) ? $package->Error->Number
                ->asXML() : t('No number'),
              '@description' => !empty($package->Error->Description) ? $package->Error->Description
                ->asXML() : t('No description'),
              '@source' => !empty($package->Error->Source) ? $package->Error->Source
                ->asXML() : t('No source'),
            );
            watchdog('usps', 'Number: @number<br />Description: @description<br />Source: @source', $watchdog_meta, WATCHDOG_ERROR);
          }
          else {

            // Add an item to the rates array for the current service.
            // @todo: Use commerce currency.
            // @todo: Markup rates using rules?
            $mail_service = commerce_usps_trim_service($package->Postage->MailService);
            $service_name = commerce_usps_return_lookup($mail_service);
            $rates[$service_name] = array(
              'amount' => commerce_currency_decimal_to_amount(commerce_usps_rate_markup((string) $package->Postage->Rate), 'USD'),
              'currency_code' => 'USD',
              'data' => array(),
            );
          }
        }

        // Cache the calculated rates for subsequent requests.
        commerce_shipping_rates_cache_set('usps', $order, $rates);
      }
      elseif (empty($response)) {

        // Log the error.
        $watchdog_meta = array(
          '@number' => !empty($response->Number) ? $response->Number
            ->asXML() : t('No number'),
          '@description' => !empty($response->Description) ? $response->Description
            ->asXML() : t('No description'),
          '@source' => !empty($response->Source) ? $response->Source
            ->asXML() : t('No source'),
        );
        watchdog('usps', 'Number: @number<br />Description: @description<br />Source: @source', $watchdog_meta, WATCHDOG_ERROR);
      }
      else {
        $watchdog_meta = array(
          '!configuration' => l('configuration', 'admin/commerce/config/shipping/methods/usps/edit'),
        );
        watchdog('usps', 'No response was received from USPS. Make sure you have the correct URL set in your !configuration.', $watchdog_meta, WATCHDOG_ERROR);
      }
    }
  }

  // Return the rate for the requested service or FALSE if not found.
  return isset($rates[$service['name']]) ? $rates[$service['name']] : FALSE;
}