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 - django How to create superuser if does not exists on migration?

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.