You are here

function _commerce_stripe_save_cardonfile in Commerce Stripe 7.3

Same name and namespace in other branches
  1. 7 commerce_stripe.module \_commerce_stripe_save_cardonfile()
  2. 7.2 commerce_stripe.module \_commerce_stripe_save_cardonfile()
2 calls to _commerce_stripe_save_cardonfile()
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 827
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_save_cardonfile($card, $uid, $payment_method, $set_default, $billing_profile = NULL) {

  // Store the Stripe customer and card ids in the remote id field of {commerce_cardonfile} table
  $remote_id = (string) $card->customer . '|' . (string) $card->id;

  // Populate and save the card
  $card_data = commerce_cardonfile_new();
  $card_data->uid = $uid;
  $card_data->payment_method = $payment_method['method_id'];
  $card_data->instance_id = $payment_method['instance_id'];
  $card_data->remote_id = $remote_id;
  $card_data->card_type = $card->brand;
  $card_data->card_name = $card->name;
  $card_data->card_number = $card->last4;
  $card_data->card_exp_month = $card->exp_month;
  $card_data->card_exp_year = $card->exp_year;
  $card_data->status = 1;
  $card_data->instance_default = $set_default;

  // Save our updated Card on file.
  $saved = commerce_cardonfile_save($card_data);

  // Associate the stored card with a billing profile, if one was given.
  if (!empty($billing_profile)) {
    $card_wrapper = entity_metadata_wrapper('commerce_cardonfile', $card_data);
    $card_wrapper->commerce_cardonfile_profile
      ->set($billing_profile);
    $card_wrapper
      ->save();
  }
  if (!empty($set_default)) {
    commerce_stripe_load_library();
    try {
      $customer = Stripe\Customer::retrieve($card->customer);
      $customer->default_source = $card->id;
      $customer
        ->save();
    } catch (Exception $e) {
      drupal_set_message('Could not set the card as default on Stripe.');
      return FALSE;
    }
  }
  watchdog('commerce_stripe', 'Stripe Customer Profile @profile_id created and saved to user @uid.', array(
    '@profile_id' => (string) $card->customer,
    '@uid' => $uid,
  ));
}