You are here

function commerce_authnet_cim_cardonfile_update in Commerce Authorize.Net 7

Card on file callback: updates the associated customer payment profile.

1 string reference to 'commerce_authnet_cim_cardonfile_update'
commerce_authnet_commerce_payment_method_info in ./commerce_authnet.module
Implements hook_commerce_payment_method_info().

File

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

Code

function commerce_authnet_cim_cardonfile_update($form, &$form_state, $payment_method, $card_data) {

  // 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);
  if (!empty($form_state['values']['credit_card']['number']) && $form_state['values']['credit_card']['number'] != $form['credit_card']['number']['#default_value']) {
    $number = $form_state['values']['credit_card']['number'];
  }
  else {
    $number = 'XXXX' . $card_data->card_number;
  }

  // Load the payment profile so that billTo can be updated.
  $payment_profile_xml = commerce_authnet_cim_get_customer_payment_profile_request($payment_method, $cim_customer_profile_id, $cim_payment_profile_id);
  $billto = $payment_profile_xml->paymentProfile->billTo;
  $first_name = (string) $billto->firstName;
  $last_name = (string) $billto->lastName;

  // Extract the first and last name from form values.
  if (!empty($form_state['values']['credit_card']['owner'])) {
    $name_parts = explode(' ', $form_state['values']['credit_card']['owner']);
    $first_name = array_shift($name_parts);
    $last_name = implode(' ', $name_parts);
  }

  // Build the base profile update data.
  $api_request_data = array(
    'customerProfileId' => $cim_customer_profile_id,
    'paymentProfile' => array(
      'billTo' => array(
        'firstName' => substr($first_name, 0, 50),
        'lastName' => substr($last_name, 0, 50),
        'company' => (string) $billto->company,
        'address' => (string) $billto->address,
        'city' => (string) $billto->city,
        'state' => (string) $billto->state,
        'zip' => (string) $billto->zip,
        'country' => (string) $billto->country,
      ),
      'payment' => array(
        'creditCard' => array(
          'cardNumber' => $number,
          'expirationDate' => $card_data->card_exp_year . '-' . $card_data->card_exp_month,
        ),
      ),
      'customerPaymentProfileId' => $cim_payment_profile_id,
    ),
  );

  // Fetch the response from the API server and let Card on File know if the
  // update was successful.
  $xml_response = commerce_authnet_cim_request($payment_method, 'updateCustomerPaymentProfileRequest', $api_request_data);
  return (string) $xml_response->messages->message->code == 'I00001';
}