Sample code for 30+ languages & platforms
JavaScript 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

Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var 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.
var http = new CkHttp();
http.AuthToken = "SANDBOX_ACCESS_TOKEN";
var resp = new CkHttpResponse();

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

if ((resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
    var errorInfo = new CkJsonObject();
    errorInfo.Load(resp.BodyStr);
    console.log("HTTP " + resp.StatusCode + " " + errorInfo.StringOf("name") + " " + errorInfo.StringOf("details[0].issue"));
    console.log("PayPal debug ID: " + errorInfo.StringOf("debug_id"));
    return;
}

var result = new CkJsonObject();
var parsedResult = result.Load(resp.BodyStr);
if (parsedResult !== true) {
    console.log("Expected a JSON response; HTTP " + resp.StatusCode);
    return;
}

console.log("Last refresh: " + result.StringOf("last_refresh_time"));
var i = 0;
while ((i < result.SizeOfArray("balances"))) {
    result.I = i;
    console.log(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;
}