Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripe - check if a customer exists

In the stripe documentation they have this:

customer = Stripe::Customer.create(email: params[:stripeEmail], card: params[:stripeToken])
charge = Stripe::Charge.create(customer: customer.id, amount: price, description: '123', currency: 'usd')

But I think it's wrong as for each payment we have to check if a customer exists first, I just tried it with a test account and it turned out there were created a number of different customers with the same email but different ids.

How do I check if a customer already exists?

like image 908
Incerteza Avatar asked Dec 21 '14 09:12

Incerteza


People also ask

How do I check if a card is valid using stripe?

Get your questions answered and find international support for Stripe. Check if a card is valid without a charge You can verify the card details, as well as see the results of the CVC and zip code checks, by saving the customer’s card in Stripe. Additional Information

Why are zip checks not showing up on stripe?

Additional Information To prevent card testing, Stripe is sometimes required to not check cvc and zip checks on card validations, so they may appear as “unavailable”. Those checks will be available on the charge object once an actual payment is made. Stripe uses machine learning to predict when card testing is likely.

Does stripe store duplicate cards when using the token?

I want to check using the Stripe token whether a newly entered card already exists or not. It will use it if it's already there, if not it will create a new card. Show activity on this post. Unfortunately while working on Stripe today I noticed that it do allows storing of duplicate cards. To avoid this, I did following steps:

Why is stripe caching my card data?

It sounds like you're caching the card data locally to be able to display it to the customer. If that is correct, Stripe provides a fingerprint for each card/token which you can begin storing in the card records (if you're not already).


3 Answers

There is no check on Stripe's end to ensure uniqueness of customers (email, card, name, etc,) and this is something you have to do on your end.

Usually, when you create a customer with a specific email address you need to associate the customer id you got back from the API with the email address. Then next time, you check whether this email address is already in your system and either create a new customer or re-use the customer id from the previous time.

like image 88
koopajah Avatar answered Oct 22 '22 03:10

koopajah


You may check if the customer exists or not by calling GET /customers with the email as a form-urlencoded parameter. You will get 200 OK response but the returned data[] will be empty if this customer email is not there.

https://stripe.com/docs/api/customers/list
like image 34
MSaudi Avatar answered Oct 22 '22 02:10

MSaudi


I think a simple "exists" test is essential to avoid unwanted exceptions being generated.

If your customer is new then they won't have a stripe ID, which is fine. However you might get a situation where you are using an existing customer with an existing stripe ID, but have performed some sort of dormant clean-up Stripe side. In which case its good practice to test the supplied stripeID to see if its still valid.

For example I store the stripeID with the customer profile. When I make a payment I check to see if the stripeID field in the profile exists. If it does I fire off a API request to my backend to check its existence.

Something like this (.net backend example).

    [HttpGet]
    public ActionResult<bool> CheckCustomer(string id)
    {

        var service = new CustomerService();
        Customer cust;

        try
        {
            service.Get(id);
        }
        catch
        {
            return Ok(false);
        }

        if (cust.Deleted ?? false)
        {
            return Ok(false);
        }

        return Ok(true);
    }

I don't need the result, just it not throwing an exception is good enough. However, when you delete customers in Stripe live it keeps a copy of the ID stub and sets the delete property to true, so you need to check for that too.

The front end then can either use the stripeId for the payment or create a new stripe customer first then take the payment.

like image 1
Darren Street Avatar answered Oct 22 '22 03:10

Darren Street