You are here

function _commerce_amax_add_addresses in Commerce American Express Payment Gateway (Amex) 7

Add the addresses to the amex post data object

Parameters

object $data: The existing Amex data object.

object $order: The order with the addresses.

Return value

object $data The Amex data object with added addresses.

2 calls to _commerce_amax_add_addresses()
commerce_amex_cardonfile_charge in ./commerce_amex.module
Card on file callback: background charge payment
_commerce_amex_process_transaction in ./commerce_amex.module

File

./commerce_amex.module, line 1839
Implements American Express payment gateway for use in Drupal Commerce.

Code

function _commerce_amax_add_addresses($data, $order) {

  // Load customer profile.
  $profile = commerce_customer_profile_load($order->commerce_customer_billing[LANGUAGE_NONE][0]['profile_id']);

  // Get user billing address.
  $billing_address = $profile->commerce_customer_address[LANGUAGE_NONE][0];
  if (isset($order->data['profiles']['customer_profile_shipping'])) {

    // Load customer delivery profile.
    $profile = commerce_customer_profile_load($order->data['profiles']['customer_profile_shipping']);

    // Get user delivery address.
    $delivery_address = $profile->commerce_customer_address[LANGUAGE_NONE][0];
  }
  elseif (isset($order->field_customer_shipping[LANGUAGE_NONE][0]['profile_id'])) {
    $profile = commerce_customer_profile_load($order->field_customer_shipping[LANGUAGE_NONE][0]['profile_id']);

    // Get user delivery address.
    $delivery_address = $profile->commerce_customer_address[LANGUAGE_NONE][0];
  }
  else {
    $delivery_address = $billing_address;
  }
  $billing_address['country'] = countries_country_lookup($billing_address['country'], 'iso2');
  $data->billing['address']['street'] = $billing_address['premise'] . ' ' . $billing_address['thoroughfare'];
  $data->billing['address']['postcodeZip'] = $billing_address['postal_code'];
  if ($billing_address['locality'] != '') {
    $data->billing['address']['city'] = $billing_address['locality'];
  }
  if ($billing_address['administrative_area'] != '') {
    $data->billing['address']['stateProvince'] = $billing_address['administrative_area'];
  }
  $data->billing['address']['country'] = $billing_address['country']->iso3;
  $delivery_address['country'] = countries_country_lookup($delivery_address['country'], 'iso2');
  $data->shipping['firstName'] = $delivery_address['first_name'];
  $data->shipping['lastName'] = $delivery_address['last_name'];
  $data->shipping['address']['street'] = $delivery_address['premise'] . ' ' . $delivery_address['thoroughfare'];
  $data->shipping['address']['postcodeZip'] = $delivery_address['postal_code'];
  if ($delivery_address['locality'] != '') {
    $data->shipping['address']['city'] = $delivery_address['locality'];
  }
  if ($delivery_address['administrative_area'] != '') {
    $data->shipping['address']['stateProvince'] = $delivery_address['administrative_area'];
  }
  $data->shipping['address']['country'] = $delivery_address['country']->iso3;
  return $data;
}