function uc_ups_fulfill_order_validate in Ubercart 6.2
Same name and namespace in other branches
- 8.4 shipping/uc_ups/src/Plugin/Ubercart/FulfillmentMethod/uc_ups.ship.inc \uc_ups_fulfill_order_validate()
- 5 shipping/uc_ups/uc_ups.module \uc_ups_fulfill_order_validate()
- 7.3 shipping/uc_ups/uc_ups.ship.inc \uc_ups_fulfill_order_validate()
Passes final information into shipment object.
See also
File
- shipping/
uc_ups/ uc_ups.ship.inc, line 228 - 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;
}
}
$_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->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']);
//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 SimpleXMLElement($response_obj->data);
//drupal_set_message('<pre>'. htmlentities($response->asXML()) .'</pre>');
if (isset($response->Response->Error)) {
$error = $response->Response->Error;
$error_msg = (string) $error->ErrorSeverity . ' Error ' . (string) $error->ErrorCode . ': ' . (string) $error->ErrorDescription;
//drupal_set_message('<pre>'. print_r($_SESSION['ups']['packages'], TRUE) .'</pre>' . htmlentities($request) .' <br /><br /> '. htmlentities($response->data));
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;
}