You are here

function commerce_stripe_cardonfile_update in Commerce Stripe 7.3

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

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

1 call to commerce_stripe_cardonfile_update()
commerce_stripe_cardonfile_update_form_submit in ./commerce_stripe.module
1 string reference to 'commerce_stripe_cardonfile_update'
commerce_stripe_commerce_payment_method_info_alter in ./commerce_stripe.module
Implements hook_commerce_payment_method_info_alter().

File

./commerce_stripe.module, line 1469
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_update($form, &$form_state, $payment_method, $card_data) {
  if (!commerce_stripe_load_library()) {
    return FALSE;
  }
  $account = $form_state['values']['user'];

  // Fetch the customer id and card id from $card_data->remote_id
  list($customer_id, $card_id) = explode('|', $card_data->remote_id);
  try {
    $customer = Stripe\Customer::retrieve($customer_id);
    $card = $customer->sources
      ->retrieve($card_id);

    // If we are updating the default in Cardonfile, update it in Stripe.
    if ($card_data->instance_default != $form_state['values']['credit_card']['cardonfile_instance_default']) {
      $customer->default_source = $card->id;
    }

    // Update our record in Cardonfile.
    $card_data->card_name = check_plain($form_state['values']['credit_card']['owner']);
    $card_data->card_exp_month = $form_state['values']['credit_card']['exp_month'];
    $card_data->card_exp_year = $form_state['values']['credit_card']['exp_year'];
    $card_data->instance_default = $form_state['values']['credit_card']['cardonfile_instance_default'];
    $card_data->status = 1;
    $langcode = $form['commerce_customer_address']['#language'];

    // If we're using Checkout integration, and the owner field is an email address,
    // this is probably not wanted; use the Name Line from the billing profile instead.
    if ($payment_method['settings']['integration_type'] == 'checkout' && valid_email_address($form_state['values']['credit_card']['owner']) && !empty($form_state['values']['commerce_customer_address'][$langcode][0]['name_line'])) {
      $card_data->card_name = check_plain($form_state['values']['commerce_customer_address'][$langcode][0]['name_line']);
    }

    // Update our record in Stripe.
    $card->exp_month = $card_data->card_exp_month;
    $card->exp_year = $card_data->card_exp_year;
    $card->name = $card_data->card_name;
    $card
      ->save();

    // Update the associated billing address and associate it with the card on file.
    $billing_profile = NULL;
    if (isset($form_state['values']['commerce_customer_profile'])) {
      $billing_profile = $form_state['values']['commerce_customer_profile'];
      $billing_profile->status = TRUE;

      // Set the profile's uid if it's being created at this time.
      if (empty($billing_profile->profile_id)) {
        $billing_profile->uid = $account->uid;
      }

      // Notify field widgets.
      field_attach_submit('commerce_customer_profile', $billing_profile, $form, $form_state);

      // Update the profile.
      commerce_customer_profile_save($billing_profile);
    }
    $card_wrapper = entity_metadata_wrapper('commerce_cardonfile', $card_data);

    // Associate the stored card with a billing profile, if one was given.
    if (!empty($billing_profile)) {
      $card_wrapper->commerce_cardonfile_profile
        ->set($billing_profile);
    }
    $card_wrapper
      ->save();

    // If the user set this to their default in Card on File, update it in Stripe.
    if (!empty($card_data->instance_default)) {
      $customer->default_source = $card->id;
      $customer
        ->save();
    }
    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 updating card @stripe_error', array(
      '@stripe_error' => $e
        ->getMessage(),
    ), WATCHDOG_NOTICE);
    return FALSE;
  }
}