Search code examples
phpstripe-paymentscredit-card

Stripe: change credit card number?


I'm using Stripe Payments and would like to give customers the possibility to change their credit card. Referring to https://stripe.com/docs/api#create_subscription -> source, I tried the following PHP-code:

        $customer = \Stripe\Customer::retrieve($client_id);

        $customer = \Stripe\Customer::create(array(
        "source" => $token) //the token contains credit card details
        );

This works, but unfortunately it unintentionally also creates a new customer ID:

Stripe Dashboard

The original customer ID was cus_6elZAJHMELXkKI and I would like to keep it.

Does anybody know the PHP-code that would update the card without creating a new customer?

Thank you very much in advance!

PS: Just in case you need it – this was the code that originally created the customer and the subscription:

$customer = \Stripe\Customer::create(array(
    "source" => $token,
    "description" => "{$fn} {$ln}",
    "email" => $e,
    "plan" => "basic_plan_id")
 );

\Stripe\Charge::create(array(
  "amount" => 10000, # amount in cents, again
  "currency" => "eur",
  "customer" => $customer->id)
);

Solution

  • I've just found the answer, maybe it helps someone of you, too:

    You can replace the old card with the new one like so:

    $customer = \Stripe\Customer::retrieve($client_id);
    $new_card = $customer->sources->create(array("source" => $token));
    $customer->default_source = $new_card->id;
    $customer->save();