-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuc_stripe.drush.inc
101 lines (86 loc) · 3.01 KB
/
uc_stripe.drush.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
* @file
* Delete legacy uc_stripe subscriptions that were created by v1.
*/
/**
* Implements hook_drush_command().
*
*
* @return
* An associative array describing your command(s).
*/
function uc_stripe_drush_command() {
$items = array();
$items['subscription-cancel'] = array(
'description' => "Delete subscriptions created by uc_stripe v1 and existing in uc_recurring_stripe table.",
'arguments' => array(
'order_id' => 'Order ID from uc_stripe_recurring table',
),
'options' => array(
'dry-run' => 'Dry run - does not actually execute, but show what would be done',
),
'examples' => array(
'drush subscription-cancel',
'drush subscription-cancel --dry-run',
'drush subscription-cancel <order_id>',
),
'aliases' => array('subcancel'),
);
return $items;
}
/**
* Command callback
*
* Delete active subscriptions found in uc_recurring_stripe table.
*/
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()}");
}
}
}
}
}