config.py
stripe_demo/config.py
"""
Stripe configuration and initialization.
Load API key from STRIPE_SECRET_KEY environment variable.
Use test keys (sk_test_...) for development; live keys (sk_live_...) for production.
"""
import os
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
pass
# Secret key: never expose in client-side code. Use for server-side API calls.
STRIPE_SECRET_KEY = os.environ.get("STRIPE_SECRET_KEY", "")
# Publishable key: safe to use in browser/mobile apps for Stripe.js / mobile SDKs.
STRIPE_PUBLISHABLE_KEY = os.environ.get("STRIPE_PUBLISHABLE_KEY", "")
# Webhook signing secret: verify webhook events (get from Stripe CLI or Dashboard).
STRIPE_WEBHOOK_SECRET = os.environ.get("STRIPE_WEBHOOK_SECRET", "")
# Currency and locale defaults
DEFAULT_CURRENCY = "usd"
DEFAULT_SUCCESS_URL = os.environ.get("SUCCESS_URL", "https://example.com/success")
DEFAULT_CANCEL_URL = os.environ.get("CANCEL_URL", "https://example.com/cancel")
def ensure_stripe_key():
"""Raise if STRIPE_SECRET_KEY is not set."""
if not STRIPE_SECRET_KEY or not STRIPE_SECRET_KEY.startswith("sk_"):
raise ValueError(
"Set STRIPE_SECRET_KEY in environment or .env. "
"Get keys at https://dashboard.stripe.com/apikeys"
)
相关文章
00_error_handling.py
00_error_handling.py — python source code from the stripe demo learning materials (stripe_demo/examples/00_error_handling.py).
阅读文章 →01_customers.py
01_customers.py — python source code from the stripe demo learning materials (stripe_demo/examples/01_customers.py).
阅读文章 →02_products_prices.py
02_products_prices.py — python source code from the stripe demo learning materials (stripe_demo/examples/02_products_prices.py).
阅读文章 →03_payment_intents.py
03_payment_intents.py — python source code from the stripe demo learning materials (stripe_demo/examples/03_payment_intents.py).
阅读文章 →04_checkout_sessions.py
04_checkout_sessions.py — python source code from the stripe demo learning materials (stripe_demo/examples/04_checkout_sessions.py).
阅读文章 →05_subscriptions.py
05_subscriptions.py — python source code from the stripe demo learning materials (stripe_demo/examples/05_subscriptions.py).
阅读文章 →