Gammer
Gammer

Reputation: 5608

Stripe : Add new card to already created customer

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" 
          )
       )
    );
    

Upvotes: 1

Views: 2506

Answers (2)

lee
lee

Reputation: 141

It seems like the answer from @karllekko is outdated now. I found the following that seems to work good currently.

$email = $_POST['email'];
$customers = \Stripe\Customer::all(['email' => $email]);
$customer = $customers->data[0];    
$token = $_POST['stripeToken'];
$newCard = \Stripe\Customer::createSource($customer->id, ['source' => $token]);

https://docs.stripe.com/api/cards/create

Upvotes: 0

karllekko
karllekko

Reputation: 7198

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

Upvotes: 2

Related Questions