Commit 1e9298f6 authored by jsacksick's avatar jsacksick

Issue #3043107 by jsacksick: Add logic for updating the billing/shipping customer profiles.

parent b6854951
......@@ -616,6 +616,15 @@ function commerce_paypal_checkout_redirect_form_validate($order, $payment_method
if (empty($order->mail)) {
$order->mail = $payer['email_address'];
}
// Create a billing information profile for the order with the available info.
if (!empty($payment_method['settings']['update_billing_profiles'])) {
commerce_paypal_checkout_customer_profile($order, 'billing', $paypal_order);
}
// If the shipping module exists on the site, create a shipping information
// profile for the order with the available info.
if (module_exists('commerce_shipping') && !empty($payment_method['settings']['update_shipping_profiles'])) {
commerce_paypal_checkout_customer_profile($order, 'shipping', $paypal_order);
}
// Recalculate the price of products on the order in case taxes have
// changed or prices have otherwise been affected.
......@@ -707,3 +716,115 @@ function commerce_paypal_checkout_do_payment($order, $payment_method) {
commerce_payment_transaction_save($transaction);
return TRUE;
}
/**
* Creates or updates a customer profile for an order based on information
* obtained from PayPal after a payment via PayPal Checkout.
*
* @param $order
* The order that was paid via PayPal Checkout.
* @param $profile_type
* The type of the customer profile that should be created or updated.
* @param array $paypal_order
* The PayPal order retrieved via the getOrder() API call.
* @param $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.
*/
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'];
}
}
// Otherwise if it's a shipping profile, populate the address with all of
// the shipping information returned from PayPal.
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 = [
'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();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment