You are here

function uc_fedex_rate_request in FedEx Shipping 5

Same name and namespace in other branches
  1. 6.2 uc_fedex.module \uc_fedex_rate_request()
  2. 6 uc_fedex.module \uc_fedex_rate_request()
  3. 7.2 uc_fedex.module \uc_fedex_rate_request()
  4. 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 532
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') . '/RateAvailableServicesService_v2.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 - let admin configure */
  $request['TransactionDetail'] = array(
    'CustomerTransactionId' => '*** Rate/Available Services Request v2 from Ubercart ***',
  );

  /* Rate Available Services Request v2.0.0 */
  $request['Version'] = array(
    'ServiceId' => 'crss',
    // crs for rate, crss for rate&services
    'Major' => '2',
    'Intermediate' => '0',
    'Minor' => '0',
  );

  /* Grab details of package origin - assumed to be store location */
  $request['Origin'] = array(
    'PostalCode' => $origin->postal_code,
    'CountryCode' => $origin->country_iso_code_2,
  );

  /* Grab details of package destination */
  $request['Destination'] = array(
    'PostalCode' => $destination->postal_code,
    'CountryCode' => $destination->country_iso_code_2,
    'Residential' => $destination->residence,
  );

  /* Currency for quote */
  $request['CurrencyType'] = variable_get('uc_currency_code', 'USD');

  /* Set Pickup/Dropoff type */
  $request['DropoffType'] = variable_get('uc_fedex_dropoff_type', 'REGULAR_PICKUP');

  // These next two aren't needed right now because quotes are being requested
  // for all avaiable services, then filtered before presenting the customer
  // with options.

  /* If CarrierCode is left out, all available services will be quoted */

  //$request['CarrierCode'] = 'FDXE';

  /* Service type - valid codes STANDARD_OVERNIGHT, FEDEX_GROUND, ... */

  //$request['ServiceType'] = 'STANDARD_OVERNIGHT';

  //
  // Packaging type - need this to be settable for each package rather
  // than one site-wide setting?
  //
  $request['PackagingType'] = variable_get('uc_fedex_package_type', 'YOUR_PACKAGING');

  /* 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
  $request['ShipDate'] = date('Y-m-d');

  // Note that ACCOUNT rates *require* a valid account number
  // and return accurate answers on the production server
  $request['RateRequestTypes'] = strtoupper(variable_get('uc_fedex_quote_type', 'list'));

  // Need to allow admin to configure special services
  $request['SpecialServicesRequested'] = array(
    'SpecialServiceTypes' => 'WEEKDAY_DELIVERY',
  );

  // Need to iterate over $packages to account for multi-package
  // shipments.  Right now everything is assumed to be one package.
  $PassRateRequestPackageSummary = TRUE;

  // Passing multi piece shipment rate request (by setting PieceCount > 1)
  if ($PassRateRequestPackageSummary) {
    $request['RateRequestPackageSummary'] = array(
      'PieceCount' => 1,
      'TotalWeight' => array(
        'Value' => $packages[0]->shipweight,
        'Units' => 'LB',
      ),
      'PerPieceDimensions' => array(
        'Length' => '1',
        'Width' => '1',
        'Height' => '1',
        'Units' => 'IN',
      ),
    );
  }
  else {

    // Passing single piece shipment rate request
    // currently only one occurrence of RequestedPackage is supported
    $request['PackageCount'] = 1;
    $request['Packages'] = array(
      0 => array(
        'Weight' => array(
          'Value' => 10.0,
          'Units' => 'LB',
        ),
        'InsuredValue' => array(
          'Amount' => 100,
          'Currency' => 'USD',
        ),
        'Dimensions' => array(
          'Length' => '1',
          'Width' => '1',
          'Height' => '1',
          'Units' => 'IN',
        ),
        'SpecialServicesRequested' => array(
          'SpecialServiceTypes' => 'WEEKDAY_DELIVERY',
        ),
      ),
    );
  }

  //
  // Send the SOAP request to the FedEx server
  //
  try {
    $response = $client
      ->__soapCall("rateAvailableServices", array(
      'parameters' => $request,
    ));
    if ($response->HighestSeverity != 'FAILURE' && $response->HighestSeverity != 'ERROR') {
      print_request_response($client);
    }
    else {
      drupal_set_message('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?
  }
}