stripe api checking for existing card

taekni picture taekni · Mar 24, 2015 · Viewed 11.8k times · Source

I'm sure I'm missing something obvious here, but I can't get my head around how to check for an existing card against a customer.

I'm using the stripe connect api within an laravel app to manage payments on behalf of others, and the basic process is as follows:

  • a stripe token is created via stripe.js and submitted with the payment form
  • if the customer exists in the local database, I grab their stripe_id, otherwise a new customer is created using the token as the source/card
  • a charge is then created using the retrieved or new customer stripe_id

Currently, if the customer returns and uses a different card, as the charge only includes a customer, not source, it'll be charged against their default card regardless.

What I'd like to do is:

  • create a stripe token
  • check customer against local database etc
  • check card fingerprint against customer's cards
  • if necessary, create new card on customer's record
  • create charge using both customer and card ids

Simply put: I can't see where in the process a persistent card_id is generated; both those used in the stripe.js response, and when created in the stripe dashboard, appear to be unique, meaning every charge creates a brand-new card object in stripe.

I know I can retrieve a list of cards stored against a customer's account - but where do I get the initial card_id from to search against?

I've seen a question that touches on this here - Can I check whether stripe a card is already existed before going to create new one? - but I don't know Ruby, so can't make head nor tail of it.

EDIT:

Simpler version - is there a way to get the fingerprint as described in the stripe docs here - https://stripe.com/docs/api/php#card_object - without having to first create a card object ?

Answer

koopajah picture koopajah · Mar 24, 2015

So the idea here would be to use the fingerprint on the Card object or the Token object and not the id itself as those would be different if you add the same card multiple times.

When you get a new card token you can retrieve it through the Retrieve Token API and look for the fingerprint in the card hash.

You would keep a list of known fingerprints in your database associated with a specific customer and/or card so that you can detect duplicate cards.

NOTE: make sure you are using the secret keys to get those information. otherwise if you are using the publishable key, you might not get the fingerprint value.