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?
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
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.
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:
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).
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With