Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stripe | Useful Code Snippets #191

Open
ngupta23 opened this issue Oct 28, 2024 · 0 comments
Open

Stripe | Useful Code Snippets #191

ngupta23 opened this issue Oct 28, 2024 · 0 comments
Labels
python Anything related to Python stripe

Comments

@ngupta23
Copy link
Owner

ngupta23 commented Oct 28, 2024

Common Imports

import stripe
stripe.api_key = "YOUR_STRIPE_API_KEY"

Customer

Listing Customers: Reference

You can list between 1-100 customers using limit. limit defaults to 10

# List 3 customers
stripe.Customer.list(limit=3)

If you want to retrieve a customer that has a test clock associated, you need to pass the test clock ID, else they will not be retrieved, e.g.

test_clock: string
Provides a list of customers that are associated with the specified test clock. The response will not include customers with test clocks if this parameter is not set.

# Need to specify the test clock, else the customer is not returned
customers = stripe.Customer.list(email="somebody@example.com", test_clock=test_clock.id)

Updating Customers: Reference

Useful for updating name, address, email, metadata, invoice settings, etc (see reference above).

stripe.Customer.modify("cus_NffrFeUfNV2Hib", metadata={"order_id": "6735"})

Price

Creating a Price: Reference

# Create a new price ID in Stripe
new_price = stripe.Price.create(
    unit_amount=2000,  # Amount in cents
    currency="usd",
    recurring={"interval": "month"},
    product_data={"name": "New Monthly Plan"},
)
price_id = new_price.id

Subscriptions

Create a Subscription: Reference

subscription = stripe.Subscription.create(
    customer=customer.id,
    items=[{"price": "your_price_id"}],
    trial_period_days=14,  # If you want your custom to be on a trial period
    payment_settings={"save_default_payment_method": "on_subscription"},
    trial_settings={"end_behavior": {"missing_payment_method": "cancel"}},
    )

Listing Subscriptions: Reference

status="all" lists all types of subscriptions associated with a customer. If no value is supplied, all subscriptions that have not been canceled are returned.

subscriptions = processor.stripe.Subscription.list(customer=customer_id, status="all")

Retrieving a Subscription" Reference

subscription = stripe.Subscription.retrieve(subscription_id)

Updating a subsctiption: Reference

stripe.Subscription.modify("sub_id", metadata={"order_id": "6735"})

Payment Method

Creating a Payment Method: Reference

# Create a test credit card for testing purposes
payment_method = stripe.PaymentMethod.create(
    type="card",
    card={"token": "tok_visa"},  # Test Visa token from Stripe
)

Once created, this payment method can be attached to a customer and the default payment method can be set to this payment

stripe.PaymentMethod.attach(payment_method.id, customer=customer_id)
stripe.Customer.modify(customer_id, invoice_settings={"default_payment_method": payment_method.id},
)

Auto Pagination

Check out the details here: Reference

import stripe
stripe.api_key = "my_api_key"
customers = stripe.Customer.list(limit=3)
for customer in customers.auto_paging_iter():
  # Do something with customer

# Starting with a batch of 100 subscriptions, then iterate over more batches
subscriptions = stripe.Subscription.list(limit=100)
for subscription in subscriptions.auto_paging_iter():
    # Do something with subscriptions, e.g.
    subscripton_id = subscription["id"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python Anything related to Python stripe
Projects
None yet
Development

No branches or pull requests

1 participant