You are here

function commerce_stripe_cardonfile_delete in Commerce Stripe 7

Same name and namespace in other branches
  1. 7.3 commerce_stripe.module \commerce_stripe_cardonfile_delete()
  2. 7.2 commerce_stripe.module \commerce_stripe_cardonfile_delete()

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

1 string reference to 'commerce_stripe_cardonfile_delete'
commerce_stripe_commerce_payment_method_info in ./commerce_stripe.module
Implements hook_commerce_payment_method_info().

File

./commerce_stripe.module, line 1059
This module provides Stripe (http://stripe.com/) payment gateway integration to Commerce. Commerce Stripe offers a PCI-compliant way to process payments straight from you Commerce shop.

Code

function commerce_stripe_cardonfile_delete($form, &$form_state, $payment_method, $card_data) {
  if (!commerce_stripe_load_library()) {
    return FALSE;
  }

  // Fetch the customer id and card id from $card_data->remote_id
  list($customer_id, $card_id) = explode('|', $card_data->remote_id);
  Stripe::setApiKey($payment_method['settings']['secret_key']);
  try {
    $customer = Stripe_Customer::retrieve($customer_id);
    if (property_exists($customer, 'card')) {
      $customer->cards
        ->retrieve($card_id)
        ->delete();
    }
    else {
      $customer->sources
        ->retrieve($card_id)
        ->delete();
    }
    return TRUE;
  } catch (Exception $e) {
    drupal_set_message(t('We received the following error processing your card: %error. Please enter your information again or try a different card.', array(
      '%error' => $e
        ->getMessage(),
    )), 'error');
    watchdog('commerce_stripe', 'Following error received when deleting card @stripe_error.', array(
      '@stripe_error' => $e
        ->getMessage(),
    ), WATCHDOG_NOTICE);
    return FALSE;
  }
}