You are here

function uc_stripe_renew in Ubercart Stripe 7.3

Same name and namespace in other branches
  1. 6.2 uc_stripe.module \uc_stripe_renew()
  2. 6 uc_stripe.module \uc_stripe_renew()
  3. 7 uc_stripe.module \uc_stripe_renew()
  4. 7.2 uc_stripe.module \uc_stripe_renew()

Handle renewing a recurring fee, called by uc_recurring

Runs when the subscription interval is hit. So once a month or whatever. This just charges the stripe customer whatever amount ubercart wants. It does not use the Stripe subscription feature.

Parameters

$order:

$fee:

Return value

bool

1 string reference to 'uc_stripe_renew'
uc_stripe_recurring_info in ./uc_stripe.module
Implements hook_recurring_info() to integrate with uc_recurring

File

./uc_stripe.module, line 783
A stripe.js PCI-compliant payment gateway Forked from Bitcookie's work (thanks!) which was posted at http://bitcookie.com/blog/pci-compliant-ubercart-and-stripe-js from discussion in the uc_stripe issue queue, https://www.drupal.org/node/1467886

Code

function uc_stripe_renew($order, &$fee) {
  try {

    //Load the API
    _uc_stripe_prepare_api();

    //Get the customer ID
    $stripe_customer_id = _uc_stripe_get_customer_id($order->uid);
    $stripe_payment_id = _uc_stripe_get_payment_id($order->uid);
    if (empty($stripe_customer_id)) {
      throw new Exception('No stripe customer ID found');
    }
    $amount = $fee->fee_amount;
    $amount = $amount * 100;

    //create intent Array
    $intent_params = array(
      'amount' => $amount,
      'currency' => strtolower($order->currency),
      'payment_method_types' => [
        'card',
      ],
      'customer' => $stripe_customer_id,
      'off_session' => true,
      'confirm' => true,
    );

    // Payment methods added with Stripe PaymentIntent API will be saved to customer
    // object in drupal. Payment cards saved with 2.x tokens will not have a value
    // saved to customer object, but the payment Intent will still continue because
    // Stipe will use default payment in those situations.
    if ($stripe_payment_id) {
      $intent_params['payment_method'] = $stripe_payment_id;
    }

    // Idempotency key to mark unique requests in Stripe API.
    $idempotency_key = _uc_stripe_create_idempotency_key($order->order_id . $amount . $stripe_payment_id);

    //Allow other modules to alter recurring $intent params array
    drupal_alter('uc_stripe_recurring_intent', $intent_params, $order);
    $payment_intent = \Stripe\PaymentIntent::create($intent_params, [
      'idempotency_key' => $idempotency_key,
    ]);
    uc_payment_enter($order->order_id, $order->payment_method, $order->order_total, $fee->uid, $payment_intent, "Success");
    $formatted_amount = number_format($fee->fee_amount, 2);
    $message = t('Card renewal payment of @amount processed successfully.', array(
      '@amount' => $formatted_amount,
    ));
    uc_order_comment_save($fee->order_id, $order->uid, $message, 'admin');
    return TRUE;
  } catch (\Stripe\Error\Card $e) {
    if ($e
      ->getDeclineCode() === 'authentication_required') {
      $NOT_COMPLETED = 0;

      // Create and store hash so that we can prompt user to authenticate payment.
      $hash = drupal_hmac_base64(REQUEST_TIME . $order->order_id, drupal_get_hash_salt() . $stripe_payment_id);
      db_insert('uc_stripe_pending_auth')
        ->fields(array(
        'order_id' => $order->order_id,
        'completed' => $NOT_COMPLETED,
        'rfee_id' => $fee->rfid,
        'hash' => $hash,
      ))
        ->execute();

      // Prepare email to alert user that authentication is required.
      $params['body'] = variable_get('uc_stripe_authentication_required_email', _uc_stripe_get_authentication_required_email_text());
      $params['user'] = user_load($order->uid);
      $params['hash'] = $hash;
      drupal_mail('uc_stripe', 'authentication_required', $params['user']->mail, language_default(), $params);
    }
    $result = array(
      'success' => FALSE,
      'comment' => $e
        ->getCode(),
      'message' => t("Renewal Failed for order !order: !message", array(
        "!order" => $order->order_id,
        "!message" => $e
          ->getMessage(),
      )),
    );
    uc_order_comment_save($order->order_id, $order->uid, $result['message'], 'admin');
    watchdog('uc_stripe', 'Renewal failed for order @order_id, code=@code, message: @message', array(
      '@order_id' => $order->order_id,
      '@code' => $e
        ->getCode(),
      '@message' => $e
        ->getMessage(),
    ));
    return FALSE;
  } catch (Exception $e) {
    watchdog('uc_stripe', 'Renewal failed for order @order_id, code=@code, message: @message', array(
      '@order_id' => $order->order_id,
      '@code' => $e
        ->getCode(),
      '@message' => $e
        ->getMessage(),
    ));
    return FALSE;
  }
}