You are here

protected function PayflowLink::itemizeOrder in Commerce PayPal 8

Returns an itemized order data array for use in a name-value pair array.

Parameters

\Drupal\commerce_order\Entity\OrderInterface $order: The order whose line items should be converted into name-value pairs.

Return value

array The name-value pair array representing the line items that should be added to the name-value pair array for an API request.

1 call to PayflowLink::itemizeOrder()
PayflowLink::createSecureToken in src/Plugin/Commerce/PaymentGateway/PayflowLink.php
Requests a secure token from Payflow for use in follow-up API requests.

File

src/Plugin/Commerce/PaymentGateway/PayflowLink.php, line 704

Class

PayflowLink
Provides the PayPal Payflow Link payment gateway.

Namespace

Drupal\commerce_paypal\Plugin\Commerce\PaymentGateway

Code

protected function itemizeOrder(OrderInterface $order) {
  $nvp = [];

  // Calculate the items total.
  $items_total = new Price('0', $order
    ->getTotalPrice()
    ->getCurrencyCode());

  // Loop over all the line items on the order.
  $i = 0;
  foreach ($order
    ->getItems() as $item) {
    $item_amount = Calculator::trim($item
      ->getUnitPrice()
      ->getNumber());

    // Add the line item to the return array.
    $nvp += [
      'L_NAME' . $i => $item
        ->getTitle(),
      'L_COST' . $i => $item_amount,
      'L_QTY' . $i => $item
        ->getQuantity(),
    ];

    // Add the SKU.

    /** @var \Drupal\commerce\PurchasableEntityInterface $purchased_entity */
    $purchased_entity = $item
      ->getPurchasedEntity();
    if ($purchased_entity instanceof ProductVariationInterface) {
      $sku = $purchased_entity
        ->getSku();
    }
    else {
      $sku = $purchased_entity
        ->getOrderItemTitle();
    }
    $nvp += [
      'L_SKU' . $i => $sku,
    ];
    $items_total = $items_total
      ->add($item
      ->getTotalPrice());
    $i++;
  }
  $tax_amount = new Price('0', $order
    ->getTotalPrice()
    ->getCurrencyCode());

  // Collect the adjustments.
  $adjustments = [];
  foreach ($order
    ->collectAdjustments() as $adjustment) {

    // Skip included adjustments.
    if ($adjustment
      ->isIncluded()) {
      continue;
    }
    if ($adjustment
      ->getType() === 'tax') {
      $tax_amount = $tax_amount
        ->add($adjustment
        ->getAmount());
    }
    else {

      // Collect other adjustments.
      $type = $adjustment
        ->getType();
      $source_id = $adjustment
        ->getSourceId();
      if (empty($source_id)) {

        // Adjustments without a source ID are always shown standalone.
        $key = count($adjustments);
      }
      else {

        // Adjustments with the same type and source ID are combined.
        $key = $type . '_' . $source_id;
      }
      if (empty($adjustments[$key])) {
        $adjustments[$key] = [
          'type' => $type,
          'label' => (string) $adjustment
            ->getLabel(),
          'total' => $adjustment
            ->getAmount(),
        ];
      }
      else {
        $adjustments[$key]['total'] = $adjustments[$key]['total']
          ->add($adjustment
          ->getAmount());
      }
    }
  }
  $i = 0;
  foreach ($adjustments as $adjustment) {
    $adjustment_amount = Calculator::trim($adjustment['total']
      ->getNumber());
    $nvp += [
      'L_NAME' . $i => $adjustment['label'],
      'L_COST' . $i => $adjustment_amount,
      'L_QTY' . $i => 1,
    ];

    // Add the adjustment to the items total.
    $items_total = $items_total
      ->add($adjustment['total']);
    $i++;
  }

  // Send the items total.
  $nvp['ITEMAMT'] = Calculator::trim($items_total
    ->getNumber());

  // Send the tax amount.
  if (!$tax_amount
    ->isZero()) {
    $nvp['TAXAMT'] = Calculator::trim($tax_amount
      ->getNumber());
  }
  return $nvp;
}