You are here

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

Gets the tax from order.

Parameters

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

Return value

\CommerceGuys\AuthNet\DataTypes\Tax The total tax.

File

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

Class

OnsiteBase
Provides the Authorize.net payment gateway base class.

Namespace

Drupal\commerce_authnet\Plugin\Commerce\PaymentGateway

Code

protected function getTax(OrderInterface $order) {
  $amount = '0';
  $labels = [];
  $adjustments = $order
    ->collectAdjustments();
  if ($adjustments) {
    $adjustments = $this->adjustmentTransformer
      ->combineAdjustments($adjustments);
    $adjustments = $this->adjustmentTransformer
      ->roundAdjustments($adjustments);
    foreach ($adjustments as $adjustment) {
      if ($adjustment
        ->getType() !== 'tax') {
        continue;
      }
      $amount = Calculator::add($amount, $adjustment
        ->getAmount()
        ->getNumber());
      $labels[] = $adjustment
        ->getLabel();
    }
  }

  // Determine whether multiple tax types are present.
  $labels = array_unique($labels);
  if (empty($labels)) {
    $name = '';
    $description = '';
  }
  elseif (count($labels) > 1) {
    $name = 'Multiple Tax Types';
    $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;

  // If amount is negative, do not transmit any information.
  if ($amount < 0) {
    $amount = 0;
    $name = '';
    $description = '';
  }
  return new Tax([
    'amount' => $amount,
    'name' => $name,
    'description' => $description,
  ]);
}