106 lines
3.4 KiB
Python
Raw Normal View History

2025-11-10 15:46:40 +01:00
import stripe
2025-11-13 23:22:05 +01:00
from typing import Dict
2025-11-10 15:46:40 +01:00
from ..settings import settings
2025-11-13 23:22:05 +01:00
2025-11-10 15:46:40 +01:00
class StripeClient:
@staticmethod
2025-11-11 01:05:13 +01:00
def _ensure_api_key():
if not stripe.api_key:
if settings.STRIPE_SECRET_KEY:
stripe.api_key = settings.STRIPE_SECRET_KEY
else:
raise ValueError("Stripe API key not configured")
2025-11-13 23:22:05 +01:00
2025-11-11 01:05:13 +01:00
@staticmethod
2025-11-10 15:46:40 +01:00
async def create_customer(email: str, name: str, metadata: Dict = None) -> str:
2025-11-11 01:05:13 +01:00
StripeClient._ensure_api_key()
2025-11-10 15:46:40 +01:00
customer = stripe.Customer.create(
2025-11-13 23:22:05 +01:00
email=email, name=name, metadata=metadata or {}
2025-11-10 15:46:40 +01:00
)
return customer.id
@staticmethod
async def create_payment_intent(
amount: int,
currency: str = "usd",
customer_id: str = None,
2025-11-13 23:22:05 +01:00
metadata: Dict = None,
2025-11-10 15:46:40 +01:00
) -> stripe.PaymentIntent:
2025-11-11 01:05:13 +01:00
StripeClient._ensure_api_key()
2025-11-10 15:46:40 +01:00
return stripe.PaymentIntent.create(
amount=amount,
currency=currency,
customer=customer_id,
metadata=metadata or {},
2025-11-13 23:22:05 +01:00
automatic_payment_methods={"enabled": True},
2025-11-10 15:46:40 +01:00
)
@staticmethod
async def create_invoice(
2025-11-13 23:22:05 +01:00
customer_id: str, description: str, line_items: list, metadata: Dict = None
2025-11-10 15:46:40 +01:00
) -> stripe.Invoice:
2025-11-11 01:05:13 +01:00
StripeClient._ensure_api_key()
2025-11-10 15:46:40 +01:00
for item in line_items:
stripe.InvoiceItem.create(
customer=customer_id,
2025-11-13 23:22:05 +01:00
amount=int(item["amount"] * 100),
currency=item.get("currency", "usd"),
description=item["description"],
metadata=item.get("metadata", {}),
2025-11-10 15:46:40 +01:00
)
invoice = stripe.Invoice.create(
customer=customer_id,
description=description,
auto_advance=True,
2025-11-13 23:22:05 +01:00
collection_method="charge_automatically",
metadata=metadata or {},
2025-11-10 15:46:40 +01:00
)
return invoice
@staticmethod
async def finalize_invoice(invoice_id: str) -> stripe.Invoice:
2025-11-11 01:05:13 +01:00
StripeClient._ensure_api_key()
2025-11-10 15:46:40 +01:00
return stripe.Invoice.finalize_invoice(invoice_id)
@staticmethod
async def pay_invoice(invoice_id: str) -> stripe.Invoice:
2025-11-11 01:05:13 +01:00
StripeClient._ensure_api_key()
2025-11-10 15:46:40 +01:00
return stripe.Invoice.pay(invoice_id)
@staticmethod
async def attach_payment_method(
2025-11-13 23:22:05 +01:00
payment_method_id: str, customer_id: str
2025-11-10 15:46:40 +01:00
) -> stripe.PaymentMethod:
2025-11-11 01:05:13 +01:00
StripeClient._ensure_api_key()
2025-11-10 15:46:40 +01:00
payment_method = stripe.PaymentMethod.attach(
2025-11-13 23:22:05 +01:00
payment_method_id, customer=customer_id
2025-11-10 15:46:40 +01:00
)
stripe.Customer.modify(
2025-11-13 23:22:05 +01:00
customer_id, invoice_settings={"default_payment_method": payment_method_id}
2025-11-10 15:46:40 +01:00
)
return payment_method
@staticmethod
async def list_payment_methods(customer_id: str, type: str = "card"):
2025-11-11 01:05:13 +01:00
StripeClient._ensure_api_key()
2025-11-13 23:22:05 +01:00
return stripe.PaymentMethod.list(customer=customer_id, type=type)
2025-11-10 15:46:40 +01:00
@staticmethod
async def create_subscription(
2025-11-13 23:22:05 +01:00
customer_id: str, price_id: str, metadata: Dict = None
2025-11-10 15:46:40 +01:00
) -> stripe.Subscription:
2025-11-11 01:05:13 +01:00
StripeClient._ensure_api_key()
2025-11-10 15:46:40 +01:00
return stripe.Subscription.create(
2025-11-13 23:22:05 +01:00
customer=customer_id, items=[{"price": price_id}], metadata=metadata or {}
2025-11-10 15:46:40 +01:00
)
@staticmethod
async def cancel_subscription(subscription_id: str) -> stripe.Subscription:
2025-11-11 01:05:13 +01:00
StripeClient._ensure_api_key()
2025-11-10 15:46:40 +01:00
return stripe.Subscription.delete(subscription_id)