You are here

protected function ExpressCheckout::itemizeOrder in Commerce PayPal 8

Returns a name-value pair array of information to the API request.

Parameters

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

string $currency_code: The currency code.

Return value

array A name-value pair array.

1 call to ExpressCheckout::itemizeOrder()
ExpressCheckout::setExpressCheckout in src/Plugin/Commerce/PaymentGateway/ExpressCheckout.php
SetExpressCheckout API Operation (NVP) request.

File

src/Plugin/Commerce/PaymentGateway/ExpressCheckout.php, line 589

Class

ExpressCheckout
Provides the Paypal Express Checkout payment gateway.

Namespace

Drupal\commerce_paypal\Plugin\Commerce\PaymentGateway

Code

protected function itemizeOrder(OrderInterface $order, $currency_code) {
  $nvp_data = [];
  $n = 0;

  // Calculate the items total.
  $items_total = new Price('0', $currency_code);

  // Add order item data.
  foreach ($order
    ->getItems() as $item) {
    $item_amount = $this->rounder
      ->round($item
      ->getUnitPrice());
    $nvp_data += [
      'L_PAYMENTREQUEST_0_NAME' . $n => $item
        ->getTitle(),
      'L_PAYMENTREQUEST_0_AMT' . $n => $item_amount
        ->getNumber(),
      'L_PAYMENTREQUEST_0_QTY' . $n => $item
        ->getQuantity(),
    ];
    $items_total = $items_total
      ->add($item
      ->getTotalPrice());
    $n++;
  }

  // Initialize Shipping|Tax prices, they need to be sent
  // separately to PayPal.
  $shipping_amount = new Price('0', $currency_code);
  $tax_amount = new Price('0', $currency_code);

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

    // Skip included adjustments.
    if ($adjustment
      ->isIncluded()) {
      continue;
    }

    // Tax & Shipping adjustments need to be handled separately.
    if ($adjustment
      ->getType() == 'shipping') {
      $shipping_amount = $shipping_amount
        ->add($adjustment
        ->getAmount());
    }
    elseif ($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());
      }
    }
  }
  foreach ($adjustments as $adjustment) {
    $adjustment_amount = $this->rounder
      ->round($adjustment['total']);
    $nvp_data += [
      'L_PAYMENTREQUEST_0_NAME' . $n => $adjustment['label'],
      'L_PAYMENTREQUEST_0_AMT' . $n => $adjustment_amount
        ->getNumber(),
      'L_PAYMENTREQUEST_0_QTY' . $n => 1,
    ];

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

  // Send the items total.
  $items_total = $this->rounder
    ->round($items_total);
  $nvp_data['PAYMENTREQUEST_0_ITEMAMT'] = $items_total
    ->getNumber();

  // Send the shipping amount separately.
  if (!$shipping_amount
    ->isZero()) {
    $shipping_amount = $this->rounder
      ->round($shipping_amount);
    $nvp_data['PAYMENTREQUEST_0_SHIPPINGAMT'] = $shipping_amount
      ->getNumber();
  }

  // Send the tax amount.
  if (!$tax_amount
    ->isZero()) {
    $tax_amount = $this->rounder
      ->round($tax_amount);
    $nvp_data['PAYMENTREQUEST_0_TAXAMT'] = $tax_amount
      ->getNumber();
  }
  return $nvp_data;
}