0

Here's the relevant code. I'm using Django, with stripe-python (2.46.0) along with dj-stripe (2.3.0).

import stripe
from djstripe.models import Customer

customer, created = Customer.get_or_create(subscriber=my_django_user)
stripe_session = stripe.checkout.Session.create(
    customer=customer,
    ...
)

When the my_django_user.email field is empty, this works just fine and the stripe_session is created successfully. But if the email is set, the session creation fails with "No such customer":

stripe.error.InvalidRequestError: Request req_bla: No such customer: <my_django_user's email>

Please correct me if I'm wrong but I don't think this is duplicate of this question because Customer.get_or_create makes an API request to stripe and so does stripe.checkout.Session.create, and IIUC both requests are handled under the same account. (Also the above code works with a brand new user, as long as the email's empty.)

Thanks in advance for any help!

Zevgon
  • 556
  • 4
  • 13

1 Answers1

1

The customer field is supposed to be an ID on an existing customer in Stripe. You're passing in a Customer model instance into that field in your code.

You should probably pass in customer.email into the customer_email field as:

stripe_session = stripe.checkout.Session.create(
    customer_email=customer.email,
    ...
)

Have a read through the docs here.

Wiggy A.
  • 496
  • 3
  • 16
  • You're exactly right, thanks so much @Wiggy A.! Works perfectly when I pass in the id instead. Seems so obvious in hindsight because stripe itself doesn't know about Django models. I guess I've been spoiled by Django's db-querying syntax XD – Zevgon Apr 26 '20 at 10:06