You are here

public function StripePaymentMethodController::retrieveCharge in Stripe 7

Retrieve the Stripe Charge object for a given Payment.

This method handles Stripe errors and logs them to Drupal's watchdog. If the user has view access to the Payment object, errors are also displayed as error message.

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

Parameters

Payment $payment: A Payment object.

Return value

\Stripe\Charge The Stripe Charge object for the given Payment. Or NULL if no Stripe Charge could be retrieved (errors are logged to Drupal's watchdog) or if the Payment does not use Stripe.

File

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

Class

StripePaymentMethodController

Code

public function retrieveCharge(Payment $payment) {
  if ($payment->method->controller->name === 'StripePaymentMethodController' && isset($payment->method_data['charge'])) {
    $cid = "stripe_payment:{$payment->method->pmid}:charge:{$payment->method_data['charge']}";
    $cache = cache_get($cid);
    if ($cache && !empty($cache->data)) {
      return $cache->data;
    }
    else {
      $api_key = !empty($payment->method->controller_data['keys']['mode']) ? $payment->method->controller_data['keys']['secret'] : stripe_get_key('secret');
      try {
        $charge = \Stripe\Charge::retrieve($payment->method_data['charge'], $api_key);
        cache_set($cid, $charge, 'cache', CACHE_TEMPORARY);
        return $charge;
      } catch (\Stripe\Error $e) {
        if (payment_access('view', $payment)) {
          drupal_set_message(t('Unable to retrieve Stripe Charge for <a href="@url">Payment</a>: @message.', array(
            '@url' => url("payment/{$payment->pid}"),
            '@message' => $e
              ->getMessage(),
          )), 'error');
        }
        watchdog('stripe_payment', 'Unable to retrieve Stripe Charge for <a href="@url">Payment</a>: @message.', array(
          '@url' => url("payment/{$payment->pid}"),
          '@message' => $e
            ->getMessage(),
        ), WATCHDOG_ERROR, l(t('view'), "payment/{$payment->pid}"));
      }
    }
  }
  return NULL;
}