import stripe from django.conf import settings # from lago_python_client import Client from lago_python_client.client import Client from lago_python_client.exceptions import LagoApiError from lago_python_client.models import Customer, CustomerBillingConfiguration client = Client(api_key=settings.LAGO_API_KEY, api_url=settings.LAGO_URL) if settings.STRIPE_TEST: stripe.api_key = settings.STRIPE_API_KEY_TEST else: stripe.api_key = settings.STRIPE_API_KEY_PROD 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: 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. :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) 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 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: 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 def delete_customer(user): """ Delete a customer from Lago. :param user: User object to delete """ try: client.customers().destroy(str(user.customer_id)) except LagoApiError: pass