2014-10-31 123 views
1

我正在嘗試使用帶有PHP的Stripe API檢索我的條紋餘額。條紋API餘額

我打電話以下幾點:

function retrieve_balance() 
{ 
    $response = Stripe_Balance::retrieve(); 

    return $response; 
} 

這將返回是這樣的:

object(Stripe_Balance)#28 (5) { 
    ["_apiKey":protected]=> 
    string(32) "my_key_here" 
    ["_values":protected]=> 
    array(4) { 
    ["pending"]=> 
     array(1) { 
     [0]=> 
      object(Stripe_Object)#32 (5) { 
      ["_apiKey":protected]=> 
      string(32) "my_key_here" 
       ["_values":protected]=> 
       array(2) { 
       ["amount"]=> 
        int(0) 
       ["currency"]=> 
        string(3) "usd" 

       etc... 

我試着做以下,但它只能導致錯誤。我正在嘗試獲取待處理和可用的數量。

<?php 
    var_dump($balance); 
    /*Issue Line Below*/ 
    echo $balance->pending->amount; 
?> 

我得到的錯誤是:Trying to get property of non-object

+0

它看起來像掛起的是對象的數組,你有沒有試過'$ BALANCE->待定[0] - > amount' – Cory 2014-11-01 00:15:23

回答

2

這裏的問題是,pending不是一個對象,而是一個數組,這就是爲什麼PHP會拋出這個錯誤。你的代碼應該是這樣的:

$response = Stripe_Balance::retrieve(); 
foreach($response->pending as $pendingBalance) 
{ 
    echo "Pending balance of " . $pendingBalance->amount . " in " . $pendingBalance->currency . "<br>"; 
} 
0

我懷疑問題是因爲您使用的是功能的包裝了這一點。實際上不需要定義你自己的函數,其唯一目的是調用另一個函數,在這種情況下這樣做可能會導致問題。我會採取的第一步是刪除函數包裝,並直接使用條紋庫。

希望幫助, 拉里

PS我在條紋上的支持工作。

2

一種更好的方式

$balance = Stripe_Balance::retrieve(); 
$balanceArr = $balance->__toArray(true); 
$amount = $balanceArr['pending']['0']['amount'];