You are here

function commerce_paypal_checkout_customer_profile in Commerce PayPal 7.2

Creates or updates a customer profile for an order based on information obtained from PayPal after a payment via PayPal Checkout.

Parameters

$order: The order that was paid via PayPal Checkout.

$profile_type: The type of the customer profile that should be created or updated.

array $paypal_order: The PayPal order retrieved via the getOrder() API call.

$skip_save: Boolean indicating whether or not this function should skip saving the order after setting it to reference the newly created customer profile; defaults to TRUE, requiring the caller to save the order.

1 call to commerce_paypal_checkout_customer_profile()
commerce_paypal_checkout_redirect_form_validate in modules/checkout/commerce_paypal_checkout.module
Payment method callback: redirect form return validation.

File

modules/checkout/commerce_paypal_checkout.module, line 1087
Implements PayPal Checkout in Drupal Commerce checkout.

Code

function commerce_paypal_checkout_customer_profile($order, $profile_type, $paypal_order, $skip_save = TRUE) {

  // First check if the order already references a customer profile of the
  // specified type.
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $field_name = variable_get('commerce_customer_profile_' . $profile_type . '_field', '');

  // If the associated order field has been set and the order currently
  // references a customer profile through it...
  if (!empty($field_name) && !empty($order_wrapper->{$field_name})) {

    // Update the existing customer profile.
    $profile = $order_wrapper->{$field_name}
      ->value();
  }
  elseif (!empty($order->data['profiles']['customer_profile_' . $profile_type])) {

    // Otherwise look for an association stored in the order's data array.
    $profile = commerce_customer_profile_load($order->data['profiles']['customer_profile_' . $profile_type]);
  }

  // Create a new profile if we could not find an existing one.
  if (empty($profile)) {
    $profile = commerce_customer_profile_new($profile_type, $order->uid);
  }

  // Add the order context to the profile to ensure it can be updated without
  // resulting in customer profile duplication.
  $profile->entity_context = array(
    'entity_type' => 'commerce_order',
    'entity_id' => $order->order_id,
  );

  // Prepare an addressfield array to set to the customer profile.
  $field = field_info_field('commerce_customer_address');
  $instance = field_info_instance('commerce_customer_profile', 'commerce_customer_address', $profile_type);
  $address = addressfield_default_values($field, $instance);
  $paypal_address = array();

  // Use the first name and last name if the profile is a billing profile.
  if ($profile_type == 'billing') {
    $address['first_name'] = $paypal_order['payer']['name']['given_name'];
    $address['last_name'] = $paypal_order['payer']['name']['surname'];
    $address['name_line'] = $address['first_name'] . ' ' . $address['last_name'];
    if (isset($paypal_order['payer']['address'])) {
      $paypal_address = $paypal_order['payer']['address'];
    }
  }
  elseif ($profile_type == 'shipping') {

    // If no shipping info was returned by PayPal.
    if (empty($paypal_order['purchase_units'][0]['shipping'])) {
      return;
    }
    $shipping_info = $paypal_order['purchase_units'][0]['shipping'];
    $names = explode(' ', $shipping_info['name']['full_name']);
    $first_name = array_shift($names);
    $last_name = implode(' ', $names);
    $address['first_name'] = $first_name;
    $address['last_name'] = $last_name;
    $address['name_line'] = $first_name . ' ' . $last_name;
    if (isset($shipping_info['address'])) {
      $paypal_address = $shipping_info['address'];
    }
  }
  if ($paypal_address) {

    // Map PayPal address keys to Address field keys.
    $mapping = array(
      'address_line_1' => 'thoroughfare',
      'address_line_2' => 'premise',
      'admin_area_1' => 'administrative_area',
      'admin_area_2' => 'locality',
      'postal_code' => 'postal_code',
      'country_code' => 'country',
    );
    foreach ($paypal_address as $key => $value) {
      if (!isset($mapping[$key])) {
        continue;
      }

      // PayPal address fields have a higher maximum length than ours.
      $value = $key == 'country_code' ? $value : mb_substr($value, 0, 255);
      $address[$mapping[$key]] = $value;
    }
  }

  // Add the addressfield value to the customer profile.
  $profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile);
  $profile_wrapper->commerce_customer_address = $address;

  // Save the customer profile and update the order to reference it.
  $profile_wrapper
    ->save();
  $order_wrapper->{'commerce_customer_' . $profile_type} = $profile_wrapper;

  // Save the order if specified.
  if (!$skip_save) {
    $order_wrapper
      ->save();
  }
}