You are here

protected function CheckoutController::extractAddress in Commerce PayPal 8

Extracts the billing address from the request body.

Parameters

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

\Symfony\Component\HttpFoundation\Request $request: The request.

Return value

\Drupal\address\AddressInterface|null The address, NULL if empty.

1 call to CheckoutController::extractAddress()
CheckoutController::onCreate in src/Controller/CheckoutController.php
Create/update the order in PayPal.

File

src/Controller/CheckoutController.php, line 201

Class

CheckoutController
PayPal checkout controller.

Namespace

Drupal\commerce_paypal\Controller

Code

protected function extractAddress(OrderInterface $order, Request $request) {
  $body = Json::decode($request
    ->getContent());

  // If the "profile copy" checkbox is checked, attempt to use the
  // shipping profile as the source of the address.
  if (!empty($body['profileCopy'])) {
    $profiles = $order
      ->collectProfiles();
    if (isset($profiles['shipping']) && !$profiles['shipping']
      ->get('address')
      ->isEmpty()) {
      return $profiles['shipping']
        ->get('address')
        ->first();
    }
  }

  /** @var \Drupal\profile\ProfileStorageInterface $profile_storage */
  $profile_storage = $this->entityTypeManager
    ->getStorage('profile');
  if (!empty($body['profile'])) {

    // When "_original" is passed, attempt to load/use the default profile.
    if ($body['profile'] === '_original') {
      $profile = $profile_storage
        ->loadByUser($order
        ->getCustomer(), 'customer');
    }
    else {
      $profile = $profile_storage
        ->load($body['profile']);
    }
    if ($profile && $profile
      ->access('view') && !$profile
      ->get('address')
      ->isEmpty()) {
      return $profile
        ->get('address')
        ->first();
    }
  }
  elseif (!empty($body['address'])) {
    $profile = $profile_storage
      ->create([
      'type' => 'customer',
      'address' => $body['address'],
    ]);
    return $profile
      ->get('address')
      ->first();
  }
  return NULL;
}