Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.0.0+

PayPal: List merchant invoices across pages

See more PayPal Examples

List invoices for the authenticated merchant. Request totals, iterate every page, and keep the PayPal ID alongside the human-readable number and current status.

PayPal documentation

Chilkat Objective-C Downloads

Objective-C
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoStringBuilder.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];

//  Request total_pages so a merchant with more than one page sees every invoice.
int page = 1;
int lastPage = 1;
while ((page <= lastPage)) {
    CkoStringBuilder *url = [[CkoStringBuilder alloc] init];
    [url Append: @"https://api-m.sandbox.paypal.com/v2/invoicing/invoices?total_required=true&page_size=20&page="];
    [url AppendInt: [NSNumber numberWithInt: page]];
    success = [http HttpNoBody: @"GET" url: [url GetAsString] 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 parsed = [result Load: resp.BodyStr];
    if (parsed != YES) {
        NSLog(@"%@%d",@"Expected a JSON response; HTTP ",[resp.StatusCode intValue]);
        return;
    }

    int i = 0;
    int count = [[result SizeOfArray: @"items"] intValue];
    while ((i < count)) {
        result.I = [NSNumber numberWithInt: i];
        NSLog(@"%@%@%@%@%@",[result StringOf: @"items[i].id"],@"  ",[result StringOf: @"items[i].detail.invoice_number"],@"  ",[result StringOf: @"items[i].status"]);
        i = i + 1;
    }

    lastPage = [[result IntOf: @"total_pages"] intValue];
    page = page + 1;
}