Handle case where a Stripe account may have been deleted via dashboard
This commit is contained in:
parent
75e90dbacd
commit
600b549704
1 changed files with 15 additions and 6 deletions
|
@ -210,21 +210,30 @@ class StripeCustomer(models.Model):
|
||||||
# check if user is not in stripe but in database
|
# check if user is not in stripe but in database
|
||||||
customer = stripe_utils.check_customer(stripe_customer.stripe_id,
|
customer = stripe_utils.check_customer(stripe_customer.stripe_id,
|
||||||
stripe_customer.user, token)
|
stripe_customer.user, token)
|
||||||
|
if "deleted" in customer and customer["deleted"]:
|
||||||
|
raise StripeCustomer.DoesNotExist()
|
||||||
if not customer.sources.data:
|
if not customer.sources.data:
|
||||||
stripe_utils.update_customer_token(customer, token)
|
stripe_utils.update_customer_token(customer, token)
|
||||||
return stripe_customer
|
return stripe_customer
|
||||||
|
|
||||||
except StripeCustomer.DoesNotExist:
|
except StripeCustomer.DoesNotExist:
|
||||||
user = CustomUser.objects.get(email=email)
|
user = CustomUser.objects.get(email=email)
|
||||||
stripe_utils = StripeUtils()
|
stripe_utils = StripeUtils()
|
||||||
stripe_data = stripe_utils.create_customer(token, email, user.name)
|
stripe_data = stripe_utils.create_customer(token, email, user.name)
|
||||||
if stripe_data.get('response_object'):
|
if stripe_data.get('response_object'):
|
||||||
stripe_cus_id = stripe_data.get('response_object').get('id')
|
stripe_cus_id = stripe_data.get('response_object').get('id')
|
||||||
|
if user.stripecustomer is None:
|
||||||
stripe_customer = StripeCustomer.objects. \
|
# The user never had an associated Stripe account
|
||||||
create(user=user, stripe_id=stripe_cus_id)
|
# So, create one
|
||||||
|
stripe_customer = StripeCustomer.objects.create(
|
||||||
|
user=user, stripe_id=stripe_cus_id
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
# User already had a Stripe account and we are here
|
||||||
|
# because the account was deleted in dashboard
|
||||||
|
# So, we simply update the stripe_id
|
||||||
|
user.stripecustomer.stripe_id = stripe_cus_id
|
||||||
|
user.stripecustomer.save()
|
||||||
|
stripe_customer = user.stripecustomer
|
||||||
return stripe_customer
|
return stripe_customer
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
Loading…
Reference in a new issue