You are here

class USPSRateRequestInternational in Commerce USPS 8

Class USPSRateRequest.

@package Drupal\commerce_usps

Hierarchy

Expanded class hierarchy of USPSRateRequestInternational

1 file declares its use of USPSRateRequestInternational
USPSInternationalRateRequestTest.php in tests/src/Unit/USPSInternationalRateRequestTest.php
1 string reference to 'USPSRateRequestInternational'
commerce_usps.services.yml in ./commerce_usps.services.yml
commerce_usps.services.yml
1 service uses USPSRateRequestInternational
commerce_usps.usps_rate_request_international in ./commerce_usps.services.yml
Drupal\commerce_usps\USPSRateRequestInternational

File

src/USPSRateRequestInternational.php, line 14

Namespace

Drupal\commerce_usps
View source
class USPSRateRequestInternational extends USPSRateRequestBase implements USPSRateRequestInterface {

  /**
   * Resolve the rates from the RateRequest response.
   *
   * @param array $response
   *   The rate request array.
   *
   * @return array
   *   An array of ShippingRates or an empty array.
   */
  public function resolveRates(array $response) {
    $rates = [];

    // Parse the rate response and create shipping rates array.
    if (!empty($response['IntlRateV2Response']['Package']['Service'])) {

      // Convert the service response to an array of rates when
      // only 1 rate is returned.
      if (!empty($response['IntlRateV2Response']['Package']['Service']['Postage'])) {
        $response['IntlRateV2Response']['Package']['Service'] = [
          $response['IntlRateV2Response']['Package']['Service'],
        ];
      }
      foreach ($response['IntlRateV2Response']['Package']['Service'] as $service) {
        $price = $service['Postage'];

        // Attempt to use an alternate rate class if selected.
        if (!empty($this->configuration['rate_options']['rate_class'])) {
          switch ($this->configuration['rate_options']['rate_class']) {
            case 'commercial_plus':
              $price = !empty($service['CommercialPlusPostage']) ? $service['CommercialPlusPostage'] : $price;
              break;
            case 'commercial':
              $price = !empty($service['CommercialPostage']) ? $service['CommercialPostage'] : $price;
              break;
          }
        }
        $service_code = $service['@attributes']['ID'];
        $service_name = $this
          ->cleanServiceName($service['SvcDescription']);

        // Only add the rate if this service is enabled.
        if (!in_array($service_code, $this->configuration['services'])) {
          continue;
        }
        $rates[] = new ShippingRate([
          'shipping_method_id' => $this->shippingMethod
            ->id(),
          'service' => new ShippingService($service_code, $service_name),
          'amount' => new Price($price, 'USD'),
        ]);
      }
    }
    return $rates;
  }

  /**
   * Initialize the rate request object needed for the USPS API.
   */
  public function buildRate() {

    // Invoke the parent to initialize the uspsRequest.
    parent::buildRate();
    $this->uspsRequest
      ->setInternationalCall(TRUE);
    $this->uspsRequest
      ->addExtraOption('Revision', 2);

    // Add each package to the request.
    // Todo: IntlRateV2 is limited to 25 packages per txn.
    foreach ($this
      ->getPackages() as $package) {
      $this->uspsRequest
        ->addPackage($package);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
USPSRateRequestBase::$commerceShipment protected property The commerce shipment entity.
USPSRateRequestBase::$configuration protected property The configuration array from a CommerceShippingMethod. Overrides USPSRequest::$configuration
USPSRateRequestBase::$eventDispatcher protected property The event dispatcher.
USPSRateRequestBase::$shippingMethod protected property The shipping method being rated.
USPSRateRequestBase::$uspsRequest protected property The USPS rate request API.
USPSRateRequestBase::$uspsShipment protected property The USPS Shipment object.
USPSRateRequestBase::alterRate public function Allow rate to be altered. Overrides USPSRateRequestInterface::alterRate
USPSRateRequestBase::cleanServiceName public function Utility function to clean the USPS service name.
USPSRateRequestBase::getPackages public function Get an array of USPS packages.
USPSRateRequestBase::getRates public function Fetch rates from the USPS API. Overrides USPSRateRequestInterface::getRates
USPSRateRequestBase::logRequest public function Logs the request data.
USPSRateRequestBase::logResponse public function Logs the response data.
USPSRateRequestBase::setConfig public function Set the request configuration. Overrides USPSRequest::setConfig
USPSRateRequestBase::setMode protected function Set the mode to either test/live.
USPSRateRequestBase::setShipment public function Set the commerce shipment.
USPSRateRequestBase::setShippingMethod public function Set the shipping method being rated.
USPSRateRequestBase::__construct public function USPSRateRequest constructor.
USPSRateRequestInternational::buildRate public function Initialize the rate request object needed for the USPS API. Overrides USPSRateRequestBase::buildRate
USPSRateRequestInternational::resolveRates public function Resolve the rates from the RateRequest response. Overrides USPSRateRequestInterface::resolveRates
USPSRequest::getAuth protected function Returns authentication array for a request.
USPSRequest::isTestMode protected function Determines if the shipping method is in test method..