function uc_ups_fulfill_order_validate in Ubercart 8.4
Same name and namespace in other branches
- 5 shipping/uc_ups/uc_ups.module \uc_ups_fulfill_order_validate()
- 6.2 shipping/uc_ups/uc_ups.ship.inc \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/ src/ Plugin/ Ubercart/ FulfillmentMethod/ uc_ups.ship.inc, line 232 - UPS functions for label generation.
Code
function uc_ups_fulfill_order_validate($form, FormStateInterface $form_state) {
$ups_config = \Drupal::config('uc_ups.setttings');
$errors = $form_state
->getErrors();
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 = [];
foreach ($form_state
->getValues() 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
->getValue('pickup_address');
$origin->phone = $form_state
->getValue('phone');
$origin->first_name = $form_state
->getValue('first_name');
$origin->last_name = $form_state
->getValue('last_name');
$origin->company = $form_state
->getValue('company');
$origin->street1 = $form_state
->getValue('street1');
$origin->street2 = $form_state
->getValue('street2');
$origin->city = $form_state
->getValue('city');
$origin->zone = $form_state
->getValue('zone');
$origin->country = $form_state
->getValue('country');
$origin->postal_code = $form_state
->getValue('postal_code');
$origin->email = $form_state
->getValue('pickup_email');
$_SESSION['ups'] = [];
$_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 = $ups_config
->get('residential_quotes');
$_SESSION['ups']['destination'] = $destination;
foreach ($form_state
->getValue('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
->getValue('service');
$_SESSION['ups']['paid'] = $form_state
->getValue('paid');
$_SESSION['ups']['ship_date'] = $form_state
->getValue('ship_date');
$_SESSION['ups']['expected_delivery'] = $form_state
->getValue('expected_delivery');
$_SESSION['ups']['order_id'] = $form_state
->getValue('order_id');
$request = uc_ups_shipment_request($_SESSION['ups']['packages'], $origin, $destination, $form_state
->getValue('service'));
$response_obj = \Drupal::httpClient()
->post($ups_config
->get('connection_address') . 'ShipConfirm', NULL, $request)
->send();
$response = new \SimpleXMLElement($response_obj
->getBody(TRUE));
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_state
->setErrorByName('', $error_msg);
return FALSE;
}
else {
\Drupal::messenger()
->addError($error_msg);
}
}
$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;
}