[Python] Stripe Subscription: latest_invoice.payment_intent raises AttributeError:...

Discussão em 'Python' iniciado por Stack, Agosto 8, 2025.

  1. Stack

    Stack Membro Participativo

    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
    ​
    What I've checked so far

    • The subscription is created successfully.
    • The parameter expand=['latest_invoice.payment_intent'] is included to embed the payment_intent object.
    • I've verified that the invoice (latest_invoice) exists.
    • However, the payment_intent field is missing from the latest_invoice object.
    Questions

    • Why is payment_intent missing from latest_invoice when creating the subscription with payment_behavior='default_incomplete' and expanding the field?
    • How should I obtain the client_secret to confirm payment on the frontend using Stripe.js in these cases?

    Continue reading...

Compartilhe esta Página