Add comments and clean up Lago customers
This commit is contained in:
parent
9d37e2bfb8
commit
5843000df6
|
@ -11,6 +11,9 @@ def expand_name(first_name, last_name):
|
|||
"""
|
||||
Convert two name variables into one.
|
||||
Last name without a first name is ignored.
|
||||
:param first_name: The first name
|
||||
:param last_name: The last name
|
||||
:return: A string with the first and last name, or None if both are None
|
||||
"""
|
||||
name = None
|
||||
if first_name:
|
||||
|
@ -26,6 +29,10 @@ def get_or_create(email, first_name, last_name):
|
|||
Get a customer ID from Stripe if one with the given email exists.
|
||||
Create a customer if one does not.
|
||||
Raise an exception if two or more customers matching the given email exist.
|
||||
:param email: The email address of the customer
|
||||
:param first_name: The first name of the customer
|
||||
:param last_name: The last name of the customer
|
||||
:return: The customer ID
|
||||
"""
|
||||
# Let's see if we're just missing the ID
|
||||
matching_customers = stripe.Customer.list(email=email, limit=2)
|
||||
|
@ -52,7 +59,19 @@ def get_or_create(email, first_name, last_name):
|
|||
return customer.id
|
||||
|
||||
|
||||
def update_customer_fields(user):
|
||||
"""
|
||||
Update the customer fields in Stripe.
|
||||
"""
|
||||
stripe.Customer.modify(user.stripe_id, email=user.email)
|
||||
name = expand_name(user.first_name, user.last_name)
|
||||
stripe.Customer.modify(user.stripe_id, name=name)
|
||||
|
||||
|
||||
def create_or_update_customer(user):
|
||||
"""
|
||||
Create or update a customer in Lago.
|
||||
"""
|
||||
try:
|
||||
customer = client.customers().find(str(user.customer_id))
|
||||
except LagoApiError:
|
||||
|
@ -81,10 +100,12 @@ def create_or_update_customer(user):
|
|||
return lago_id
|
||||
|
||||
|
||||
def update_customer_fields(user):
|
||||
def delete_customer(user):
|
||||
"""
|
||||
Update the customer fields in Stripe.
|
||||
Delete a customer from Lago.
|
||||
:param user: User object to delete
|
||||
"""
|
||||
stripe.Customer.modify(user.stripe_id, email=user.email)
|
||||
name = expand_name(user.first_name, user.last_name)
|
||||
stripe.Customer.modify(user.stripe_id, name=name)
|
||||
try:
|
||||
client.customers().destroy(str(user.customer_id))
|
||||
except LagoApiError:
|
||||
pass
|
||||
|
|
|
@ -106,6 +106,9 @@ class User(AbstractUser):
|
|||
if self.stripe_id:
|
||||
stripe.Customer.delete(self.stripe_id)
|
||||
log.info(f"Deleted Stripe customer {self.stripe_id}")
|
||||
if self.billing_provider_id:
|
||||
billing.delete_customer(self)
|
||||
log.info(f"Deleted Billing customer {self.billing_provider_id}")
|
||||
super().delete(*args, **kwargs)
|
||||
|
||||
# Override save to update attributes in Lago
|
||||
|
@ -118,9 +121,8 @@ class User(AbstractUser):
|
|||
self.stripe_id = billing.get_or_create(
|
||||
self.email, self.first_name, self.last_name
|
||||
)
|
||||
|
||||
billing_id = billing.create_or_update_customer(self)
|
||||
self.billing_provider_id = billing_id
|
||||
if not self.billing_provider_id:
|
||||
self.billing_provider_id = billing.create_or_update_customer(self)
|
||||
|
||||
billing.update_customer_fields(self)
|
||||
|
||||
|
|
Loading…
Reference in New Issue