You are here

function commerce_usps_intl_rate_v2_request in Commerce USPS 7.2

Builds an international USPS rate request.

Parameters

object $order: The commerce order object.

array $shipping_address: The commerce_customer_address array of the shipping profile.

Return value

array An array of shipping rates.

1 call to commerce_usps_intl_rate_v2_request()
commerce_usps_rate in ./commerce_usps.module
Returns a base price array for a shipping service calculated for the order.

File

includes/commerce_usps.xml.inc, line 90
Handles rate request/response related stuff for the Commerce USPS module.

Code

function commerce_usps_intl_rate_v2_request($order, $shipping_address) {
  $rates = array();
  $weight = commerce_usps_get_order_weight($order);
  $request = new SimpleXMLElement('<IntlRateV2Request/>');
  $request
    ->addAttribute('USERID', variable_get('commerce_usps_user', ''));
  $request
    ->addChild('Revision', 2);
  $shipment_value = commerce_usps_get_shipment_value($order);

  // @TODO: Support multiple packages based on physical attributes.
  $package = $request
    ->addChild('Package');
  $package
    ->addAttribute('ID', 1);
  $package
    ->addChild('Pounds', $weight['pounds']);
  $package
    ->addChild('Ounces', $weight['ounces']);
  $package
    ->addChild('Machinable', 'True');
  $package
    ->addChild('MailType', 'Package');
  $package
    ->addChild('ValueOfContents', commerce_currency_amount_to_decimal($shipment_value, commerce_default_currency()));
  $package
    ->addChild('Country', commerce_usps_country_get_predefined_list($shipping_address['country']));
  $package
    ->addChild('Container', 'RECTANGULAR');
  $package
    ->addChild('Size', 'REGULAR');
  $package
    ->addChild('Width', '');
  $package
    ->addChild('Length', '');
  $package
    ->addChild('Height', '');
  $package
    ->addChild('Girth', '');
  $package
    ->addChild('OriginZip', substr(variable_get('commerce_usps_postal_code', ''), 0, 5));
  $package
    ->addChild('CommercialFlag', 'N');
  drupal_alter('commerce_usps_intl_rate_v2_request', $request);

  // Submit the rate request to USPS.
  $response = commerce_usps_api_request('API=IntlRateV2&XML=' . $request
    ->asXML());
  if (!empty($response->Package->Service)) {
    foreach ($response->Package->Service as $service) {
      $id = (string) $service
        ->attributes()->{'ID'};

      // Look up the shipping service by it's id.
      $usps_service = commerce_usps_service_by_id($id, 'international');

      // Make sure that the package service is registered.
      if (!empty($usps_service['machine_name'])) {
        $rates[$usps_service['machine_name']] = array(
          'amount' => commerce_currency_decimal_to_amount((string) $service->Postage, commerce_default_currency()),
          'currency_code' => commerce_default_currency(),
          'data' => array(),
        );
      }
    }
  }
  return $rates;
}