13_customer_portal.py
stripe_demo/examples/13_customer_portal.py
"""
Stripe Customer Portal – let customers manage subscription, payment methods, invoices.
Create a portal session and redirect the customer to session.url.
Docs: https://docs.stripe.com/api/customer_portal/sessions
"""
import stripe
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from config import STRIPE_SECRET_KEY, DEFAULT_SUCCESS_URL, ensure_stripe_key
RETURN_URL = DEFAULT_SUCCESS_URL
stripe.api_key = STRIPE_SECRET_KEY
def create_portal_session(customer_id: str, return_url: str = None):
"""
Create a Customer Portal session. Redirect the customer to session.url.
In the portal they can: update payment method, cancel subscription, view invoices.
"""
ensure_stripe_key()
return_url = return_url or RETURN_URL
session = stripe.billing_portal.Session.create(
customer=customer_id,
return_url=return_url,
)
print(f"Portal session: {session.url}")
return session
def create_portal_configuration():
"""
Create or update portal configuration (what features are shown).
Default configuration is used if you don't create one.
"""
ensure_stripe_key()
# Example: allow subscription cancellation and payment method update
config = stripe.billing_portal.Configuration.create(
business_profile={
"headline": "Manage your subscription",
},
features={
"customer_update": {"enabled": True},
"invoice_history": {"enabled": True},
"payment_method_update": {"enabled": True},
"subscription_cancel": {"enabled": True},
},
)
print(f"Portal configuration: {config.id}")
return config
def list_portal_configurations(limit: int = 10):
"""List billing portal configurations."""
ensure_stripe_key()
return stripe.billing_portal.Configuration.list(limit=limit)
if __name__ == "__main__":
ensure_stripe_key()
print("=== Create portal session for customer ===")
# session = create_portal_session("cus_xxx")
# Redirect user to: session.url
Artigos relacionados
config.py
config.py — python source code from the stripe demo learning materials (stripe_demo/config.py).
Ler artigo →00_error_handling.py
00_error_handling.py — python source code from the stripe demo learning materials (stripe_demo/examples/00_error_handling.py).
Ler artigo →01_customers.py
01_customers.py — python source code from the stripe demo learning materials (stripe_demo/examples/01_customers.py).
Ler artigo →02_products_prices.py
02_products_prices.py — python source code from the stripe demo learning materials (stripe_demo/examples/02_products_prices.py).
Ler artigo →03_payment_intents.py
03_payment_intents.py — python source code from the stripe demo learning materials (stripe_demo/examples/03_payment_intents.py).
Ler artigo →04_checkout_sessions.py
04_checkout_sessions.py — python source code from the stripe demo learning materials (stripe_demo/examples/04_checkout_sessions.py).
Ler artigo →