You are here

public function USPSRateRequestBase::getRates in Commerce USPS 8

Fetch rates from the USPS API.

Parameters

\Drupal\commerce_shipping\Entity\ShipmentInterface $commerce_shipment: The commerce shipment.

\Drupal\commerce_shipping\Entity\ShippingMethodInterface $shipping_method: The shipping method being rated.

Return value

array An array of ShippingRate objects.

Throws

\Exception Exception when required properties are missing.

Overrides USPSRateRequestInterface::getRates

File

src/USPSRateRequestBase.php, line 97

Class

USPSRateRequestBase
Class USPSRateRequest.

Namespace

Drupal\commerce_usps

Code

public function getRates(ShipmentInterface $commerce_shipment, ShippingMethodInterface $shipping_method) {

  // Validate a commerce shipment has been provided.
  if ($commerce_shipment === NULL) {
    throw new \Exception('Shipment not provided');
  }

  // Set the necessary info needed for the request.
  $this
    ->setShipment($commerce_shipment);
  $this
    ->setShippingMethod($shipping_method);

  // Build the rate request.
  $this
    ->buildRate();

  // Allow others to alter the rate.
  $this
    ->alterRate();

  // Fetch the rates.
  $this
    ->logRequest();
  $this->uspsRequest
    ->getRate();
  $this
    ->logResponse();
  $response = $this->uspsRequest
    ->getArrayResponse();
  return $this
    ->resolveRates($response);
}