how to prevent user to add duplicate card in stripe using laravel

Solutions on MaxInterview for how to prevent user to add duplicate card in stripe using laravel by the best coders in the world

showing results for - "how to prevent user to add duplicate card in stripe using laravel"
Matthias
16 Jan 2020
1$stripe = new StripeClient('STRIPE_SECRET_KEY');
2
3$cards = $stripe->paymentMethods->all(['customer' => 'CUSTOMER_ID', 'type' => 'card']);
4
5$fingerprints = [];
6
7foreach ($cards as $card) {
8   $fingerprint = $card['card']['fingerprint'];
9
10    if (in_array($fingerprint, $fingerprints, true)) {
11      $stripe->paymentMethods->detach($card['id']);
12   } else {
13     $fingerprints[] = $fingerprint;
14   }
15}
Pia
10 Jan 2021
1public function checkBankingDetailsAlert()
2  {
3    $user = Auth::user();
4    $stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));
5    $cards = $stripe->paymentMethods->all(['customer' => $user->stripe_customer_id, 'type' => 'card']);
6    $fingerprints = [];
7    foreach ($cards as $card) {
8       $fingerprint = $card['card']['fingerprint'];
9        if (in_array($fingerprint, $fingerprints, true)) {
10          $stripe->paymentMethods->detach($card['id']);
11       } else {
12         $fingerprints[] = $fingerprint;
13       }
14    }
15    return response()->json([
16      'status' => 'Success',
17      'fingerprint' => $fingerprints,
18    ]);
19  }
20