You are here

public function StripePaymentMethodController::retrieveAccount in Stripe 7

Retrieve the Stripe Account object for a given Payment Method.

This method handles Stripe errors and logs them to Drupal's watchdog.

Result for a given Payment Method is cached both statically and temporarily in Drupal's cache.

Parameters

PaymentMethod $payment_method: A Payment Method.

Return value

\Stripe\Account The Stripe Account object for the given payment method. Or NULL if the account could not be retrieved (errors are logged to Drupal's watchdog).

1 call to StripePaymentMethodController::retrieveAccount()
StripePaymentMethodController::validate in stripe_payment/includes/StripePaymentMethodController.inc
Validate a payment against a payment method and this controller.

File

stripe_payment/includes/StripePaymentMethodController.inc, line 300
Stripe Payment controller class and helper code (classes and function).

Class

StripePaymentMethodController

Code

public function retrieveAccount(PaymentMethod $payment_method) {
  if ($payment_method->controller->name === 'StripePaymentMethodController') {
    $accounts[] =& drupal_static(__METHOD__, array());
    if (!isset($accounts[$payment_method->pmid])) {
      $cid = "stripe_payment:{$payment_method->pmid}:account";
      $cache = cache_get($cid);
      if ($cache && !empty($cache->data)) {
        $accounts[$payment_method->pmid] = $cache->data;
      }
      else {
        $api_key = !empty($payment_method->controller_data['keys']['mode']) ? $payment_method->controller_data['keys']['secret'] : stripe_get_key('secret');
        try {
          $stripe_account = \Stripe\Account::retrieve($api_key);
          cache_set($cid, $stripe_account, 'cache', CACHE_TEMPORARY);
          $accounts[$payment_method->pmid] = $stripe_account;
        } catch (\Stripe\Error $e) {
          if (payment_method_access('update', $payment_method)) {
            drupal_set_message(t("Unable to retrieve Stripe Account for <a href='@url'>Payment Method</a>: @message.", array(
              '@message' => $e
                ->getMessage(),
              '@url' => url("admin/config/services/payment/method/{$payment_method->pmid}"),
            )), 'error');
          }
          watchdog('stripe_payment', "Unable to retrieve Stripe Account for <a href='@url'>Payment Method</a>: @message.", array(
            '@message' => $e
              ->getMessage(),
            '@url' => url("admin/config/services/payment/method/{$payment_method->pmid}"),
          ), WATCHDOG_ERROR, l(t('edit'), "admin/config/services/payment/method/{$payment_method->pmid}"));
          $accounts[$payment_method->pmid] = NULL;
        }
      }
    }
    return $accounts[$payment_method->pmid];
  }
  else {
    return NULL;
  }
}