You are here

public function USPSDomesticRate::rateRequest in Ubercart 8.4

Constructs a quote request for domestic shipments.

Parameters

array $packages: Array of packages received from the cart.

$origin: Delivery origin address information.

$destination: Delivery destination address information.

Return value

string RateV4Request XML document to send to USPS.

1 call to USPSDomesticRate::rateRequest()
USPSDomesticRate::quote in shipping/uc_usps/src/Plugin/Ubercart/ShippingQuote/USPSDomesticRate.php
Callback for retrieving USPS shipping quote.

File

shipping/uc_usps/src/Plugin/Ubercart/ShippingQuote/USPSDomesticRate.php, line 302

Class

USPSDomesticRate
Provides a percentage rate shipping quote plugin.

Namespace

Drupal\uc_usps\Plugin\Ubercart\ShippingQuote

Code

public function rateRequest(array $packages, $origin, $destination) {
  $usps_config = \Drupal::config('uc_usps.settings');
  $request = '<RateV4Request USERID="' . $usps_config
    ->get('user_id') . '">';
  $request .= '<Revision>2</Revision>';
  $rate_type = $usps_config
    ->get('online_rates');
  $package_id = 0;
  foreach ($packages as $package) {
    $qty = $package->qty;
    for ($i = 0; $i < $qty; $i++) {
      $request .= '<Package ID="' . $package_id . '">' . '<Service>' . ($rate_type ? 'ONLINE' : 'ALL') . '</Service>' . '<ZipOrigination>' . substr(trim($origin->postal_code), 0, 5) . '</ZipOrigination>' . '<ZipDestination>' . substr(trim($destination->postal_code), 0, 5) . '</ZipDestination>' . '<Pounds>' . intval($package->pounds) . '</Pounds>' . '<Ounces>' . number_format($package->ounces, 1, '.', '') . '</Ounces>' . '<Container>' . $package->container . '</Container>' . '<Size>' . $package->size . '</Size>' . '<Width>' . $package->width . '</Width>' . '<Length>' . $package->length . '</Length>' . '<Height>' . $package->height . '</Height>' . '<Girth>' . $package->girth . '</Girth>' . '<Value>' . $package->price . '</Value>' . '<Machinable>' . ($package->machinable ? 'TRUE' : 'FALSE') . '</Machinable>' . '<ReturnLocations>TRUE</ReturnLocations>' . '<ShipDate Option="EMSH">' . \Drupal::service('date.formatter')
        ->format(\Drupal::time()
        ->getCurrentTime(), 'custom', 'd-M-Y', 'America/New_York', 'en') . '</ShipDate>';

      // Check if we need to add any special services to this package.
      if ($usps_config
        ->get('insurance') || $usps_config
        ->get('delivery_confirmation') || $usps_config
        ->get('signature_confirmation')) {
        $request .= '<SpecialServices>';
        if ($usps_config
          ->get('insurance')) {
          $request .= '<SpecialService>1</SpecialService>';
        }
        if ($usps_config
          ->get('delivery_confirmation')) {
          $request .= '<SpecialService>13</SpecialService>';
        }
        if ($usps_config
          ->get('signature_confirmation')) {
          $request .= '<SpecialService>15</SpecialService>';
        }
        $request .= '</SpecialServices>';
      }

      // Close off Package tag.
      $request .= '</Package>';
      $package_id++;
    }
  }
  $request .= '</RateV4Request>';
  return 'API=RateV4&XML=' . UrlHelper::encodePath($request);
}