You are here

function uc_usps_rate_request in Ubercart 7.3

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

Constructs a quote request for domestic shipments.

Parameters

$packages: Array of packages received from the cart.

$origin: Delivery origin address information.

$destination: Delivery destination address information.

Return value

RateV4Request XML document to send to USPS.

1 call to uc_usps_rate_request()
uc_usps_quote in shipping/uc_usps/uc_usps.module
Callback for retrieving USPS shipping quote.

File

shipping/uc_usps/uc_usps.module, line 443
United States Postal Service (USPS) shipping quote module.

Code

function uc_usps_rate_request($packages, $origin, $destination) {
  $request = '<RateV4Request USERID="' . variable_get('uc_usps_user_id', '') . '">';
  $request .= '<Revision>2</Revision>';
  $rate_type = variable_get('uc_usps_online_rates', FALSE);
  $package_id = 0;
  foreach ($packages as $package) {
    $qty = $package->qty;
    for ($i = 0; $i < $qty; $i++) {
      $request .= '<Package ID="' . $package_id . '">' . '<Service>' . ($rate_type ? 'ONLINE' : 'ALL') . '</Service>' . '<ZipOrigination>' . substr(trim($origin->postal_code), 0, 5) . '</ZipOrigination>' . '<ZipDestination>' . substr(trim($destination->postal_code), 0, 5) . '</ZipDestination>' . '<Pounds>' . intval($package->pounds) . '</Pounds>' . '<Ounces>' . number_format($package->ounces, 1, '.', '') . '</Ounces>' . '<Container>' . $package->container . '</Container>' . '<Size>' . $package->size . '</Size>' . '<Width>' . $package->width . '</Width>' . '<Length>' . $package->length . '</Length>' . '<Height>' . $package->height . '</Height>' . '<Girth>' . $package->girth . '</Girth>' . '<Value>' . $package->price . '</Value>' . '<Machinable>' . ($package->machinable ? 'TRUE' : 'FALSE') . '</Machinable>' . '<ReturnLocations>TRUE</ReturnLocations>' . '<ShipDate Option="EMSH">' . format_date(time(), 'custom', 'd-M-Y', 'America/New_York', 'en') . '</ShipDate>';

      // Check if we need to add any special services to this package.
      if (variable_get('uc_usps_insurance', FALSE) || variable_get('uc_usps_delivery_confirmation', FALSE) || variable_get('uc_usps_signature_confirmation', FALSE)) {
        $request .= '<SpecialServices>';
        if (variable_get('uc_usps_insurance', FALSE)) {
          $request .= '<SpecialService>1</SpecialService>';
        }
        if (variable_get('uc_usps_delivery_confirmation', FALSE)) {
          $request .= '<SpecialService>13</SpecialService>';
        }
        if (variable_get('uc_usps_signature_confirmation', FALSE)) {
          $request .= '<SpecialService>15</SpecialService>';
        }
        $request .= '</SpecialServices>';
      }

      // Close off Package tag.
      $request .= '</Package>';
      $package_id++;
    }
  }
  $request .= '</RateV4Request>';
  return 'API=RateV4&XML=' . drupal_encode_path($request);
}