Begin implementing billing
This commit is contained in:
@@ -1,4 +1,11 @@
|
||||
# from lago_python_client import Client
|
||||
# from django.conf import settings
|
||||
from django.conf import settings
|
||||
from lago_python_client import Client
|
||||
from lago_python_client.models import (
|
||||
Charge,
|
||||
Charges,
|
||||
Customer,
|
||||
CustomerBillingConfiguration,
|
||||
Plan,
|
||||
)
|
||||
|
||||
# client = Client(api_key=settings.LAGO_API_KEY, api_url=settings.LAGO_URL)
|
||||
client = Client(api_key=settings.LAGO_API_KEY, api_url=settings.LAGO_URL)
|
||||
|
||||
@@ -1,65 +1,65 @@
|
||||
import logging
|
||||
# import logging
|
||||
|
||||
import stripe
|
||||
# import stripe
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
# 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 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}")
|
||||
# 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
|
||||
# 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
|
||||
# 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}")
|
||||
# # 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
|
||||
# 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}")
|
||||
# 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}")
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
from asgiref.sync import sync_to_async
|
||||
# from asgiref.sync import sync_to_async
|
||||
|
||||
from core.models import Plan
|
||||
# from core.models import Plan
|
||||
|
||||
|
||||
async def assemble_plan_map(product_id_filter=None):
|
||||
"""
|
||||
Get all the plans from the database and create an object Stripe wants.
|
||||
"""
|
||||
line_items = []
|
||||
for plan in await sync_to_async(list)(Plan.objects.all()):
|
||||
if product_id_filter:
|
||||
if plan.product_id != product_id_filter:
|
||||
continue
|
||||
line_items.append(
|
||||
{
|
||||
"price": plan.product_id,
|
||||
"quantity": 1,
|
||||
}
|
||||
)
|
||||
return line_items
|
||||
# async def assemble_plan_map(product_id_filter=None):
|
||||
# """
|
||||
# Get all the plans from the database and create an object Stripe wants.
|
||||
# """
|
||||
# line_items = []
|
||||
# for plan in await sync_to_async(list)(Plan.objects.all()):
|
||||
# if product_id_filter:
|
||||
# if plan.product_id != product_id_filter:
|
||||
# continue
|
||||
# line_items.append(
|
||||
# {
|
||||
# "price": plan.product_id,
|
||||
# "quantity": 1,
|
||||
# }
|
||||
# )
|
||||
# return line_items
|
||||
|
||||
Reference in New Issue
Block a user