You are here

function commerce_braintree_dropin_cardonfile_delete in Commerce Braintree 7.2

Same name and namespace in other branches
  1. 7.3 modules/commerce_braintree_dropin/commerce_braintree_dropin.module \commerce_braintree_dropin_cardonfile_delete()

Delete callback for commerce_cardonfile.

File

modules/commerce_braintree_dropin/commerce_braintree_dropin.module, line 390
Provides integration with Braintree Drop-in UI.

Code

function commerce_braintree_dropin_cardonfile_delete($form, &$form_state, $payment_method, $card) {
  commerce_braintree_initialize($payment_method);
  try {
    Braintree_PaymentMethod::delete($card->remote_id);
  } catch (Exception $ex) {
    watchdog('commerce_braintree_dropin', 'Unable to delete payment method due to @error', array(
      '@error' => $ex
        ->getMessage(),
    ), WATCHDOG_ERROR);
    drupal_set_message(t('Unable to delete the payment method at this time'), 'error');
  }

  // If the card being deleted is the instance default, attempt to update
  // another card owned by the the same user as the instance default.
  if ($card->instance_default) {
    $cards = commerce_cardonfile_load_multiple_by_uid($card->uid, $payment_method);
    unset($cards[$card->card_id]);
    if (!empty($cards)) {
      $new_default = reset($cards);
      $new_default->instance_default = TRUE;
      commerce_cardonfile_save($new_default);
    }
  }
  commerce_cardonfile_delete($card->card_id);
  return TRUE;
}