You are here

function commerce_fedex_submit_soap_request in Commerce FedEx 7

Submits a SOAP request to FedEx and returns the response object.

Parameters

object $request: The order object.

string $method: The method to invoke when submitting the request.

Return value

object The response object returned after submitting the SOAP request.

1 call to commerce_fedex_submit_soap_request()
commerce_fedex_service_rate_order in ./commerce_fedex.module
Returns an array of shipping method rates obtained from FedEx servers.

File

includes/commerce_fedex_soap_client.inc, line 69
Handles the SOAP request/response to FedEx Web Services servers.

Code

function commerce_fedex_submit_soap_request($request, $method = 'getRates') {

  // If the SoapClient class doesn't exist, then get outta here!
  if (!class_exists('SoapClient')) {
    watchdog('fedex', 'PHP SOAP extension is not enabled. Unable to get rates.', array(), WATCHDOG_ERROR);
    return FALSE;
  }

  // Add the authentication array to the request.
  $request += _commerce_fedex_soap_authentication();

  // Allow other modles the ability to alter the request before sending.
  drupal_alter('commerce_fedex_submit_soap_request', $request, $method);

  // Log the API request if specified.
  $logging_config = variable_get('commerce_fedex_log', array());
  if (!empty($logging_config['request']) && $logging_config['request'] !== 0) {
    $message = t('Submitting API request to the FedEx');
    watchdog('fedex', '@message:<pre>@request</pre>', array(
      '@message' => $message,
      '@request' => var_export($request, TRUE),
    ));
  }
  ini_set('soap.wsdl_cache_enabled', '0');

  // Determine if the production or testing WSDL should be used.
  $request_mode = variable_get('commerce_fedex_request_mode', 'testing');

  // Determine the correct wsdl file to use for this method.
  $wsdl_file = _commerce_fedex_soap_wsdl_file($method);

  // Locate the WDSL file for describing the web service.
  $wsdl = drupal_get_path('module', 'commerce_fedex') . '/includes/wsdl-' . $request_mode . '/' . $wsdl_file;

  // Make sure that the wsdl file exists before attempting the request.
  if (!file_exists($wsdl)) {
    return FALSE;
  }
  try {

    // Create the SOAP client.
    $client = new SoapClient($wsdl, array(
      'trace' => 1,
    ));

    // Attempt the SOAP request.
    $response = $client
      ->{$method}($request);

    // Log the API response if specified.
    if (!empty($logging_config['response']) && $logging_config['response'] !== 0) {
      watchdog('fedex', 'API response received:<pre>@response</pre>', array(
        '@response' => var_export($response, TRUE),
      ));
    }

    // Fedex has 5 codes for the status of a request. If we have success or
    // the status is "INFO", then just return the response since there were no
    // major problems.
    if (in_array($response->HighestSeverity, array(
      'SUCCESS',
      'NOTE',
    ))) {
      return $response;
    }
    else {
      if (!empty($response->Notifications) && !is_array($response->Notifications)) {
        $response->Notifications = array(
          $response->Notifications,
        );
      }
    }
    if (!empty($response->Notifications)) {
      foreach ($response->Notifications as $notification) {
        switch ($notification->Severity) {
          case 'ERROR':
            $watchdog_severity = WATCHDOG_ERROR;
            break;
          case 'FAILURE':
            $watchdog_severity = WATCHDOG_ALERT;
            break;
          case 'WARNING':
          default:
            $watchdog_severity = WATCHDOG_WARNING;
        }
        watchdog('fedex', 'FedEx API @severity (@code)' . PHP_EOL . '<br/><b>Source:</b> @source<br/><b>Message:</b> @message<br/><b>Localized Message:</b> @localized_message', array(
          '@severity' => $notification->Severity,
          '@code' => $notification->Code,
          '@source' => $notification->Source,
          '@message' => $notification->Message,
          '@localized_message' => $notification->LocalizedMessage,
        ), $watchdog_severity);
      }
    }
  } catch (SoapFault $exception) {
    $exception_info = array(
      '@faultcode' => $exception->faultcode,
      '@faultstring' => $exception->faultstring,
    );
    if (!empty($exception->detail)) {
      $exception_info += array(
        '@detailcause' => $exception->detail->cause,
        '@detailcode' => $exception->detail->code,
        '@detaildesc' => $exception->detail->desc,
      );
    }
    watchdog('fedex', '<h2>SOAP Fault</h2><br /><b>Code:</b> @faultcode<br /><b>String:</b> @faultstring' . (!empty($exception->detail) ? '<br/><b>Cause:</b> @detailcause<br/><b>Code:</b> @detailcode<br/><b>Description:</b> @detaildesc' : ''), $exception_info);
  }
  return FALSE;
}