You are here

function uc_ups_fulfill_order_validate in Ubercart 5

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. 6.2 shipping/uc_ups/uc_ups.ship.inc \uc_ups_fulfill_order_validate()
  3. 7.3 shipping/uc_ups/uc_ups.ship.inc \uc_ups_fulfill_order_validate()

Validation handler for uc_ups_fulfill_order().

Pass final information into shipment object.

See also

uc_ups_confirm_shipment

File

shipping/uc_ups/uc_ups.module, line 1112
Shipping quote module that interfaces with www.ups.com to get rates for small package shipments.

Code

function uc_ups_fulfill_order_validate($form_id, $form_values) {
  include_once drupal_get_path('module', 'uc_store') . '/includes/simplexml.php';
  $origin = new stdClass();
  $destination = new stdClass();
  $packages = array();
  foreach ($form_values as $key => $value) {
    if (substr($key, 0, 7) == 'pickup_') {
      $field = substr($key, 7);
      $origin->{$field} = $value;
    }
    else {
      if (substr($key, 0, 9) == 'delivery_') {
        $field = substr($key, 9);
        $destination->{$field} = $value;
      }
    }
  }
  $_SESSION['ups'] = array();
  $_SESSION['ups']['origin'] = $origin;
  $_SESSION['ups']['destination'] = $destination;
  foreach ($form_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->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_values['service'];
  $_SESSION['ups']['ship_date'] = $form_values['ship_date'];
  $_SESSION['ups']['expected_delivery'] = $form_values['expected_delivery'];
  $_SESSION['ups']['order_id'] = $form_values['order_id'];
  $request = uc_ups_shipment_request($_SESSION['ups']['packages'], $origin, $destination, $form_values['service']);

  //print htmlentities($request);
  $response_obj = drupal_http_request(variable_get('uc_ups_connection_address', 'https://wwwcie.ups.com/ups.app/xml/') . 'ShipConfirm', array(), 'POST', $request);
  $response = new JSimpleXML();
  $response
    ->loadString($response_obj->data);

  //drupal_set_message('<pre>'. htmlentities($response->document->asXML()) .'</pre>');
  if (is_array($response->document->response[0]->error)) {
    $error = $response->document->response[0]->error[0];
    $error_msg = $error->errorseverity[0]
      ->data() . ' Error ' . $error->errorcode[0]
      ->data() . ': ' . $error->errordescription[0]
      ->data();
    drupal_set_message($error_msg, 'error');

    //drupal_set_message('<pre>'. print_r($_SESSION['ups']['packages'], true) .'</pre>' . htmlentities($request) .' <br /><br /> '. htmlentities($response->data));
    if ($error->errorseverity[0]
      ->data() == 'Hard') {
      return null;
    }
  }
  $charge = new stdClass();

  // if NegotiatedRates exist, quote based on those, otherwise, use TotalCharges
  if (is_array($response->document->shipmentcharges)) {
    $charge = $response->document->shipmentcharges[0]->totalcharges[0];
    $_SESSION['ups']['rate']['type'] = t('Total Charges');
    if (is_array($response->document->shipmentcharges[0]->negotiatedrates)) {
      $charge = $response->document->shipmentcharges[0]->negotiatedrates[0]->netsummarycharges[0]->grandtotal[0];
      $_SESSION['ups']['rate']['type'] = t('Negotiated Rates');
    }
  }
  $_SESSION['ups']['rate']['currency'] = $charge->currencycode[0]
    ->data();
  $_SESSION['ups']['rate']['amount'] = $charge->monetaryvalue[0]
    ->data();
  $_SESSION['ups']['digest'] = $response->document->shipmentdigest[0]
    ->data();
}