Stripe api check existing map

I'm sure I'm missing something obvious here, but I can't figure out how to check an existing card for a client.

I use the Stripe Connect API in the Laravel application to manage payments on behalf of others, and the main process is as follows:

  • the token strip is created using stripe.js and sent with the payment form
  • if the client exists in the local database, I take it stripe_id , otherwise a new client is created using the token as a source / map
  • then a charge is created using the found or new client stripe_id

Currently, if a customer returns and uses a different card, since the fee includes only the client and not the source, he will be debited from the card by default, regardless.

I would like to do the following:

  • create a token strip
  • check customer with local database, etc.
  • check card fingerprint on customer cards
  • if necessary, create a new card in the customer record
  • create a charge using customer and card identifiers

Simply put: I do not see where in the process a constant card_id generated; both the ones used in stripe.js answer and when creating stripes on the dashboard seem unique, which means that each charge creates a new map object in the strip.

I know that I can get a list of cards stored in the client’s account, but where can I get the initial card_id for the search?

I saw a question that addresses this question here - Can I check if a map strip exists before creating a new one? β€œBut I don’t know Ruby, so I can’t make out a single tail of it.

EDIT:

The simplified version - is there a way to get a fingerprint , as described here in the strip documents - https://stripe.com/docs/api/php#card_object - without having to first create a map object?

+12
php laravel stripe-payments stripe-connect
source share
2 answers

So, the idea here would be to use a fingerprint on a Card or Token object, not the identifier itself, since they will be different if you add the same card several times.

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

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

NOTE. make sure you use secret keys to get this information. otherwise, if you use a publicly accessible key, you may not receive the fingerprint value.

+19
source share

I created a function for this:

  • $customer is a strip $customer object
  • $stripe_account is either your account id or connected account id
  • $token comes from stripe.js elements
  • $check_exp allows you to decide whether you also want to check the expiration date of the card, because fingerprint does not change if the card number matches
  • PHP API patch 7.0.0

     function check_duplicate_card($customer, $stripe_account, $token, $check_exp) { $loc = "check_duplicate_card >> "; $debug = true; if ($debug) { // see here for an explanation for logging: http://php.net/set_error_handler >> Examples trigger_error("$loc started", E_USER_NOTICE); } try { // get token data $response = \Stripe\Token::retrieve( $token, ["stripe_account" => $stripe_account] ); $token_fingerprint = $response->card->fingerprint; $token_exp_month = $response->card->exp_month; $token_exp_year = $response->card->exp_year; if ($debug) { trigger_error("$loc token_fingerprint = $token_fingerprint; token_exp_month = $token_exp_month; token_exp_year = $token_exp_year", E_USER_NOTICE); } // check for duplicate source if ($debug) { trigger_error("$loc customer sources = " . json_encode($customer->sources), E_USER_NOTICE); } $duplicate_found = false; foreach ($customer->sources->data as &$value) { // get data $fingerprint = $value->fingerprint; $exp_month = $value->exp_month; $exp_year = $value->exp_year; if ($fingerprint == $token_fingerprint) { if ($check_exp) { if (($exp_month == $token_exp_month) && ($exp_year == $token_exp_year)) { $duplicate_found = true; break; } } else { $duplicate_found = true; break; } } } if ($debug) { trigger_error("$loc duplicate_found = " . json_encode($duplicate_found), E_USER_NOTICE); } } catch (Exception $e) { if ($e instanceof \Stripe\Exception\ApiErrorException) { $return_array = [ "status" => $e->getHttpStatus(), "type" => $e->getError()->type, "code" => $e->getError()->code, "param" => $e->getError()->param, "message" => $e->getError()->message, ]; $return_str = json_encode($return_array); trigger_error("$loc $return_str", E_USER_WARNING); http_response_code($e->getHttpStatus()); echo $return_str; } else { $return_array = [ "message" => $e->getMessage(), ]; $return_str = json_encode($return_array); trigger_error("$loc $return_str", E_USER_ERROR); http_response_code(500); // Internal Server Error echo $return_str; } } if ($debug) { trigger_error("$loc ended", E_USER_NOTICE); } return $duplicate_found; } 
0
source share

All Articles