I'm trying to create a subscription in Stripe using Python and the official library, with the goal of obtaining the client_secret
from the initial invoice's PaymentIntent
to complete payment on the frontend.
Here is the code I'm using:
import stripe
stripe.api_key = 'sk_test_...'
price_id = 'price_...' # Example
subscription = stripe.Subscription.create(
customer=customer.id,
items=[{'price': price_id}],
payment_behavior='default_incomplete',
expand=['latest_invoice.payment_intent'],
)
I want to access: subscription.latest_invoice.payment_intent.client_secret
But I get the error:
AttributeError: payment_intent
expand=['latest_invoice.payment_intent']
is included to embed the payment_intent
object.latest_invoice
) exists.payment_intent
field is missing from the latest_invoice
object.payment_intent
missing from latest_invoice
when creating the subscription with payment_behavior='default_incomplete'
and expanding the field?client_secret
to confirm payment on the frontend using Stripe.js in these cases?This part of the API was changed by Stripe in their latest major API version called Basil. The stripe-python SDK is pinned to the latest API version so if you are using the most recent version you have that newer API version. I recommend carefully reading their changelog here that covers all breaking changes for each API version.
In your specific case, Stripe removed the connection between the Invoice and its PaymentIntent to add support for multiple partial payments on one Invoice. You can read more about this in this specific changelog entry where they explain that instead of looking at the PaymentIntent's client_secret
, you can access this directly on the Invoice in the confirmation_secret
property that you also have to expand.
Your code should be
subscription = stripe.Subscription.create(
customer=customer.id,
items=[{'price': price_id}],
payment_behavior='default_incomplete',
expand=['latest_invoice.confirmation_secret'],
)