You are here

function uc_stripe_cancel in Ubercart Stripe 7

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

Cancel the recurring fee using the Stripe API.

Parameters

$order: The order object.

$op: The operation.

Return value

TRUE if recurring fee was cancelled.

1 string reference to 'uc_stripe_cancel'
uc_stripe_recurring_info in ./uc_stripe.module
Implements hook_recurring_info().

File

./uc_stripe.module, line 601
A module used for processing payments with Stripe.

Code

function uc_stripe_cancel($order, $op) {
  global $user;
  $plan_id = str_replace('/', '_', $order->data['model']);

  // Must be logged in to cancel.
  if ($user->uid == 0) {
    return FALSE;
  }
  $result = db_query("SELECT * FROM {uc_recurring_stripe} WHERE uid = %d", $user->uid);
  while ($sub = db_fetch_object($result)) {
    if ($sub->plan_id == $plan_id) {
      $customer_id = $sub->customer_id;
      $rfid = $sub->rfid;
    }
  }

  // Load the Stripe API and set the API key.
  if (!_uc_stripe_load_api()) {
    return FALSE;
  }
  try {
    $customer = Stripe_Customer::retrieve($customer_id);
    $customer
      ->cancelSubscription();
    $cust_record->rfid = $rfid;
    $cust_record->active = 0;
    drupal_write_record('uc_recurring_stripe', $cust_record, array(
      'rfid',
    ));
  } catch (Exception $e) {
    drupal_set_message(t("Unable to retrieve customer subscription: @message", array(
      "@message" => $e
        ->getMessage(),
    )), 'error');
    watchdog('uc_stripe', "Unable to retrieve customer subscription for %customer_id", array(
      '%customer_id' => $customer_id,
    ));
    return FALSE;
  }
  uc_order_comment_save($order->order_id, 0, t('Stripe: Subscription @customer_id cancelled.', array(
    '@subscription_id' => $customer_id,
  )), 'admin');
  return FALSE;
}