Stripe:更改信用卡号?

Stripe: change credit card number?

我正在使用 Stripe Payments,我想让客户能够更改他们的信用卡。参考 https://stripe.com/docs/api#create_subscription -> source,我尝试了以下 PHP-代码:

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

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

这有效,但不幸的是它无意中也创建了一个新的客户 ID:

原来的客户ID是cus_6elZAJHMELXkKI,我想保留它。

有人知道 PHP 代码可以在不创建新客户的情况下更新卡片吗?

非常感谢您!

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

我刚刚找到了答案,也许它对你们中的某些人也有帮助:

您可以像这样用新卡替换旧卡:

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

答案帮了大忙,但评论者说旧卡没有被删除是正确的。

假设您只会为一位客户准备一张卡,您会这样做:

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

//get the only card's ID
$card_id=$customer->sources->data[0]->id;
//delete the card if it exists
if ($card_id) $customer->sources->retrieve($card_id)->delete();

//add new card
$new_card = $customer->sources->create(array("source" => $token));
$customer->default_source = $new_card->id;
$customer->save();