Skip to content

PHP code snippet – How to prevent user to add duplicate card in stripe using laravel?

$stripe = new StripeClient('STRIPE_SECRET_KEY');

$cards = $stripe->paymentMethods->all(['customer' => 'CUSTOMER_ID', 'type' => 'card']);

$fingerprints = [];

foreach ($cards as $card) {
   $fingerprint = $card['card']['fingerprint'];

    if (in_array($fingerprint, $fingerprints, true)) {
      $stripe->paymentMethods->detach($card['id']);
   } else {
     $fingerprints[] = $fingerprint;
   }
}
See also  Python code snippet - How to check if there are duplicates in a list ?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.