You are here

function commerce_braintree_express_checkout_create_customer_profile in Commerce Braintree 7.3

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

Parameters

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

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

$payload: The payload array from a Express Checkout payload.

$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_braintree_express_checkout_create_customer_profile()
commerce_braintree_express_checkout_submit in modules/commerce_braintree_express_checkout/commerce_braintree_express_checkout.module
Form submit handler for Express Checkout payload submission.

File

modules/commerce_braintree_express_checkout/commerce_braintree_express_checkout.module, line 427
Provides integration PayPal Express Checkout for Braintree.

Code

function commerce_braintree_express_checkout_create_customer_profile($order, $profile_type, $payload, $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);

  // Handle setting the name fields for billing profiles.
  if ($profile_type == 'billing') {
    $address['first_name'] = $payload['details']['firstName'];
    $address['last_name'] = $payload['details']['lastName'];
    $address['name_line'] = $address['first_name'] . ' ' . $address['last_name'];
  }

  // Handle setting the name and address for shipping profiles.
  if ($profile_type == 'shipping' && !empty($payload['details']['shippingAddress'])) {
    $address_values = $payload['details']['shippingAddress'];

    // The recipient name is returned as one field. Attempt
    // to split the first and last names for address field.
    $names = explode(' ', $address_values['recipientName']);
    $address['first_name'] = array_shift($names);
    $address['last_name'] = implode(' ', $names);
    $address['name_line'] = $address_values['recipientName'];
    $address = array_merge($address, array(
      'country' => !empty($address_values['countryCode']) ? $address_values['countryCode'] : '',
      'administrative_area' => !empty($address_values['state']) ? $address_values['state'] : '',
      'locality' => !empty($address_values['city']) ? $address_values['city'] : '',
      'postal_code' => !empty($address_values['postalCode']) ? $address_values['postalCode'] : '',
      'thoroughfare' => !empty($address_values['line1']) ? $address_values['line1'] : '',
      'premise' => !empty($address_values['line2']) ? $address_values['line2'] : '',
    ));
  }

  // 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();
  }
}