You are here

function commerce_cardonfile_update_form_submit in Commerce Card on File 7

Form submit handler: update stored card data.

File

includes/commerce_cardonfile.pages.inc, line 112
User page callbacks and forms for Commerce Card on File.

Code

function commerce_cardonfile_update_form_submit($form, &$form_state) {

  // Update the card data with items from the form.
  $card_data = $form_state['values']['card_data'];
  $card_data['card_name'] = $form_state['values']['credit_card']['owner'];

  // Only update the last 4 if we were given a new valid credit card number.
  if ($form_state['values']['credit_card']['number'] != $form['credit_card']['number']['#default_value']) {
    $card_data['card_number'] = substr($form_state['values']['credit_card']['number'], -4);
  }
  $card_data['card_exp_month'] = $form_state['values']['credit_card']['exp_month'];
  $card_data['card_exp_year'] = $form_state['values']['credit_card']['exp_year'];

  // Invoke the payment method's card update callback.
  $payment_method = commerce_payment_method_instance_load($card_data['instance_id']);
  $callback = $payment_method['cardonfile']['update callback'];
  if (function_exists($callback)) {
    if (!$callback($form, $form_state, $payment_method, $card_data)) {

      // Display a message if we failed to communicate properly with the payment
      // gateway in the Card on File update callback.
      drupal_set_message(t('We encountered an error attempting to update your card data. Please try again and contact us if this error persists.'), 'error');
      $form_state['rebuild'] = TRUE;
      return;
    }
  }
  commerce_cardonfile_data_save($card_data);
  drupal_set_message(t('The stored card data has been updated.'));

  // Redirect to the payment methods tab if the user has other stored payment.
  $stored_cards = commerce_cardonfile_data_load_multiple($card_data['uid']);
  if (!empty($stored_cards)) {
    $form_state['redirect'] = 'user/' . $card_data['uid'] . '/stored-payment-methods';
  }
  else {
    $form_state['redirect'] = 'user/' . $card_data['uid'];
  }
}