Protecting Cards with Stripe in Laravel: A Simple Guide

How to save all type of cards using stripe in Laravel PHP


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 this article we will learn how can we add or save different type of cards in stripe without charging anything from customer in Laravel.

This process of saving cards in stripe used to save customers payment details for later use, so that the customer do not need to provide payment detail again and again and can make hassle free transaction.

To save a cards detail on stripe we basically needs two thing -

  • card token
  • customer id

Card Token :-

Card token is basically generated using stripe JS , you can also create a card token from your Laravel or PHP code but its not recommended . So what happens is you have to implement Stripe JS in the frontend code so that whenever user provides his/her card detail your JS code will generate a card token and send it to backend and in the backend (Laravel or PHP) you will get the card token and use it to save the card . The card token is similar to as below .


Customer ID :-

You need a customer id to save a card because you have to tell stripe for which customer this card belongs. You might create a new customer each time you are saving a card or you can create a customer one time on behalf of his email id and store it on your local DB for late use . The customer id is similar to as below .


When you have both card token and customer id you can use the following code to save card on stripe .

public function saveCard(Request $request){
        // Get card token from frontend / stripe JS

        $card_token = $request->token;  // tok_***********************

        // fetch your customer ID from your DB or create a new customer

        $customer_id = "cus_KFaOSB6b3g5pZb";

        // Get stripe secret key from your constants file

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

        $stripe = new \Stripe\StripeClient(
        // create and save card on stripe
        $card_response = $stripe->customers->createSource(
            'source' => $card_token
        // From the response you can store the partial card detail on your local DB also
        return $card_response;

Output Response from stripe :-

    "id": "card_1JdrFeSHkP5axHyf0LYuWVku",
    "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_KFaOSB6b3g5pZb",
    "cvc_check": "pass",
    "dynamic_last4": null,
    "exp_month": 9,
    "exp_year": 2023,
    "fingerprint": "56e9LqPpKviLmqNp",
    "funding": "credit",
    "last4": "4242",
    "metadata": [],
    "name": null,
    "tokenization_method": null

You can use these details to store on on your local DB also .

Previous Post Next Post

Contact Form