From e60b93d126dfe81a9f0360144136d08c91beb05a Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 7 Jul 2018 01:28:25 +0200 Subject: [PATCH] Add management command import_usercarddetails --- .../commands/import_usercarddetails.py | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 hosting/management/commands/import_usercarddetails.py diff --git a/hosting/management/commands/import_usercarddetails.py b/hosting/management/commands/import_usercarddetails.py new file mode 100644 index 00000000..fa5b975e --- /dev/null +++ b/hosting/management/commands/import_usercarddetails.py @@ -0,0 +1,39 @@ +from django.core.management.base import BaseCommand + +from hosting.models import UserCardDetail +from membership.models import CustomUser +from utils.stripe_utils import StripeUtils + + +class Command(BaseCommand): + help = '''Imports the usercard details of all customers. Created just for + multiple card support.''' + + def handle(self, *args, **options): + try: + stripe_utils = StripeUtils() + for user in CustomUser.objects.filter(id__gte=114): + if user.stripecustomer: + card_details_resp = stripe_utils.get_card_details( + user.stripecustomer.stripe_id + ) + card_details = card_details_resp['response_object'] + if card_details: + ucd = UserCardDetail.get_or_create_user_card_detail( + stripe_customer=user.stripecustomer, + card_details=card_details + ) + UserCardDetail.save_default_card_local( + user.stripecustomer.stripe_id, + ucd.card_id + ) + print("Saved user card details for {}".format( + user.email + )) + else: + print(" --- Could not get card details for {}".format( + user.email + )) + print("Error: {}".format(card_details_resp['error'])) + except Exception as e: + print("Error occurred. Details {}".format(str(e)))