0

I'm trying a way to figure out how to retrieve the Stripe Customer by e-mail:

In my views, I wrote the below code:

    if stripe.Customer.retrieve(email=request.POST['email']):
        customer = stripe.Customer.retrieve(id)
    else:
        customer = stripe.Customer.create(
            email=request.POST['email'],
            name=request.POST['nickname'],
        )

    charge = stripe.Charge.create(
        customer=customer,
        amount=500,
        currency='brl',
        description='First Test Ever!',
        source=request.POST['stripeToken'],
    )

Basically, if the customer is found by their e-mail, so I don't need to create a new account. Otherwise, the account is created. The last step is to charge either the new customer or the existent one.

The code is not working properly. I assume this is the wrong way to search for an existent customer.

Any help?

Thank you!

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Felipe Dourado
  • 441
  • 2
  • 12
  • Does this answer your question? [Stripe, is it possible to search a customer by their email?](https://stackoverflow.com/questions/26767150/stripe-is-it-possible-to-search-a-customer-by-their-email) – JPG Jul 25 '20 at 15:52

2 Answers2

1

You can't retrieve a single customer by email because you can have multiple customers with the same email address - it is not a unique value.

Instead, you can list [0] customers matching that email and then decide for yourself if any of the results are the one you want.

[0] https://stripe.com/docs/api/customers/list#list_customers-email

Brendan Moore
  • 1,131
  • 7
  • 13
0

With "stripe.Customer.list()" and "stripe.Customer.search()", you can get customers by email.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129