Add Stripe Customer Portal session API for account billing.
Expose POST /api/finance/portal/ so authenticated users can open Stripe's hosted portal for plan changes, payment methods, and cancellations (chat_web_app#33 companion).
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
"""Stripe Checkout session helpers."""
|
||||
"""Stripe Checkout and Billing Portal session helpers."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -7,6 +7,8 @@ from typing import Any
|
||||
import stripe
|
||||
from django.conf import settings
|
||||
|
||||
from finance.models import Invoice
|
||||
|
||||
|
||||
class StripeNotConfiguredError(RuntimeError):
|
||||
"""Raised when Stripe secret key is missing."""
|
||||
@@ -69,3 +71,27 @@ def create_checkout_session(
|
||||
subscription_data={"metadata": metadata},
|
||||
)
|
||||
return session
|
||||
|
||||
|
||||
def resolve_stripe_customer_id(*, user) -> str | None:
|
||||
"""Return the most recent Stripe customer id stored on the user's invoices."""
|
||||
return (
|
||||
Invoice.objects.filter(user=user)
|
||||
.exclude(stripe_customer_id="")
|
||||
.order_by("-created")
|
||||
.values_list("stripe_customer_id", flat=True)
|
||||
.first()
|
||||
)
|
||||
|
||||
|
||||
def create_billing_portal_session(
|
||||
*,
|
||||
customer_id: str,
|
||||
return_url: str | None = None,
|
||||
):
|
||||
"""Create a Stripe Customer Portal session for plan/payment/cancel management."""
|
||||
configure_stripe()
|
||||
return stripe.billing_portal.Session.create(
|
||||
customer=customer_id,
|
||||
return_url=return_url or settings.STRIPE_PORTAL_RETURN_URL,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user