Search code examples
pythondjangoemailstripe-paymentsstripe-customers

Retrieve Stripe customer by E-mail | Django


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!


Solution

  • 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