function uc_fedex_rate_request in FedEx Shipping 6.2
Same name and namespace in other branches
- 5 uc_fedex.module \uc_fedex_rate_request()
- 6 uc_fedex.module \uc_fedex_rate_request()
- 7.2 uc_fedex.module \uc_fedex_rate_request()
- 7 uc_fedex.module \uc_fedex_rate_request()
Constructs and executes a SOAP RateAvailabilityService request. Obtains Rate and Available Services information needed for shipping quote.
SOAP call parameters are set in the order they appear in the WSDL file Associative array of DOM returned.
Parameters
$packages: Array of packages received from the cart.
$origin: Delivery origin address information.
$destination: Delivery destination address information.
Return value
Associative array mirroring contents of SOAP object returned from server.
1 call to uc_fedex_rate_request()
- uc_fedex_quote in ./
uc_fedex.module - Callback for retrieving a FedEx shipping quote.
File
- ./
uc_fedex.module, line 510 - FedEx Web Services Rate / Available Services Quote.
Code
function uc_fedex_rate_request($packages, $origin, $destination) {
// Set up SOAP call.
// Allow tracing so details of request can be retrieved for error logging
$client = new SoapClient(drupal_get_path('module', 'uc_fedex') . '/wsdl-' . variable_get('uc_fedex_server_role', 'testing') . '/RateService_v10.wsdl', array(
'trace' => 1,
));
// FedEx user key and password filled in by user on admin form
$request['WebAuthenticationDetail'] = array(
'UserCredential' => array(
'Key' => variable_get('uc_fedex_user_credential_key', 0),
'Password' => variable_get('uc_fedex_user_credential_password', 0),
),
);
// FedEx account and meter number filled in by user on admin form
$request['ClientDetail'] = array(
'AccountNumber' => variable_get('uc_fedex_account_number', 0),
'MeterNumber' => variable_get('uc_fedex_meter_number', 0),
);
// Optional parameter, contains anything
$request['TransactionDetail'] = array(
'CustomerTransactionId' => '*** Rate/Available Services Request v10 from Ubercart ***',
);
// Rate Services Availability Request v10.0.0.
$request['Version'] = array(
'ServiceId' => 'crs',
'Major' => '10',
'Intermediate' => '0',
'Minor' => '0',
);
// Grab details of sender origin - not necessarily package origin
$request['RequestedShipment']['Shipper'] = array(
'Address' => array(
'PostalCode' => $origin->postal_code,
'CountryCode' => $origin->country_iso_code_2,
),
);
// Grab details of package destination
$request['RequestedShipment']['Recipient'] = array(
'Address' => array(
'PostalCode' => $destination->postal_code,
'CountryCode' => $destination->country_iso_code_2,
'Residential' => $destination->residential,
),
);
// Currency for quote
$request['RequestedShipment']['CurrencyType'] = variable_get('uc_currency_code', 'USD');
// Set Pickup/Dropoff type
$request['RequestedShipment']['DropoffType'] = variable_get('uc_fedex_dropoff_type', 'REGULAR_PICKUP');
// Note that ACCOUNT rates *require* a valid account number
// and return accurate answers on the production server
$request['RequestedShipment']['RateRequestTypes'] = strtoupper(variable_get('uc_fedex_quote_type', 'list'));
// When the package is going to ship
// have to think about this -
// cutoff times, commits store owner to exact ship date, etc.
// Probably have to make an admin menu option with cutoff time, after
// which ShipDate becomes "tomorrow" unless of course tomorrow is a
// weekend when you're closed... But this shouldn't affect the rate
// Drupal 6.x version of format_date() doesn't support 'c', so until
// then we use date() directly. date() doesn't take care of site
// timezone, though.
//$request['RequestedShipment']['ShipTimestamp'] = format_date(time(), 'custom', 'c');
$request['RequestedShipment']['ShipTimestamp'] = date('c');
//
// Packaging type - need this to be settable for each package rather
// than one site-wide setting?
//
//$request['RequestedShipment']['PackagingType'] = variable_get('uc_fedex_package_type', 'YOUR_PACKAGING');
$request['RequestedShipment']['PackageDetail'] = 'INDIVIDUAL_PACKAGES';
$request['RequestedShipment']['PackageCount'] = count($packages);
$request['RequestedShipment']['RequestedPackageLineItems'] = array();
// Determine weight and length units to send to FedEx
// FedEx supports only LB and KG; if store units are set to
// something else then determine conversion factor to apply
// to package weights
$weight_units = strtoupper(variable_get('uc_weight_unit', 'LB'));
$weight_conversion_factor = 1;
if ($weight_units != 'LB' && $weight_units != 'KG') {
$weight_conversion_factor = uc_weight_conversion($weight_units, 'LB');
$weight_units = 'LB';
}
// FedEx supports only IN and CM; if store units are set to something
// else then determine conversion factor to apply to package lengths
$length_units = strtoupper(variable_get('uc_length_unit', 'IN'));
$length_conversion_factor = 1;
if ($length_units != 'IN' && $length_units != 'CM') {
$length_conversion_factor = uc_length_conversion($length_units, 'IN');
$length_units = 'IN';
}
// Iterate over $packages to account for multi-package shipments
$sequence = 0;
foreach ($packages as $package) {
$sequence++;
$package_properties = array(
'SequenceNumber' => $sequence,
// New for v10 - what's the proper way to use this?
'GroupPackageCount' => 1,
// Weights must be rounded up to nearest integer value.
'Weight' => array(
'Value' => ceil($package->shipweight * $weight_conversion_factor),
'Units' => $weight_units,
),
// Lengths must be rounded up to nearest integer value
// Package size hardwired to 1" x 1" x 1" to force weight-based rates
'Dimensions' => array(
'Length' => ceil(1.0 * $length_conversion_factor),
'Width' => ceil(1.0 * $length_conversion_factor),
'Height' => ceil(1.0 * $length_conversion_factor),
'Units' => $length_units,
),
);
// Add Insurance if requested
if (variable_get('uc_fedex_insurance', FALSE)) {
$package_properties['InsuredValue'] = array(
'Amount' => $package->price,
'Currency' => variable_get('uc_currency_code', 'USD'),
);
}
// Fill in SOAP request with $package_properties
$request['RequestedShipment']['RequestedPackageLineItems'][] = $package_properties;
// Grab package origin - not necessarily the same as shipper
// $request['RequestedShipment']['RequestedPackageLineItems']['Origin'] = array(
// 'Address' => array(
// 'PostalCode' => $origin->postal_code,
// 'CountryCode' => $origin->country_iso_code_2,
// )
// );
}
//
// Send the SOAP request to the FedEx server
//
try {
$response = $client
->getRates($request);
if ($response->HighestSeverity != 'FAILURE' && $response->HighestSeverity != 'ERROR') {
print_request_response($client);
}
else {
drupal_set_message(t('Error in processing FedEx Shipping Quote transaction.'), 'error');
foreach ($response->Notifications as $notification) {
if (is_array($response->Notifications)) {
drupal_set_message($notification->Severity . ': ' . $notification->Message, 'error');
}
else {
drupal_set_message($notification, 'error');
}
}
}
return $response;
} catch (SoapFault $exception) {
drupal_set_message('<h2>Fault</h2><br /><b>Code:</b>' . $exception->faultcode . '<br /><b>String:</b>' . $exception->faultstring . '<br />', 'error');
// what else needs to be done here if FedEx quote fails? What to display
// to customer?
}
}