Listing Customer Cards on Stripe: A Simple Laravel How-To

 How to fetch all cards of customer from stripe in Laravel - PHP

Listing-Customer-Cards-on-Stripe-A-Simple-Laravel-How-To

Stripe is a most common and trusted payment gateway used by lots of businesses and companies and it provides lots of functionalities to make payment more smoother, faster and secure in no time. Stripe provides every service to fulfill your requirement for implementing payment gateway to your business.

We have already discussed about why to use stripe , how to create a customer , how to save a card and how to create a token and continuing this series , in this article we will see how to fetch all the cards saved by a customer  in Stripe.

Card details of a customer is very sensitive, so while saving the cards we are storing card details on stripe rather than our server databases. So while fetching card details we have to use stripe's service to fetch card details of a customer.

You can refer the following codes to fetch card details of a user from stripe.


PaymentController.php :-

On your controller , you need customer ID to fetch his/her card details as shown below.


public function listCard(Request $request){
      try{
        // Get the customer_id from your database or from request body
        $customer_id = $request->customer_id;
        // Connect to your stripe account

        $stripe_account = Config('constants.Stripe_account');
        $stripe_secret = $stripe_account['secret_key'];

        $stripe = new \Stripe\StripeClient(
          $stripe_secret
        );

        $cards = $stripe->customers->allSources(
          $customer_id,
          ['object' => 'card', 'limit' => 3]
          // you can remove the limit key to get all the cards
        );
        return $cards;

      }
      catch(\Exception $e){
        Log::error(
          'Failed to fetch user cards',
          ['message' => $e->getMessage(), 'trace' => $e->getTraceAsString()]
        );
        return false;
      }
    }


Output:-

{
    "object": "list",
    "data": [
        {
            "id": "card_1KKnvLSGh31t2ygOhtFm3GU2",
            "object": "card",
            "address_city": null,
            "address_country": null,
            "address_line1": null,
            "address_line1_check": null,
            "address_line2": null,
            "address_state": null,
            "address_zip": null,
            "address_zip_check": null,
            "brand": "Visa",
            "country": "US",
            "customer": "cus_L0pWRQZ5SnhP0i",
            "cvc_check": "pass",
            "dynamic_last4": null,
            "exp_month": 9,
            "exp_year": 2023,
            "fingerprint": "u6Dg2RZXAL6RvET8",
            "funding": "credit",
            "last4": "4242",
            "metadata": [],
            "name": null,
            "tokenization_method": null
        },
        {
            "id": "card_1KKnxLSGh31t2ygOsDFJtt3G",
            "object": "card",
            "address_city": null,
            "address_country": null,
            "address_line1": null,
            "address_line1_check": null,
            "address_line2": null,
            "address_state": null,
            "address_zip": null,
            "address_zip_check": null,
            "brand": "MasterCard",
            "country": "US",
            "customer": "cus_L0pWRQZ5SnhP0i",
            "cvc_check": "pass",
            "dynamic_last4": null,
            "exp_month": 9,
            "exp_year": 2023,
            "fingerprint": "vHtRKW7u9Ii2o0IH",
            "funding": "credit",
            "last4": "4444",
            "metadata": [],
            "name": null,
            "tokenization_method": null
        }
        
    ],
    "has_more": false,
    "url": "/v1/customers/cus_L0pWRQZ5SnhP0i/sources"
}


Previous Post Next Post

Contact Form