You are here

protected function CheckoutSdk::prepareOrderRequest in Commerce PayPal 8

Prepare the order request parameters.

Parameters

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

\Drupal\address\AddressInterface $billing_address: (optional) A billing address to pass to PayPal as the payer information. This is used in checkout to pass the entered address that is not yet submitted and associated to the order.

Return value

array An array suitable for use in the create|update order API calls.

2 calls to CheckoutSdk::prepareOrderRequest()
CheckoutSdk::createOrder in src/CheckoutSdk.php
Creates an order in PayPal.
CheckoutSdk::updateOrder in src/CheckoutSdk.php
Updates an existing PayPal order.

File

src/CheckoutSdk.php, line 236

Class

CheckoutSdk
Provides a replacement of the PayPal SDK.

Namespace

Drupal\commerce_paypal

Code

protected function prepareOrderRequest(OrderInterface $order, AddressInterface $billing_address = NULL) {
  $items = [];
  $item_total = NULL;
  $tax_total = NULL;
  foreach ($order
    ->getItems() as $order_item) {
    $item_total = $item_total ? $item_total
      ->add($order_item
      ->getTotalPrice()) : $order_item
      ->getTotalPrice();
    $item = [
      'name' => mb_substr($order_item
        ->getTitle(), 0, 127),
      'unit_amount' => [
        'currency_code' => $order_item
          ->getUnitPrice()
          ->getCurrencyCode(),
        'value' => Calculator::trim($order_item
          ->getUnitPrice()
          ->getNumber()),
      ],
      'quantity' => intval($order_item
        ->getQuantity()),
    ];
    $purchased_entity = $order_item
      ->getPurchasedEntity();
    if ($purchased_entity instanceof ProductVariationInterface) {
      $item['sku'] = mb_substr($purchased_entity
        ->getSku(), 0, 127);
    }
    $items[] = $item;
  }

  // Now, pass adjustments that are not "supported" by PayPal such as fees
  // and "custom" adjustments.
  // We could pass fees under "handling", but we can't make that assumption.
  $adjustments = $order
    ->collectAdjustments();
  $adjustments = $this->adjustmentTransformer
    ->processAdjustments($adjustments);
  foreach ($adjustments as $adjustment) {

    // Skip included adjustments and the adjustment types we're handling
    // below such as "shipping" and "tax".
    if ($adjustment
      ->isIncluded() || in_array($adjustment
      ->getType(), [
      'tax',
      'shipping',
      'promotion',
    ])) {
      continue;
    }
    $item_total = $item_total ? $item_total
      ->add($adjustment
      ->getAmount()) : $adjustment
      ->getAmount();
    $items[] = [
      'name' => mb_substr($adjustment
        ->getLabel(), 0, 127),
      'unit_amount' => [
        'currency_code' => $adjustment
          ->getAmount()
          ->getCurrencyCode(),
        'value' => Calculator::trim($adjustment
          ->getAmount()
          ->getNumber()),
      ],
      'quantity' => 1,
    ];
  }
  $breakdown = [
    'item_total' => [
      'currency_code' => $item_total
        ->getCurrencyCode(),
      'value' => Calculator::trim($item_total
        ->getNumber()),
    ],
  ];
  $tax_total = $this
    ->getAdjustmentsTotal($adjustments, [
    'tax',
  ]);
  if (!empty($tax_total)) {
    $breakdown['tax_total'] = [
      'currency_code' => $tax_total
        ->getCurrencyCode(),
      'value' => Calculator::trim($tax_total
        ->getNumber()),
    ];
  }
  $shipping_total = $this
    ->getAdjustmentsTotal($adjustments, [
    'shipping',
  ]);
  if (!empty($shipping_total)) {
    $breakdown['shipping'] = [
      'currency_code' => $shipping_total
        ->getCurrencyCode(),
      'value' => Calculator::trim($shipping_total
        ->getNumber()),
    ];
  }
  $promotion_total = $this
    ->getAdjustmentsTotal($adjustments, [
    'promotion',
  ]);
  if (!empty($promotion_total)) {
    $breakdown['discount'] = [
      'currency_code' => $promotion_total
        ->getCurrencyCode(),
      'value' => Calculator::trim($promotion_total
        ->multiply(-1)
        ->getNumber()),
    ];
  }
  $payer = [];
  if (!empty($order
    ->getEmail())) {
    $payer['email_address'] = $order
      ->getEmail();
  }
  $profiles = $order
    ->collectProfiles();
  if (!empty($billing_address)) {
    $payer += static::formatAddress($billing_address);
  }
  elseif (isset($profiles['billing'])) {

    /** @var \Drupal\address\AddressInterface $address */
    $address = $profiles['billing']->address
      ->first();
    if (!empty($address)) {
      $payer += static::formatAddress($address);
    }
  }
  $params = [
    'intent' => strtoupper($this->config['intent']),
    'purchase_units' => [
      [
        'reference_id' => 'default',
        'custom_id' => $order
          ->id(),
        'invoice_id' => $order
          ->id() . '-' . $this->time
          ->getRequestTime(),
        'amount' => [
          'currency_code' => $order
            ->getTotalPrice()
            ->getCurrencyCode(),
          'value' => Calculator::trim($order
            ->getTotalPrice()
            ->getNumber()),
          'breakdown' => $breakdown,
        ],
        'items' => $items,
      ],
    ],
    'application_context' => [
      'brand_name' => mb_substr($order
        ->getStore()
        ->label(), 0, 127),
    ],
  ];
  $shipping_address = [];
  if (isset($profiles['shipping'])) {

    /** @var \Drupal\address\AddressInterface $address */
    $address = $profiles['shipping']->address
      ->first();
    if (!empty($address)) {
      $shipping_address = static::formatAddress($address, 'shipping');
    }
  }
  $shipping_preference = $this->config['shipping_preference'];

  // The shipping module isn't enabled, override the shipping preference
  // configured.
  if (!$this->moduleHandler
    ->moduleExists('commerce_shipping')) {
    $shipping_preference = 'no_shipping';
  }
  else {

    // If no shipping address was already collected, override the shipping
    // preference to "GET_FROM_FILE" so that the shipping address is collected
    // on the PayPal site.
    if ($shipping_preference == 'set_provided_address' && !$shipping_address) {
      $shipping_preference = 'get_from_file';
    }
  }

  // No need to pass a shipping_address if the shipping address collection
  // is configured to "no_shipping".
  if ($shipping_address && $shipping_preference !== 'no_shipping') {
    $params['purchase_units'][0]['shipping'] = $shipping_address;
  }
  $params['application_context']['shipping_preference'] = strtoupper($shipping_preference);
  if ($payer) {
    $params['payer'] = $payer;
  }
  return $params;
}