You are here

uc_stripe.module in Ubercart Stripe 8.2

A stripe.js PCI-compliant payment gateway

File

uc_stripe.module
View source
<?php

/**
 * @file
 * A stripe.js PCI-compliant payment gateway
 */
use Drupal\Core\Form\FormState;

/**
 * @TODO: Revisit this when uc_recurring is available
 *
 * Implements hook_recurring_info() to integrate with uc_recurring
 *
 * @return mixed
 */

//function uc_stripe_recurring_info() {

//  $items['uc_stripe'] = array(
//    'name' => t('Stripe'),
//    'payment method' => 'credit',
//    'module' => 'uc_recurring',
//    'fee handler' => 'uc_stripe',
//    'process callback' => 'uc_stripe_process',
//    'renew callback' => 'uc_stripe_renew',
//    'cancel callback' => 'uc_stripe_cancel',
//    'own handler' => FALSE,
//    'menu' => array(
//      'charge' => UC_RECURRING_MENU_DEFAULT,
//      'edit' => UC_RECURRING_MENU_DEFAULT,
//      'cancel' => UC_RECURRING_MENU_DEFAULT,
//    ),
//  );
//  return $items;

//}

/**
 * Implements hook_form_FORMID_alter() to make sure we've loaded the stripe js.
 * It must be loaded with the original form, as can't get loaded later by ajax,
 * although the attach method gets called IF it's been loaded.
 *
 * @param $form
 * @param $form_state
 */
function uc_stripe_form_uc_cart_checkout_form_alter(&$form, FormState $form_state) {
  $form['#attached']['library'][] = 'uc_stripe/uc_stripe';
}

/**
 * Check that all API keys are configured.
 *
 * @return bool
 *   TRUE if all 4 keys have a value.
 */
function _uc_stripe_check_api_keys($configuration) {
  return $configuration['live_publishable_key'] && $configuration['live_secret_key'] && $configuration['test_publishable_key'] && $configuration['test_secret_key'];
}

/**
 * Implements hook_uc_checkout_complete()
 *
 * Saves stripe customer_id into the user->data object
 *
 * @param $order
 * @param $account
 */
function uc_stripe_uc_checkout_complete($order, $account) {
  if ($order->payment_method == "credit") {

    // Pull the stripe customer ID from the temp storage.
    // It got there in uc_stripe_checkout_form_customsubmit()
    // This is only really necessary for uc_recurring
    $stripe_customer_id = \Drupal::service('user.private_tempstore')
      ->get('uc_stripe')
      ->get('uc_stripe_customer_id');
    \Drupal::getContainer('user.data')
      ->set('uc_stripe', $account
      ->id(), 'uc_stripe_customer_id', $stripe_customer_id);
  }
}

/**
 * TODO: Revisit uc_recurring renewal when uc_recurring is available.
 *
 * 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.
 *
 * @param $order
 * @param $fee
 * @return bool
 */

//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->id());
//
//    if (empty($stripe_customer_id)) {
//      throw new Exception('No stripe customer ID found');
//    }
//
//
//    //Create the charge
//    $amount = $fee->fee_amount;
//    $amount = $amount * 100;
//
//    $charge = \Stripe\Charge::create(array(
//        "amount" => $amount,
//        "currency" => strtolower($order->currency),
//        "customer" => $stripe_customer_id
//      )
//    );
//
//
//    uc_payment_enter($order->order_id, $order->payment_method, $order->order_total, $fee->id(), $charge, "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, 'order', 'completed', FALSE);
//
//    return TRUE;
//
//  } catch (Exception $e) {
//    $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');
//
//    \Drupal::logger('uc_stripe')->notice('Renewal failed for order @order_id, code=@code, message: @message', array('@order_id' => $order->order_id, '@code' => $result['comment'], '@message' => $result['message']));
//
//    return FALSE;
//  }
//
//

//}

/**
 * TODO: Revisit stripe renewal processing when uc_recurring is available
 * UC Recurring: Process a new recurring fee.
 * This runs when subscriptions are "set up" for the first time.
 * There is no action to be taken here except returning TRUE because the customer
 * ID is already stored with the user, where it can be accessed when next charge
 * takes place.
 *
 * @param $order
 * @param $fee
 * @return bool
 */

//function uc_stripe_process($order, &$fee) {

//  return TRUE;

//}

/**
 * TODO: Revisit this when uc_recurring is available
 *
 * UC Recurring: Cancel a recurring fee.
 * This runs when subscriptions are cancelled
 * Since we're handling charge intervals in ubercart, this doesn't need to do anything.
 *
 * @param $order
 * @param $op
 * @return bool
 */

//function uc_stripe_cancel($order, $op) {

//  $message = t("Subscription Canceled");
//  uc_order_comment_save($order->order_id, $order->uid, $message, 'order', 'completed', FALSE);
//  return TRUE;

//}

Functions

Namesort descending Description
uc_stripe_form_uc_cart_checkout_form_alter Implements hook_form_FORMID_alter() to make sure we've loaded the stripe js. It must be loaded with the original form, as can't get loaded later by ajax, although the attach method gets called IF it's been loaded.
uc_stripe_uc_checkout_complete Implements hook_uc_checkout_complete()
_uc_stripe_check_api_keys Check that all API keys are configured.