Subscribing a customer to a planΒΆ
See this example from tests.apps.example.views.PurchaseSubscriptionView.form_valid
# Create the stripe Customer, by default subscriber Model is User,
# this can be overridden with settings.DJSTRIPE_SUBSCRIBER_MODEL
customer, created = djstripe.models.Customer.get_or_create(subscriber=user)
# Add the source as the customer's default card
customer.add_card(stripe_source)
# Using the Stripe API, create a subscription for this customer,
# using the customer's default payment source
stripe_subscription = stripe.Subscription.create(
customer=customer.id,
items=[{"plan": plan.id}],
billing="charge_automatically",
# tax_percent=15,
api_key=djstripe.settings.STRIPE_SECRET_KEY,
)
# Sync the Stripe API return data to the database,
# this way we don't need to wait for a webhook-triggered sync
subscription = djstripe.models.Subscription.sync_from_stripe_data(
stripe_subscription
)