You are here

function stripe_libraries_postload_callback in Stripe 7

Post-load callback for the Stripe PHP Library.

Sets the API key and, if configured, API Version.

Parameters

array $library: An array of library information.

string $version: If the $library array belongs to a certain version, a string containing the version.

string $variant: If the $library array belongs to a certain variant, a string containing the variant name.

1 string reference to 'stripe_libraries_postload_callback'
stripe_libraries_info in ./stripe.module
Implements hook_libraries_info().

File

./stripe.module, line 398
stripe.module Drupal hooks used for integrating the Stripe service.

Code

function stripe_libraries_postload_callback($library, $version = NULL, $variant = NULL) {
  $secret_key = variable_get('stripe_secret', '');

  // Only secret keys (not publishable) can call setApiVersion().
  try {
    \Stripe\Stripe::setApiKey($secret_key);
  } catch (\Stripe\Error\RateLimit $e) {

    // Too many requests made to the API too quickly.
    _stripe_error('Stripe', 'Stripe RateLimit was hit.');
  } catch (\Stripe\Error\InvalidRequest $e) {

    // Invalid parameters were supplied to Stripe's API,
    _stripe_error(t('Invalid Stripe request: %msg', array(
      '%msg' => $e
        ->getMessage(),
    )));
  } catch (\Stripe\Error\Authentication $e) {

    // Authentication with Stripe's API failed.
    _stripe_error(t('Could not authenticate to Stripe. Reason: %msg', array(
      '%msg' => $e
        ->getMessage(),
    )));
  } catch (\Stripe\Error\ApiConnection $e) {

    // Network communication with Stripe failed.
    _stripe_error(t('Could not connect to Stripe. Reason: %msg', array(
      '%msg' => $e
        ->getMessage(),
    )));
  } catch (\Stripe\Error\Base $e) {

    // Generic error.
    _stripe_error(t('Stripe error: %msg', array(
      '%msg' => $e
        ->getMessage(),
    )));
  } catch (Exception $e) {

    // Something else that we could not catch.
    _stripe_error(t('Could not load Stripe library. Reason: %msg', array(
      '%msg' => $e
        ->getMessage(),
    )));
  }
  if (!empty($library['loaded'])) {

    // If configured to, set the API Version for all requests.
    // Because the default is the version configured in the Stripe
    // Account dashboard, we only set the version if something else
    // has been configured by an administrator.
    $api_version = variable_get('stripe_api_version', STRIPE_API_ACCOUNT_DEFAULT);
    if ($api_version != STRIPE_API_ACCOUNT_DEFAULT) {
      if ($api_version == STRIPE_API_VERSION_CUSTOM) {
        $api_version = check_plain(variable_get('stripe_api_version_custom'));
      }
      try {
        \Stripe\Stripe::setApiVersion($api_version);
      } catch (\Stripe\Error\InvalidRequest $e) {
        _stripe_error(t('Stripe InvalidRequest Exception: %error', array(
          '%error' => $e
            ->getMessage(),
        )));
      } catch (\Stripe\Error\Authentication $e) {
        _stripe_error(t('Stripe setApiVersion Exception: %error', array(
          '%error' => $e
            ->getMessage(),
        )));
      }
    }
  }
  return $library;
}