Stripe : Add new card to already created customer

2.1k Views Asked by At

I have a stripe customer already added, I am figuring out to add new card to customer. I searched around but couldn't found anything confirmed to asnwer my following questions.

  1. Do stripe have any form of their own to add new card ?
  2. Is following is the correct way to add new card ?

    $customer = \Stripe\Customer::retrieve(Auth::user()->stripe_key);
    // Got the customer details successfully from the above call.  
    $card = $customer->cards->create(
      array( 
        "card" => 
          array( 
            "number"=> "4242424242424242", 
            "exp_month" => "12", 
            "exp_year" => "2016", 
            "cvc" => "123" 
          )
       )
    );
    
1

There are 1 best solutions below

0
karllekko On

Stripe does not have a direct form specifically for adding a new card to a customer, however you can use Checkout or Elements to collect the customer's card details.

The process for adding a new card to a customer would be as follows:

  1. Collect and tokenize the customer's card details using Checkout or Elements[0]. This will give you a Stripe token representing the card.
  2. Send this token to your backend, where you can use something similar to the following code to save the card to the customer:
$token = $_POST['stripeToken']; #for example
$customer = \Stripe\Customer::retrieve(Auth::user()->stripe_key);
$customer->sources->create(array("source" => $token));

[0] - https://stripe.com/docs/checkout or https://stripe.com/docs/stripe-js/elements/quickstart

[1] - https://stripe.com/docs/api/php#create_card