0

To get the balance object from stripe I know to use the following call:

$balance = $this->stripe->balance->retrieve(null, [
    'stripe_account' => $facility->stripe_account_id
]);

However this provides an array of arrays, the 2 main keys pending and available both have arrays beneath but from what I can tell there is only 1 item in that nested array.

I also believe that to get a total balance you need to add the contents of pending and available. However, would there ever be a reason for a second item within either of those top level items?

Would this implementation work, or do I need to loop both available and pending to get an accurate total balance:

$totalBalance = $balance->available[0]->amount + $balance->pending[0]->amount;

Actual php response

#_originalValues: array:4 [
    "object" => "balance"
    "available" => array:1 [
      0 => array:3 [
        "amount" => 0
        "currency" => "gbp"
        "source_types" => array:1 [
          "card" => 0
        ]
      ]
    ]
    "livemode" => false
    "pending" => array:1 [
      0 => array:3 [
        "amount" => 0
        "currency" => "gbp"
        "source_types" => array:1 [
          "card" => 0
        ]
      ]
    ]
  ]
Lewis Smith
  • 1,271
  • 1
  • 14
  • 39
  • [Have you tried this?](https://stackoverflow.com/questions/30726537/how-can-i-sum-objects-property-of-an-array-using-php). Also viewing the [Stripe documentation](https://stripe.com/docs/api/balance/balance_retrieve) I can't see why there would be more than one value in the array? Unless you're combining multiple currencies or something. So if you're on a single currency I suspect in practise all you need is the line of code you show in your question. – Martin Jun 24 '22 at 17:43
  • I'll add context for the actual response – Lewis Smith Jun 24 '22 at 17:44
  • And `pending` by definition is **NOT** available balance so to sum it up as such can cause you issues down the line if for whatever reason the pending funds are not delivered (ie they're declined / refused etc.). – Martin Jun 24 '22 at 17:44
  • Okay that's a different thought I hadn't had, probably worth ignoring the pending entirely then? – Lewis Smith Jun 24 '22 at 17:46
  • I would think if you are only working with one currency and you are sure you want to combine `pending` and `available` then there is no problem with your code in your question, just adding these two values together. If you work with multiple currencies then you'd best find the answer on the linked question [How can I sum objects property of an array using PHP](https://stackoverflow.com/questions/30726537/how-can-i-sum-objects-property-of-an-array-using-php). Let me know if there's something I've missed here and update your question, else I'd think this is a dupe of the linked question. thx – Martin Jun 24 '22 at 17:49
  • Also Stripe may well have different array values for different `source_types`, if using multiple payment methods on the same account, this would cause an array of more than 1 element for pending definitely; ie pending bitcoin and pending card payments, I wouldn't know if this also applies to available. – Martin Jun 24 '22 at 17:52
  • I think you're spot on about the reason being due to currency which we're only using gbp for now so we can probably ignore the idea there could be multiple arrays. And I'm not interested in source as it's only card payment enabled in our system` – Lewis Smith Jun 24 '22 at 17:56
  • Then I think that your maths for `$totalBalance` is absolutely fine as it is. If you're unsure I'd recommend enquiring the reasoning for the structure with an email to Stripe themselves to confirm. – Martin Jun 24 '22 at 18:00
  • 1
    Hi! I just wanted to share what my test account's available balance looks like in case that helps clarify the issue of multiple source types: `[ { "amount": 15803333, "currency": "usd", "source_types": { "bank_account": 5455, "card": 15797878 } } ],` (Apologies for formatting). You can see that the `amount` is still accessible in the same place but we have totals broken out by source type. – RyanM Jun 24 '22 at 18:14

0 Answers0