【问题标题】:PHP Stripe API error on adding new card添加新卡时出现 PHP Stripe API 错误
【发布时间】:2017-06-18 21:47:40
【问题描述】:

我试图向现有客户添加一张新卡并收到一个我不明白为什么会发生的错误。这是代码示例:

$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 在那里。什么可能导致该错误?

【问题讨论】:

  • var_dump($strp_customer-&gt;source) 的输出是什么?

标签: php api stripe-payments


【解决方案1】:

这个问题是一个错字。代码应该是

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

它是sources 而不是source。您还需要确保您使用的是最新版本的 PHP 库以支持此调用。

这将为客户添加一张新卡,但不会将其设置为默认来源。您也不会再次检索客户,因此您将打印与以前相同的值。

【讨论】:

    【解决方案2】:

    另一个带有卡详细信息的解决方案。

    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.');
        }
    

    【讨论】:

      猜你喜欢
      • 2021-02-08
      • 2016-08-04
      • 2015-05-08
      • 2013-07-22
      • 2020-06-24
      • 1970-01-01
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多