You are here

protected function Checkout::updateProfile in Commerce PayPal 8

Updates the profile of the given type using the response returned by PayPal.

Parameters

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

string $type: The type (billing|profile).

array $paypal_order: The PayPal order.

1 call to Checkout::updateProfile()
Checkout::onReturn in src/Plugin/Commerce/PaymentGateway/Checkout.php
Processes the "return" request.

File

src/Plugin/Commerce/PaymentGateway/Checkout.php, line 780

Class

Checkout
Provides the PayPal Checkout payment gateway.

Namespace

Drupal\commerce_paypal\Plugin\Commerce\PaymentGateway

Code

protected function updateProfile(OrderInterface $order, $type, array $paypal_order) {
  if ($type == 'billing') {

    /** @var \Drupal\profile\Entity\ProfileInterface $profile */
    $profile = $order
      ->getBillingProfile() ?: $this
      ->buildCustomerProfile($order);
    $profile->address->given_name = $paypal_order['payer']['name']['given_name'];
    $profile->address->family_name = $paypal_order['payer']['name']['surname'];
    if (isset($paypal_order['payer']['address'])) {
      $this
        ->populateProfile($profile, $paypal_order['payer']['address']);
    }
    $profile
      ->save();
    $order
      ->setBillingProfile($profile);
  }
  elseif ($type == 'shipping' && !empty($paypal_order['purchase_units'][0]['shipping'])) {
    $shipping_info = $paypal_order['purchase_units'][0]['shipping'];
    $shipments = $order->shipments
      ->referencedEntities();
    if (!$shipments) {

      /** @var \Drupal\commerce_shipping\PackerManagerInterface $packer_manager */
      $packer_manager = \Drupal::service('commerce_shipping.packer_manager');
      list($shipments) = $packer_manager
        ->packToShipments($order, $this
        ->buildCustomerProfile($order), $shipments);
    }

    // Can't proceed without shipments.
    if (!$shipments) {
      return;
    }

    /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface $first_shipment */
    $first_shipment = $shipments[0];

    /** @var \Drupal\profile\Entity\ProfileInterface $profile */
    $profile = $first_shipment
      ->getShippingProfile() ?: $this
      ->buildCustomerProfile($order);

    // This is a hack but shipments with empty amounts is crashing other
    // contrib modules.
    // Ideally, we shouldn't have to pack the shipments ourselves...
    if (!$first_shipment
      ->getAmount()) {
      $shipment_amount = Price::fromArray([
        'number' => 0,
        'currency_code' => $order
          ->getTotalPrice()
          ->getCurrencyCode(),
      ]);
      $first_shipment
        ->setAmount($shipment_amount);
    }

    // We only get the full name from PayPal, so we need to "guess" the given
    // name and the family name.
    $names = explode(' ', $shipping_info['name']['full_name']);
    $given_name = array_shift($names);
    $family_name = implode(' ', $names);
    $profile->address->given_name = $given_name;
    $profile->address->family_name = $family_name;
    if (!empty($shipping_info['address'])) {
      $this
        ->populateProfile($profile, $shipping_info['address']);
    }
    $profile
      ->save();
    $first_shipment
      ->setShippingProfile($profile);
    $first_shipment
      ->save();
    $order
      ->set('shipments', $shipments);
  }
}