You are here

function uc_ups_fulfill_order_validate in Ubercart 7.3

Same name and namespace in other branches
  1. 8.4 shipping/uc_ups/src/Plugin/Ubercart/FulfillmentMethod/uc_ups.ship.inc \uc_ups_fulfill_order_validate()
  2. 5 shipping/uc_ups/uc_ups.module \uc_ups_fulfill_order_validate()
  3. 6.2 shipping/uc_ups/uc_ups.ship.inc \uc_ups_fulfill_order_validate()

Passes final information into shipment object.

See also

uc_ups_fulfill_order()

uc_ups_confirm_shipment()

File

shipping/uc_ups/uc_ups.ship.inc, line 229
UPS functions for label generation.

Code

function uc_ups_fulfill_order_validate($form, &$form_state) {
  $errors = form_get_errors();
  if (isset($errors)) {

    // Some required elements are missing - don't bother with making
    // a UPS API call until that gets fixed.
    return;
  }
  $origin = new stdClass();
  $destination = new stdClass();
  $packages = array();
  foreach ($form_state['values'] as $key => $value) {
    if (substr($key, 0, 7) == 'pickup_') {
      $field = substr($key, 7);
      $origin->{$field} = $value;
    }
    elseif (substr($key, 0, 9) == 'delivery_') {
      $field = substr($key, 9);
      $destination->{$field} = $value;
    }
  }

  // This is a total hack to work around changes made in the return value
  // from uc_shipping_address_form().  That function needs to be fixed, but
  // until then this should do the trick.
  $origin = $form_state['values']['pickup_address'];
  $origin->phone = $form_state['values']['phone'];
  $origin->first_name = $form_state['values']['first_name'];
  $origin->last_name = $form_state['values']['last_name'];
  $origin->company = $form_state['values']['company'];
  $origin->street1 = $form_state['values']['street1'];
  $origin->street2 = $form_state['values']['street2'];
  $origin->city = $form_state['values']['city'];
  $origin->zone = $form_state['values']['zone'];
  $origin->country = $form_state['values']['country'];
  $origin->postal_code = $form_state['values']['postal_code'];
  $origin->email = $form_state['values']['pickup_email'];
  $_SESSION['ups'] = array();
  $_SESSION['ups']['origin'] = $origin;
  if (empty($destination->company)) {
    $destination->company = $destination->first_name . ' ' . $destination->last_name;
  }

  // Determine if address is Residential or Commercial
  $destination->residential = variable_get('uc_ups_residential_quotes', FALSE);
  $_SESSION['ups']['destination'] = $destination;
  foreach ($form_state['values']['packages'] as $id => $pkg_form) {
    $package = uc_shipping_package_load($id);
    $package->pkg_type = $pkg_form['pkg_type'];
    $package->value = $pkg_form['declared_value'];
    $package->weight = $pkg_form['weight']['weight'];
    $package->weight_units = $pkg_form['weight']['units'];
    $package->length = $pkg_form['dimensions']['length'];
    $package->width = $pkg_form['dimensions']['width'];
    $package->height = $pkg_form['dimensions']['height'];
    $package->length_units = $pkg_form['dimensions']['units'];
    $package->qty = 1;
    $_SESSION['ups']['packages'][$id] = $package;
  }
  $_SESSION['ups']['service'] = $form_state['values']['service'];
  $_SESSION['ups']['paid'] = $form_state['values']['paid'];
  $_SESSION['ups']['ship_date'] = $form_state['values']['ship_date'];
  $_SESSION['ups']['expected_delivery'] = $form_state['values']['expected_delivery'];
  $_SESSION['ups']['order_id'] = $form_state['values']['order_id'];
  $request = uc_ups_shipment_request($_SESSION['ups']['packages'], $origin, $destination, $form_state['values']['service']);
  $response_obj = drupal_http_request(variable_get('uc_ups_connection_address', 'https://wwwcie.ups.com/ups.app/xml/') . 'ShipConfirm', array(
    'method' => 'POST',
    'data' => $request,
  ));
  $response = new SimpleXMLElement($response_obj->data);
  if (isset($response->Response->Error)) {
    $error = $response->Response->Error;
    $error_msg = (string) $error->ErrorSeverity . ' Error ' . (string) $error->ErrorCode . ': ' . (string) $error->ErrorDescription;
    if (strpos((string) $error->ErrorSeverity, 'Hard') !== FALSE) {
      form_set_error('', $error_msg);
      return FALSE;
    }
    else {
      drupal_set_message($error_msg, 'error');
    }
  }
  $charge = new stdClass();

  // if NegotiatedRates exist, quote based on those, otherwise, use TotalCharges
  if (isset($response->ShipmentCharges)) {
    $charge = $response->ShipmentCharges->TotalCharges;
    $_SESSION['ups']['rate']['type'] = t('Total Charges');
    if (isset($response->NegotiatedRates)) {
      $charge = $response->NegotiatedRates->NetSummaryCharges->GrandTotal;
      $_SESSION['ups']['rate']['type'] = t('Negotiated Rates');
    }
  }
  $_SESSION['ups']['rate']['currency'] = (string) $charge->CurrencyCode;
  $_SESSION['ups']['rate']['amount'] = (string) $charge->MonetaryValue;
  $_SESSION['ups']['digest'] = (string) $response->ShipmentDigest;
}