You are here

function _commerce_stripe_create_card in Commerce Stripe 7

Same name and namespace in other branches
  1. 7.3 commerce_stripe.module \_commerce_stripe_create_card()
  2. 7.2 commerce_stripe.module \_commerce_stripe_create_card()
2 calls to _commerce_stripe_create_card()
commerce_stripe_cardonfile_create in ./commerce_stripe.module
Card on file callback: create
commerce_stripe_submit_form_submit in ./commerce_stripe.module
Payment method callback: checkout form submission.

File

./commerce_stripe.module, line 612
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_create_card($stripe_token, $uid, $payment_method) {
  if (!commerce_stripe_load_library()) {
    return FALSE;
  }
  Stripe::setApiKey($payment_method['settings']['secret_key']);

  // If there is no existing customer id, use the Stripe form token to create one.
  $stripe_customer_id = commerce_stripe_customer_id($uid, $payment_method['instance_id']);
  if (!$stripe_customer_id) {
    $account = user_load($uid);
    try {
      $customer = Stripe_Customer::create(array(
        'card' => $stripe_token,
        'email' => $account->mail,
      ));
      if (property_exists($customer, 'cards')) {
        foreach ($customer->cards->data as $card) {
          if ($card->id == $customer->default_card) {
            return $card;
          }
        }
      }
      else {
        foreach ($customer->sources->data as $card) {
          if ($card->object == 'card' && $card->id == $customer->default_source) {
            return $card;
          }
        }
      }
    } 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 creating Stripe customer: @stripe_error.', array(
        '@stripe_error' => $e
          ->getMessage(),
      ), WATCHDOG_NOTICE);
      return FALSE;
    }
  }
  else {
    try {
      $customer = Stripe_Customer::retrieve($stripe_customer_id);
      if (property_exists($customer, 'cards')) {
        $card = $customer->cards
          ->create(array(
          'card' => $stripe_token,
        ));
      }
      else {
        $card = $customer->sources
          ->create(array(
          'card' => $stripe_token,
        ));
      }
      return $card;
    } 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 adding a card to customer: @stripe_error.', array(
        '@stripe_error' => $e
          ->getMessage(),
      ), WATCHDOG_NOTICE);
      return FALSE;
    }
  }
}