File: //home/arjun/projects/unlimited-leads/Unlimited-Leads-Be/services/stripe/seeder_function.py
import os
import django
# Set the default Django settings module (adjust the path as necessary)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'unlimited_leads.settings')
# Setup Django
django.setup()
import stripe
from payment.models import Product
from django.conf import settings
# Initialize the Stripe API with your secret key
stripe.api_key = settings.STRIPE_SECRET_KEY
STRIPE_TO_BILLING_PERIOD = {
'day': 'day',
'month': 'month',
'year': 'year',
}
def create_recurring_product(name, description, amount, currency, interval, interval_count=1, limit='Unlimited'):
try:
billing_period = STRIPE_TO_BILLING_PERIOD.get(interval)
# Check in the local database
existing_product = Product.objects.filter(plan_name=name, billing_period=billing_period, limit=limit).first()
if existing_product:
# If a product with the same name and interval exists, return an error message
print(f"Error: A product with the name '{name}' and billing period '{billing_period}' already exists.")
return
# Create a product in Stripe
product = stripe.Product.create(
name=name,
description=description
)
# Create a recurring price for the product
price = stripe.Price.create(
unit_amount=amount,
currency=currency,
product=product.id,
recurring={
"interval": interval,
"interval_count": interval_count
},
)
billing_period = STRIPE_TO_BILLING_PERIOD.get(interval)
local_product = Product.objects.create(
plan_name=name,
description=description,
amount=amount / 100, # Convert cents to dollars (Stripe stores amounts in cents)
currency=currency,
product_id=price.id, # Use the auto-generated UUID
billing_period=billing_period, # Use the interval as the billing period
limit=limit
)
print(f"Created product in Django DB: {local_product.product_id}")
print(f"Created product: {product.id}, price: {price.id}")
except Exception as e:
print(f"Error creating product: {str(e)}")
if __name__ == '__main__':
create_recurring_product(
name="Monthly Plan",
description="Download up to 10,000 leads per month.",
amount=2500,
currency="usd",
interval="month",
interval_count=1,
limit="10000"
)