forked from pinax/pinax-stripe-light
/
sync_customers.py
36 lines (28 loc) · 1.21 KB
/
sync_customers.py
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
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from stripe.error import InvalidRequestError
from ...actions import customers, charges, invoices
class Command(BaseCommand):
help = "Sync customer data"
def handle(self, *args, **options):
User = get_user_model()
qs = User.objects.exclude(customer__isnull=True)
count = 0
total = qs.count()
for user in qs:
count += 1
perc = int(round(100 * (float(count) / float(total))))
username = getattr(user, user.USERNAME_FIELD)
self.stdout.write(u"[{0}/{1} {2}%] Syncing {3} [{4}]\n".format(
count, total, perc, username, user.pk
))
customer = customers.get_customer_for_user(user)
try:
customers.sync_customer(customer)
except InvalidRequestError as e:
if e.http_status == 404:
# This user doesn't exist (might be in test mode)
continue
if customer.date_purged is None:
invoices.sync_invoices_for_customer(customer)
charges.sync_charges_for_customer(customer)