You are here

function _stripe_customer_update in Stripe 7

Update a Stripe customer on Stripe's servers.

1 call to _stripe_customer_update()
stripe_update_customer in stripe_customer/stripe_customer.module
Wrapper around updating a customer object.

File

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

Code

function _stripe_customer_update(\Stripe\Customer $customer) {
  try {
    stripe_load_library();

    // Load the user based on the uid associated with this customer ID.
    $user = user_load(stripe_customer_get_uid($customer->id));

    // Allow other modules to alter the parameters for this customer.
    $properties = module_invoke_all('stripe_customer', $user);
    if (!empty($properties)) {
      foreach ($properties as $property => $value) {
        $customer->{$property} = $value;
      }
    }

    // Save it at Stripe and update our database record accordingly.
    $customer
      ->save();

    // We don't need to update every value, just the relevant ones.
    db_merge('stripe_customers')
      ->key(array(
      'uid' => $user->uid,
      'customer_id' => $customer->id,
    ))
      ->fields(array(
      'uid' => $user->uid,
      'changed' => REQUEST_TIME,
      'default_source' => $customer->default_source,
      'currency' => $customer->currency,
    ))
      ->execute();

    // Notify Rules of this update event.
    if (module_exists('rules')) {
      rules_invoke_all('stripe_customer_updated', $customer, $user);
    }
  } catch (\Stripe\Error\InvalidRequest $e) {
    drupal_set_message(t('Could not update customer :id. Reason: :error', array(
      ':id' => $customer->id,
      ':error' => $e
        ->getMessage(),
    )), 'error');
  }
}