我正在构建与 WordPress 的 Stripe 连接集成,并尝试检索已连接帐户的余额。到目前为止,这是我的代码:// Show User Balancefunction show_user_balance($uid){require 'vendor/autoload.php';$user_id = $uid;$connected_account_id = get_user_meta( $user_id, 'stripe_connect_id', true );// Set your secret key. Remember to switch to your live secret key in production!\Stripe\Stripe::setApiKey('sk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');$balance = \Stripe\Balance::retrieve( ['stripe_account' => $connected_account_id] );echo "Current Balance: ".$balance;}Echoing$balance返回以下 JSON 格式的字符串:Current Balance: Stripe\Balance JSON: { "object": "balance", "available": [ { "amount": 0, "currency": "usd", "source_types": { "card": 0 } } ], "instant_available": [ { "amount": 0, "currency": "usd", "source_types": { "card": 0 } } ], "livemode": false, "pending": [ { "amount": 0, "currency": "usd", "source_types": { "card": 0 } } ] }我想回显可用余额的金额。我已经尝试使用json_decode设置为 true 的函数。它返回一个对象数组,我已经尝试了这两种方法,print_r但var_dump没有成功的结果。有什么建议么?
1 回答
蝴蝶不菲
TA贡献1810条经验 获得超4个赞
Balance 对象具有“available”和“pending”属性,每个属性都是不同货币的余额数组。以美元打印可用余额的一种简单方法是:
foreach ($balance->available as $bal) {
if ($bal->currency == "usd")
echo "{$bal->amount} {$bal->currency} available\n";
}
foreach ($balance->pending as $bal) {
if ($bal->currency == "usd")
echo "{$bal->amount} {$bal->currency} pending\n";
}
当然,您希望根据您的语言环境正确设置货币格式。而且,如果您希望以多种货币付款,您还必须打印这些货币。祝你的项目好运!
- 1 回答
- 0 关注
- 72 浏览
添加回答
举报
0/150
提交
取消