function commerce_paypal_ec_customer_profile in Commerce PayPal 7.2
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.
$response: The response array from a GetExpressCheckoutDetails API request.
$prefix: The prefix for keys in the response array that will be used to populate the customer profile.
$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_ec_customer_profile()
- commerce_paypal_ec_redirect_form_validate in modules/
ec/ commerce_paypal_ec.module - Payment method callback: redirect form return validation.
File
- modules/
ec/ commerce_paypal_ec.module, line 1408 - Implements PayPal Express Checkout in Drupal Commerce checkout.
Code
function commerce_paypal_ec_customer_profile($order, $profile_type, $response, $prefix, $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);
// Use the first name and last name if the profile is a billing profile.
if ($profile_type == 'billing') {
$address['first_name'] = $response['FIRSTNAME'];
$address['last_name'] = $response['LASTNAME'];
$address['name_line'] = $address['first_name'] . ' ' . $address['last_name'];
$address['country'] = $response['COUNTRYCODE'];
}
elseif ($profile_type == 'shipping') {
// Map addressfield value keys to keys available in the response array.
$key_map = array(
'country' => 'SHIPTOCOUNTRYCODE',
'name_line' => 'SHIPTONAME',
'first_name' => NULL,
'last_name' => NULL,
'organisation_name' => NULL,
'administrative_area' => 'SHIPTOSTATE',
'sub_administrative_area' => NULL,
'locality' => 'SHIPTOCITY',
'dependent_locality' => NULL,
'postal_code' => 'SHIPTOZIP',
'thoroughfare' => 'SHIPTOSTREET',
'premise' => 'SHIPTOSTREET2',
'sub_premise' => NULL,
'data' => NULL,
);
// Loop over the addressfield value array looking for values in the response
// array that match parts of the address to update.
foreach ($address as $key => &$value) {
// If there is no correlation for the current field key, skip it.
if (empty($key_map[$key])) {
continue;
}
// Update the addressfield value array with the value from the response
// array. Note that we will erase existing data if it isn't present in the
// response array.
$response_key = $prefix . $key_map[$key];
if (empty($response[$response_key])) {
$value = '';
}
else {
$value = $response[$response_key];
}
}
}
// 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();
}
}