簡體   English   中英

添加新卡時出現 PHP Stripe API 錯誤

[英]PHP Stripe API error on adding new card

我試圖向現有客戶添加一張新卡並收到一個錯誤,我不明白為什么會發生這種情況。 這是代碼示例:

 $strp_customer = \\Stripe\\Customer::retrieve($customer['stripe_id']); echo $strp_customer->id; echo "<br>"; echo $_POST['stripeToken']; echo "<br>"; echo $strp_customer->default_source; echo "<br>"; //save card to the customer in stripe $strp_customer->source->create(array("source" => $_POST['stripeToken'])); echo $strp_customer->default_source;

帶有 source create 的行給我“致命錯誤:在 null 上調用成員函數 create()”

但所有回聲都返回正確的信息。 所以客戶被檢索並且stripeToken在那里。 什么可能導致該錯誤?

這個問題是筆誤。 代碼應該是

$strp_customer->sources->create(array("source" => $_POST['stripeToken']));

它是sources而不是source 您還需要確保您使用的是最新版本的 PHP 庫以支持此調用。

這將向客戶添加一張新卡,但不會將其設置為默認來源。 您也不會再次檢索客戶,因此您將打印與以前相同的值。

帶有卡片詳細信息的另一種解決方案。

try {
        $customer = \Stripe\Customer::retrieve('cus_xxxxxxxxxxx');
        $card = $customer->sources->create(
            array(
                "source" => [
                    "object" => "card",
                    "name" => 'card holder name',
                    "number" => 'card number',
                    "cvc" => 'cvc',
                    "exp_month" => 'exp month',// E.g: 01,02...12
                    "exp_year" => 'exp year' // exp year
                ]
            )
        );
        echo ('Your card has been added successfully!');
    } catch (Exception $exception) {
        echo ('Unable to add new card, please enter valid card details.');
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM