Sample code for 30+ languages & platforms
PowerShell Requires Chilkat v11.0.0+

PayPal: Show available, withheld and total balances by currency

See more PayPal Examples

Read the merchant balance snapshot using reporting access. Keep each currency separate and show the last refresh time so accounting knows when PayPal last updated the figures.

PayPal documentation

Chilkat PowerShell Downloads

PowerShell
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"

$success = $false

#  See Unlock Chilkat before making requests.
#  For the merchant's internal backoffice application. Load the sandbox token
#  from the merchant's credential store. Keep it out of publicly distributed apps.
#  See Get a PayPal access token.
$http = New-Object Chilkat.Http
$http.AuthToken = "SANDBOX_ACCESS_TOKEN"
$resp = New-Object Chilkat.HttpResponse

$success = $http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v1/reporting/balances",$resp)
if ($success -ne $true) {
    $("No HTTP response: " + $http.LastErrorText)
    exit
}

if (($resp.StatusCode -lt 200) -or ($resp.StatusCode -ge 300)) {
    $errorInfo = New-Object Chilkat.JsonObject
    $errorInfo.Load($resp.BodyStr)
    $("HTTP " + $resp.StatusCode + " " + $errorInfo.StringOf("name") + " " + $errorInfo.StringOf("details[0].issue"))
    $("PayPal debug ID: " + $errorInfo.StringOf("debug_id"))
    exit
}

$result = New-Object Chilkat.JsonObject
$parsedResult = $result.Load($resp.BodyStr)
if ($parsedResult -ne $true) {
    $("Expected a JSON response; HTTP " + $resp.StatusCode)
    exit
}

$("Last refresh: " + $result.StringOf("last_refresh_time"))
$i = 0
while (($i -lt $result.SizeOfArray("balances"))) {
    $result.I = $i
    $($result.StringOf("balances[i].currency") + "  total " + $result.StringOf("balances[i].total_balance.value") + "  available " + $result.StringOf("balances[i].available_balance.value")
         + "  withheld " + $result.StringOf("balances[i].withheld_balance.value"))
    $i = $i + 1
}