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

Objective-C
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoJsonObject.h>

BOOL success = NO;

//  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.
CkoHttp *http = [[CkoHttp alloc] init];
http.AuthToken = @"SANDBOX_ACCESS_TOKEN";
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];

success = [http HttpNoBody: @"GET" url: @"https://api-m.sandbox.paypal.com/v1/reporting/balances" response: resp];
if (success != YES) {
    NSLog(@"%@%@",@"No HTTP response: ",http.LastErrorText);
    return;
}

if (([resp.StatusCode intValue] < 200) || ([resp.StatusCode intValue] >= 300)) {
    CkoJsonObject *errorInfo = [[CkoJsonObject alloc] init];
    [errorInfo Load: resp.BodyStr];
    NSLog(@"%@%d%@%@%@%@",@"HTTP ",[resp.StatusCode intValue],@" ",[errorInfo StringOf: @"name"],@" ",[errorInfo StringOf: @"details[0].issue"]);
    NSLog(@"%@%@",@"PayPal debug ID: ",[errorInfo StringOf: @"debug_id"]);
    return;
}

CkoJsonObject *result = [[CkoJsonObject alloc] init];
BOOL parsedResult = [result Load: resp.BodyStr];
if (parsedResult != YES) {
    NSLog(@"%@%d",@"Expected a JSON response; HTTP ",[resp.StatusCode intValue]);
    return;
}

NSLog(@"%@%@",@"Last refresh: ",[result StringOf: @"last_refresh_time"]);
int i = 0;
while ((i < [[result SizeOfArray: @"balances"] intValue])) {
    result.I = [NSNumber numberWithInt: i];
    NSLog(@"%@%@%@%@%@%@%@",[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;
}