Integrate Lago with Stripe
parent
cde1392e68
commit
9d37e2bfb8
@ -1,11 +1,90 @@
|
||||
import stripe
|
||||
from django.conf import settings
|
||||
from lago_python_client import Client
|
||||
from lago_python_client.models import (
|
||||
Charge,
|
||||
Charges,
|
||||
Customer,
|
||||
CustomerBillingConfiguration,
|
||||
Plan,
|
||||
)
|
||||
from lago_python_client.clients.base_client import LagoApiError
|
||||
from lago_python_client.models import Customer, CustomerBillingConfiguration
|
||||
|
||||
client = Client(api_key=settings.LAGO_API_KEY, api_url=settings.LAGO_URL)
|
||||
|
||||
|
||||
def expand_name(first_name, last_name):
|
||||
"""
|
||||
Convert two name variables into one.
|
||||
Last name without a first name is ignored.
|
||||
"""
|
||||
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.
|
||||
"""
|
||||
# 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 create_or_update_customer(user):
|
||||
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 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)
|
||||
|
@ -1,65 +0,0 @@
|
||||
# import logging
|
||||
|
||||
# import stripe
|
||||
|
||||
# logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# def expand_name(first_name, last_name):
|
||||
# """
|
||||
# Convert two name variables into one.
|
||||
# Last name without a first name is ignored.
|
||||
# """
|
||||
# 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.
|
||||
# """
|
||||
# # 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
|
||||
# logger.error(f"Two customers found for email {email}")
|
||||
# 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)
|
||||
# logger.info(f"Created new Stripe customer {customer.id} with email {email}")
|
||||
|
||||
# return customer.id
|
||||
|
||||
|
||||
# def update_customer_fields(stripe_id, email=None, first_name=None, last_name=None):
|
||||
# """
|
||||
# Update the customer fields in Stripe.
|
||||
# """
|
||||
# if email:
|
||||
# stripe.Customer.modify(stripe_id, email=email)
|
||||
# logger.info(f"Modified Stripe customer {stripe_id} to have email {email}")
|
||||
# if first_name or last_name:
|
||||
# name = expand_name(first_name, last_name)
|
||||
# stripe.Customer.modify(stripe_id, name=name)
|
||||
# logger.info(f"Modified Stripe customer {stripe_id} to have email {name}")
|
@ -0,0 +1,20 @@
|
||||
# Generated by Django 4.1.7 on 2023-02-24 13:21
|
||||
|
||||
import uuid
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('core', '0002_account_activemanagementpolicy_assetgroup_hook_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='user',
|
||||
name='customer_id',
|
||||
field=models.UUIDField(blank=True, default=uuid.uuid4, null=True),
|
||||
),
|
||||
]
|
@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.1.7 on 2023-02-24 16:09
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('core', '0003_user_customer_id'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='user',
|
||||
name='stripe_id',
|
||||
field=models.CharField(blank=True, max_length=255, null=True),
|
||||
),
|
||||
]
|
Loading…
Reference in New Issue