~linuxgoose/bocpress

ref: 59e1e4a7abc00dc5031b1dd32f2fead83f4640e9 bocpress/main/management/commands/checkstripe.py -rw-r--r-- 1.3 KiB
59e1e4a7Jordan Robinson remove show posts on homepage command from Blog Settings page 2 months ago
                                                                                
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
import stripe
from django.conf import settings
from django.core.management.base import BaseCommand

from main import models


class Command(BaseCommand):
    help = "Check Stripe data is in sync with database."

    def handle(self, *args, **options):
        stripe.api_key = settings.STRIPE_API_KEY

        total_count = 0
        last = None
        while True:
            if last:
                subscription_list = stripe.Subscription.list(
                    limit=100, starting_after=last.id
                )
            else:
                subscription_list = stripe.Subscription.list(limit=100)
            total_count += len(subscription_list)
            print(f"Current total: {total_count}")

            for subscription in subscription_list:
                if not models.User.objects.filter(
                    stripe_customer_id=subscription.customer
                ).exists():
                    self.stdout.write(
                        self.style.NOTICE(
                            "Customer not found in mataroa "
                            f"database: {subscription.customer}"
                        )
                    )

            if not subscription_list.has_more:
                break
            last = list(reversed(subscription_list))[0]