You are here

function commerce_authnet_form_commerce_cardonfile_update_form_alter in Commerce Authorize.Net 7

Implements hook_form_FORM_ID_alter().

File

./commerce_authnet.module, line 785
Implements Authorize.Net payment services for use in Drupal Commerce.

Code

function commerce_authnet_form_commerce_cardonfile_update_form_alter(&$form, &$form_state) {

  // Extract the card data from the form and load the payment method instance.
  $card_data = $form['card_data']['#value'];
  $payment_method = commerce_payment_method_instance_load($card_data->instance_id);

  // If this is not an Authorize.Net card then bail.
  if ($payment_method['method_id'] != 'authnet_aim') {
    return;
  }

  // Extract the Customer Profile and Payment Profile IDs from the remote_id.
  list($cim_customer_profile_id, $cim_payment_profile_id) = explode('|', $card_data->remote_id);

  // Load the full payment profile from Authorize.Net.
  $payment_profile_xml = commerce_authnet_cim_get_customer_payment_profile_request($payment_method, $cim_customer_profile_id, $cim_payment_profile_id);
  if ($payment_profile_xml->messages->message->code == 'I00001') {
    $billto = $payment_profile_xml->paymentProfile->billTo;
    $address = array(
      '<strong>' . t('Billing address:') . '</strong>',
      (string) $billto->firstName . ' ' . (string) $billto->lastName,
      (string) $billto->company,
      (string) $billto->address,
      (string) $billto->city . ', ' . (string) $billto->state . ' ' . (string) $billto->zip,
      (string) $billto->country,
    );

    // Add the address info to the form.
    $form['billto'] = array(
      '#type' => 'markup',
      '#markup' => '<div class="commerce-authnet-cim-billto">' . implode('<br />', array_diff($address, array(
        '',
      ))) . '</div>',
      '#weight' => -50,
    );
    $form_state['billto_xml'] = $billto;
  }
}