You are here

protected function OnsiteBase::getShipping in Commerce Authorize.Net 8

Gets the shipping from order.

Parameters

\Drupal\commerce_order\Entity\OrderInterface $order: The order.

Return value

\CommerceGuys\AuthNet\DataTypes\Shipping The total shipping.

File

src/Plugin/Commerce/PaymentGateway/OnsiteBase.php, line 457

Class

OnsiteBase
Provides the Authorize.net payment gateway base class.

Namespace

Drupal\commerce_authnet\Plugin\Commerce\PaymentGateway

Code

protected function getShipping(OrderInterface $order) {

  // Return empty if there is no shipments field.
  if (!$order
    ->hasField('shipments')) {
    return new Shipping([
      'amount' => 0,
      'name' => '',
      'description' => '',
    ]);
  }
  $amount = '0';
  $labels = [];

  /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface[] $shipments */
  $shipments = $order
    ->get('shipments')
    ->referencedEntities();
  if ($shipments) {
    foreach ($shipments as $shipment) {

      // Shipments without an amount are incomplete / unrated.
      if ($shipment_amount = $shipment
        ->getAmount()) {
        $amount = Calculator::add($amount, $shipment_amount
          ->getNumber());
        $labels[] = $shipment
          ->label();
      }
    }
  }

  // Determine whether multiple tax types are present.
  $labels = array_unique($labels);
  if (empty($labels)) {
    $name = '';
    $description = '';
  }
  elseif (count($labels) > 1) {
    $name = 'Multiple shipments';
    $description = implode(', ', $labels);
  }
  else {
    $name = $labels[0];
    $description = $labels[0];
  }

  // Limit name, description fields to 32, 255 characters.
  $name = strlen($name) > 31 ? substr($name, 0, 28) . '...' : $name;
  $description = strlen($description) > 255 ? substr($description, 0, 252) . '...' : $description;
  return new Shipping([
    'amount' => $amount,
    'name' => $name,
    'description' => $description,
  ]);
}