function drush_uc_stripe_subscription_cancel in Ubercart Stripe 7.2
Same name and namespace in other branches
- 6.2 uc_stripe.drush.inc \drush_uc_stripe_subscription_cancel()
- 7.3 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;
}
$library = libraries_load('stripe');
$info = libraries_info('stripe');
if (!$library['loaded'] || !array_key_exists(\Stripe\Stripe::VERSION, $info['versions'])) {
drush_print("Failed to load one of the supported Stripe PHP library versions: " . join(', ', array_keys($info['versions'])));
return;
}
if ($dry_run) {
drush_print('dry-run is set so not deleting any subscriptions');
}
_uc_stripe_prepare_api();
$query = db_select('uc_recurring_stripe', 'urs', array(
'fetch' => PDO::FETCH_ASSOC,
))
->fields('urs', array(
'uid',
'customer_id',
'order_id',
));
if ($order_id != 'all') {
$query
->condition('order_id', ':id');
}
$result = $query
->execute();
foreach ($result as $item) {
$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()}");
}
}
}
}
}