12_balance_and_events.py
stripe_demo/examples/12_balance_and_events.py
"""
Stripe Balance, Balance Transactions, and Events.
Balance: available and pending funds. Balance Transactions: ledger of moves.
Events: audit log of what happened (also used for webhooks).
Docs: https://docs.stripe.com/api/balance, balance_transactions, events
"""
import stripe
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from config import STRIPE_SECRET_KEY, ensure_stripe_key
stripe.api_key = STRIPE_SECRET_KEY
def retrieve_balance():
"""Get current balance (available and pending by currency)."""
ensure_stripe_key()
balance = stripe.Balance.retrieve()
for amount in balance.available:
print(f" available: {amount.amount/100} {amount.currency}")
for amount in balance.pending:
print(f" pending: {amount.amount/100} {amount.currency}")
return balance
def list_balance_transactions(limit: int = 10, type: str = None):
"""List balance transactions (charges, refunds, payouts, etc.)."""
ensure_stripe_key()
params = {"limit": limit}
if type:
params["type"] = type
bt = stripe.BalanceTransaction.list(**params)
for t in bt.data:
print(f" {t.id} | {t.amount/100} {t.currency} | {t.type} | {t.created}")
return bt
def retrieve_balance_transaction(txn_id: str):
"""Retrieve a single balance transaction."""
ensure_stripe_key()
return stripe.BalanceTransaction.retrieve(txn_id)
def list_events(limit: int = 10, type: str = None):
"""List recent events (e.g. charge.succeeded, payment_intent.created)."""
ensure_stripe_key()
params = {"limit": limit}
if type:
params["type"] = type
events = stripe.Event.list(**params)
for e in events.data:
print(f" {e.id} | {e.type} | {e.created}")
return events
def retrieve_event(event_id: str):
"""Retrieve a single event (includes data.object)."""
ensure_stripe_key()
return stripe.Event.retrieve(event_id)
if __name__ == "__main__":
ensure_stripe_key()
print("=== Balance ===")
retrieve_balance()
print("=== Balance transactions ===")
list_balance_transactions(limit=5)
print("=== Events ===")
list_events(limit=5)
相關文章
config.py
config.py — python source code from the stripe demo learning materials (stripe_demo/config.py).
閱讀文章 →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).
閱讀文章 →