You are here

function commerce_cardonfile_card_form_submit in Commerce Card on File 7.2

Submit callback for commerce_cardonfile_card_form().

File

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

Code

function commerce_cardonfile_card_form_submit($form, &$form_state) {
  $op = $form_state['op'];
  $card = $form_state['card'];
  $card->card_name = $form_state['values']['credit_card']['owner'];
  $card->card_exp_month = $form_state['values']['credit_card']['exp_month'];
  $card->card_exp_year = $form_state['values']['credit_card']['exp_year'];
  if ($op == 'create') {
    $card->card_number = substr($form_state['values']['credit_card']['number'], -4);
    $card->card_type = $form_state['values']['credit_card']['type'];
  }
  $card->instance_default = $form_state['values']['credit_card']['cardonfile_instance_default'];

  // Invoke the payment method's card create/update callback.
  $payment_method = commerce_payment_method_instance_load($card->instance_id);
  $callback = $payment_method['cardonfile'][$op . ' callback'];
  $success = FALSE;
  if (function_exists($callback)) {
    $callback_return = $callback($form, $form_state, $payment_method, $card);
    if ($callback_return) {
      if ($op == 'create') {
        $card_save = $callback_return;
        $confirm_message = t('A new card has been added.');
      }
      else {
        $card_save = $card;
        $confirm_message = t('The card has been updated.');
      }
      commerce_cardonfile_save($card_save);
      drupal_set_message($confirm_message);
      $success = TRUE;
    }
  }
  if (!$success) {
    if ($op == 'create') {
      drupal_set_message(t('We encountered an error attempting to save your card data. Please try again and contact us if this error persists.'), 'error');
    }
    else {
      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['redirect'] = 'user/' . $card->uid . '/cards';
}