【问题标题】:Stripe: change credit card number?条纹:更改信用卡号码?
【发布时间】:2015-10-16 03:43:29
【问题描述】:

我正在使用 Stripe Payments,并希望让客户能够更改他们的信用卡。参考https://stripe.com/docs/api#create_subscription -> 源码,我尝试了以下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:以防万一您需要它 - 这是最初的代码 创建了客户和订阅:

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

【问题讨论】:

    标签: php stripe-payments credit-card


    【解决方案1】:

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

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

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

    【讨论】:

    • 嗨,感谢您提供此代码。我尝试了此代码,但它不能替换旧卡。这样做是存储新卡并将新卡设置为默认卡。旧卡仍在条纹客户的帐户中。
    【解决方案2】:

    答案帮助了一堆,但评论者是正确的,旧卡没有被删除。

    假设您只会为客户提供一张卡,您会这样做:

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

    【讨论】:

      猜你喜欢
      • 2013-12-02
      • 1970-01-01
      • 2019-02-25
      • 2015-11-10
      • 2016-10-22
      • 2019-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多