Add comments and clean up Lago customers

This commit is contained in:
2023-02-27 07:20:42 +00:00
parent 9d37e2bfb8
commit 5843000df6
2 changed files with 31 additions and 8 deletions

View File

@@ -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