2023-02-24 07:20:51 +00:00
|
|
|
import stripe
|
2023-02-24 07:20:31 +00:00
|
|
|
from django.conf import settings
|
2023-07-29 16:28:12 +00:00
|
|
|
from lago_python_client import Client
|
2023-08-10 17:11:40 +00:00
|
|
|
from lago_python_client.exceptions import LagoApiError
|
2023-02-24 07:20:51 +00:00
|
|
|
from lago_python_client.models import Customer, CustomerBillingConfiguration
|
2023-02-15 07:20:53 +00:00
|
|
|
|
2023-02-24 07:20:31 +00:00
|
|
|
client = Client(api_key=settings.LAGO_API_KEY, api_url=settings.LAGO_URL)
|
2023-02-24 07:20:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
def expand_name(first_name, last_name):
|
|
|
|
"""
|
|
|
|
Convert two name variables into one.
|
|
|
|
Last name without a first name is ignored.
|
2023-02-27 07:20:42 +00:00
|
|
|
: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
|
2023-02-24 07:20:51 +00:00
|
|
|
"""
|
|
|
|
name = None
|
|
|
|
if first_name:
|
|
|
|
name = first_name
|
|
|
|
# We only want to put the last name if we have a first name
|
|
|
|
if last_name:
|
|
|
|
name += f" {last_name}"
|
|
|
|
return name
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
2023-02-27 07:20:42 +00:00
|
|
|
: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
|
2023-02-24 07:20:51 +00:00
|
|
|
"""
|
|
|
|
# Let's see if we're just missing the ID
|
|
|
|
matching_customers = stripe.Customer.list(email=email, limit=2)
|
|
|
|
if len(matching_customers) == 2:
|
|
|
|
# Something is horribly wrong
|
|
|
|
raise Exception(f"Two customers found for email {email}")
|
|
|
|
|
|
|
|
elif len(matching_customers) == 1:
|
|
|
|
# We found a customer. Let's copy the ID
|
|
|
|
customer = matching_customers["data"][0]
|
|
|
|
customer_id = customer["id"]
|
|
|
|
return customer_id
|
|
|
|
|
|
|
|
else:
|
|
|
|
# We didn't find anything. Create the customer
|
|
|
|
|
|
|
|
# Create a name, since we have 2 variables which could be null
|
|
|
|
name = expand_name(first_name, last_name)
|
|
|
|
cast = {"email": email}
|
|
|
|
if name:
|
|
|
|
cast["name"] = name
|
|
|
|
customer = stripe.Customer.create(**cast)
|
|
|
|
|
|
|
|
return customer.id
|
|
|
|
|
|
|
|
|
2023-02-27 07:20:42 +00:00
|
|
|
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)
|
|
|
|
|
2023-08-26 11:05:28 +00:00
|
|
|
|
2023-02-24 07:20:51 +00:00
|
|
|
def create_or_update_customer(user):
|
2023-02-27 07:20:42 +00:00
|
|
|
"""
|
|
|
|
Create or update a customer in Lago.
|
|
|
|
"""
|
2023-02-24 07:20:51 +00:00
|
|
|
try:
|
|
|
|
customer = client.customers().find(str(user.customer_id))
|
|
|
|
except LagoApiError:
|
|
|
|
customer = None
|
|
|
|
if not customer:
|
|
|
|
customer = Customer(
|
|
|
|
external_id=str(user.customer_id),
|
|
|
|
name=f"{user.first_name} {user.last_name}",
|
|
|
|
)
|
|
|
|
|
|
|
|
customer.external_id = str(user.customer_id)
|
|
|
|
customer.email = user.email
|
|
|
|
customer.name = f"{user.first_name} {user.last_name}"
|
|
|
|
customer.billing_configuration = CustomerBillingConfiguration(
|
|
|
|
payment_provider="stripe",
|
|
|
|
provider_customer_id=str(user.stripe_id),
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
created = client.customers().create(customer)
|
|
|
|
except LagoApiError as e:
|
|
|
|
print(e.response)
|
|
|
|
|
|
|
|
lago_id = created.lago_id
|
|
|
|
|
|
|
|
return lago_id
|
|
|
|
|
|
|
|
|
2023-02-27 07:20:42 +00:00
|
|
|
def delete_customer(user):
|
2023-02-24 07:20:51 +00:00
|
|
|
"""
|
2023-02-27 07:20:42 +00:00
|
|
|
Delete a customer from Lago.
|
|
|
|
:param user: User object to delete
|
2023-02-24 07:20:51 +00:00
|
|
|
"""
|
2023-02-27 07:20:42 +00:00
|
|
|
try:
|
|
|
|
client.customers().destroy(str(user.customer_id))
|
|
|
|
except LagoApiError:
|
|
|
|
pass
|