【问题标题】:Stripe API BalanceStripe API 余额
【发布时间】:2019-09-21 09:04:10
【问题描述】:

我正在尝试使用带 PHP 的 Stripe API 检索我的 Stripe 余额。

我正在拨打以下电话:

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

【问题讨论】:

  • 看起来pending是一个对象数组,你试过$balance-&gt;pending[0]-&gt;amount

标签: php stripe-payments


【解决方案1】:

这里的问题是pending 不是一个对象,而是一个数组,这就是 PHP 抛出该错误的原因。你的代码应该是这样的:

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

【讨论】:

    【解决方案2】:

    更好的方法

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

    【讨论】:

      【解决方案3】:

      我怀疑问题是因为您使用函数作为包装器。确实没有必要定义您自己的函数,其唯一目的是调用另一个函数,在这种情况下这样做可能会导致问题。我要做的第一步是删除函数包装器,直接使用 Stripe 库。

      希望对您有所帮助, 拉里

      PS 我在 Stripe 从事支持工作。

      【讨论】:

        【解决方案4】:

        对于在提出此问题多年后到达这里的任何人,这是另一个使用 PHP reset function 的示例:

        $balance = Stripe_Balance::retrieve();
        $amount =  reset($balance->pending)->amount;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-28
          • 1970-01-01
          • 2021-08-31
          • 2020-11-18
          • 1970-01-01
          • 2017-06-22
          相关资源
          最近更新 更多