You are here

function drush_uc_stripe_subscription_cancel in Ubercart Stripe 6.2

Same name and namespace in other branches
  1. 7.3 uc_stripe.drush.inc \drush_uc_stripe_subscription_cancel()
  2. 7.2 uc_stripe.drush.inc \drush_uc_stripe_subscription_cancel()

Command callback

Delete active subscriptions found in uc_recurring_stripe table.

File

./uc_stripe.drush.inc, line 43
Delete legacy uc_stripe subscriptions that were created by v1.

Code

function drush_uc_stripe_subscription_cancel($order_id = 'all') {
  $dry_run = drush_get_option('dry-run');
  if (!db_table_exists('uc_recurring_stripe')) {
    drush_print('No uc_recurring_stripe table exists, exiting.');
    return;
  }
  if ($dry_run) {
    drush_print('dry-run is set so not deleting any subscriptions');
  }
  _uc_stripe_prepare_api();
  $query = 'SELECT * from {uc_recurring_stripe} ucs ';
  if ($order_id != 'all') {
    $query .= ' WHERE order_id = %d';
  }
  $result = db_query($query, array(
    $order_id,
  ));
  while ($item = db_fetch_array($result)) {
    $customer = NULL;
    $account = user_load($item['uid']);
    try {
      $customer = Stripe_Customer::retrieve($item['customer_id']);
      $subscriptions = $customer->subscriptions
        ->all();
    } catch (Exception $e) {
      drush_print("Failed to retrieve customer subscriptions for {$item['customer_id']} (user:{$account->name}}. Message: {$e->getMessage()}");
      continue;
    }
    $count = count($subscriptions['data']);
    $msg = "Retrieved Stripe Customer with {$count} subscriptions,  uid:{$item['uid']} username:{$account->name} order:{$item['order_id']} stripe customer id:{$item['customer_id']}";
    drush_print($msg);
    foreach ($subscriptions['data'] as $sub) {
      drush_print("   Subscription: {$sub->id} {$sub->status} {$sub->plan->name} {$sub->plan->id}");
      if (empty($dry_run) && drush_confirm('Are you sure you want to cancel this subscription?')) {
        try {
          $sub
            ->cancel();
          drush_print("    Cancelled subscription {$sub->id}");
        } catch (Exception $e) {
          drush_print("    FAILED to cancel subscription {$sub->id} Message: {$e->getMessage()}");
        }
      }
    }
  }
}