user3968645
user3968645

Reputation: 135

Stripe API Balance

I am trying to retrieve my Stripe balance using the Stripe API with PHP.

I am calling the following:

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

   return $response;
}

This returns something like this:

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...

I've tried doing the following but it only resulted in an error. I'm trying to get the amount of pending and available.

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

The error I get is: Trying to get property of non-object

Upvotes: 1

Views: 1569

Answers (4)

LobsterBaz
LobsterBaz

Reputation: 2003

For anyone arriving here years after this question was asked, here's another example using PHP reset function:

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

Upvotes: 0

MotsManish
MotsManish

Reputation: 485

A better way

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

Upvotes: 2

koopajah
koopajah

Reputation: 25622

The problem here is that pending is not an object but an array which is why PHP is throwing that error. Your code should be something like this:

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

Upvotes: 3

Larry Ullman
Larry Ullman

Reputation: 2281

I suspect the problem is because you're using a function as a wrapper to this. There's really no need to define your own function whose sole purpose is to call another function, and doing so in this case may be causing the issue. The first step I'd take is to remove the function wrapper and just use the Stripe library directly.

Hope that helps, Larry

PS I work on Support at Stripe.

Upvotes: 0

Related Questions