You are here

function _commerce_stripe_create_card in Commerce Stripe 7.3

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

Call Stripe to create card for a user.

Parameters

string $stripe_token: Token of the card.

int $uid: User identifier of the card owner.

array $payment_method: Array containing payment_method informations.

Return value

bool

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 778
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, $account, $payment_method, $throw_exceptions = FALSE) {
  if (!commerce_stripe_load_library()) {
    return FALSE;
  }

  // If there is no existing customer id, use the Stripe form token to create one.
  $stripe_customer_id = commerce_stripe_customer_id($account->uid, $payment_method['instance_id']);
  if (!$stripe_customer_id) {
    try {
      $customer = Stripe\Customer::create(array(
        'email' => $account->mail,
        'description' => t('Customer for !mail', array(
          '!mail' => $account->mail,
        )),
        'source' => $stripe_token,
      ));
      $cards = Stripe\Customer::retrieve($customer->id)->sources
        ->all(array(
        'object' => 'card',
      ));
      $cards_array = Stripe\Util\Util::convertStripeObjectToArray(array(
        $cards,
      ));
      foreach ($cards_array[0]['data'] as $card) {
        return (object) $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);
      if ($throw_exceptions) {
        throw $e;
      }
      return FALSE;
    }
  }
  else {
    try {
      $customer = Stripe\Customer::retrieve($stripe_customer_id);
      $card = $customer->sources
        ->create(array(
        'source' => $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);
      if ($throw_exceptions) {
        throw $e;
      }
      return FALSE;
    }
  }
}