You are here

function _stripe_customer_create in Stripe 7

Create a new customer object on Stripe's servers for a Drupal account.

Parameters

object $account: The Drupal user we are creating a customer for.

Return value

object Instance of \Stripe\Customer

1 call to _stripe_customer_create()
stripe_create_customer in stripe_customer/stripe_customer.module
Create a new Stripe customer for a user, or return one if it already exists.

File

stripe_customer/stripe_customer.module, line 145
Provides integration with Stripe and Drupal Users as Customers.

Code

function _stripe_customer_create($account, $extra = array()) {
  stripe_load_library();

  // Add properties to the Stripe customer and allow modules to add their own.
  $params = module_invoke_all('stripe_customer_info', $account, $extra);
  try {
    $customer = \Stripe\Customer::create($params);
    if ($customer instanceof \Stripe\Customer) {

      // Save the newly-created customer's info before returning it.
      $insert = new stdClass();
      $insert->uid = $account->uid;
      $insert->customer_id = $customer->id;
      $insert->livemode = $customer->livemode;
      $insert->created = $customer->created;
      $insert->changed = REQUEST_TIME;
      $insert->default_source = $customer->default_source;
      $insert->currency = $customer->currency;
      $saved = drupal_write_record('stripe_customers', $insert);
      if ($saved !== FALSE) {

        // Notify Rules that a customer has been created.
        if (module_exists('rules')) {
          rules_invoke_all('stripe_customer_created', $customer, $account);
        }
        return $customer;
      }
    }
  } catch (Exception $e) {
    drupal_set_message(t('Could not create Customer. Reason: @error', array(
      '@error' => $e
        ->getMessage(),
    )), 'error');
  }
}