You are here

function commerce_cardonfile_delete_form_submit in Commerce Card on File 7

Same name and namespace in other branches
  1. 7.2 includes/commerce_cardonfile.pages.inc \commerce_cardonfile_delete_form_submit()

Form submit handler: delete stored card data.

File

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

Code

function commerce_cardonfile_delete_form_submit($form, &$form_state) {
  $card_data = $form_state['values']['card_data'];

  // Invoke the payment method's card delete callback.
  $payment_method = commerce_payment_method_instance_load($card_data['instance_id']);
  $callback = $payment_method['cardonfile']['delete 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 delete callback.
      drupal_set_message(t('We encountered an error attempting to delete your card data. Please try again and contact us if this error persists.'), 'error');
      $form_state['redirect'] = 'user/' . $card_data['uid'] . '/stored-payment-methods';
      return;
    }
  }

  // Disable the card data but retain the record.
  $card_data['status'] = 0;
  commerce_cardonfile_data_save($card_data);
  drupal_set_message(t('The stored card data has been deleted.'));

  // 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'];
  }
}