14_payment_methods.py
stripe_demo/examples/14_payment_methods.py
"""
Stripe Payment Methods – attach, detach, list cards and other methods.
Docs: https://docs.stripe.com/api/payment_methods
"""
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 list_payment_methods(customer_id: str, type: str = "card", limit: int = 10):
"""List payment methods attached to a customer. type: 'card'|'us_bank_account'|etc."""
ensure_stripe_key()
methods = stripe.PaymentMethod.list(customer=customer_id, type=type, limit=limit)
for pm in methods.data:
print(f" {pm.id} | {pm.type} | {getattr(pm.card, 'last4', '-')}")
return methods
def retrieve_payment_method(payment_method_id: str):
"""Retrieve a payment method by ID."""
ensure_stripe_key()
return stripe.PaymentMethod.retrieve(payment_method_id)
def attach_payment_method(payment_method_id: str, customer_id: str):
"""Attach a payment method to a customer (e.g. after SetupIntent confirm)."""
ensure_stripe_key()
return stripe.PaymentMethod.attach(payment_method_id, customer=customer_id)
def detach_payment_method(payment_method_id: str):
"""Detach a payment method from a customer."""
ensure_stripe_key()
return stripe.PaymentMethod.detach(payment_method_id)
def set_default_payment_method(customer_id: str, payment_method_id: str):
"""Set a payment method as the customer's default for subscriptions/invoices."""
ensure_stripe_key()
return stripe.Customer.modify(
customer_id,
invoice_settings={"default_payment_method": payment_method_id},
)
if __name__ == "__main__":
ensure_stripe_key()
print("=== List payment methods for customer ===")
# list_payment_methods("cus_xxx")
print("=== Attach/Detach/Set default are used after SetupIntent or PaymentElement. ===")
관련 글
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).
글 읽기 →