2017-02-01 37 views
1

即时尝试向现有客户添加新卡并获取我不明白其为何发生的错误。这里是代码的样本:添加新卡时PHP Stripe API错误

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

与源行创建给我“致命错误:调用一个成员函数上的空创建()”

但所有回波返回适当的信息。所以客户被检索并且stripeToken在那里。什么可能导致该错误?

+0

var_dump($ strp_customer-> source)'的输出是什么? –

回答

1

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

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

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

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

0

卡片细节的另一个解决方案。

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