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

PayPal: Build a worklist of overdue invoices

See more PayPal Examples

Find open invoices due in the past 90 days, including partial payments. Walk all result pages and display the invoice number, due date, and remaining balance for the collections team.

PayPal documentation

Chilkat Objective-C Downloads

Objective-C
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoJsonObject.h>
#import <CkoDateTime.h>
#import <CkoStringBuilder.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];
CkoJsonObject *body = [[CkoJsonObject alloc] init];
[body UpdateString: @"status[0]" value: @"SENT"];
[body UpdateString: @"status[1]" value: @"PARTIALLY_PAID"];
[body UpdateString: @"status[2]" value: @"UNPAID"];
[body UpdateString: @"status[3]" value: @"SHARED"];
//  Yesterday is the last overdue date. Include invoices due in the past 90 days.
CkoDateTime *day = [[CkoDateTime alloc] init];
[day SetFromCurrentSystemTime];
[day AddDays: [NSNumber numberWithInt: -1]];
[body UpdateString: @"due_date_range.end" value: [day GetAsIso8601: @"YYYY-MM-DD" bLocal: NO]];
[day AddDays: [NSNumber numberWithInt: -89]];
[body UpdateString: @"due_date_range.start" value: [day GetAsIso8601: @"YYYY-MM-DD" bLocal: NO]];
int page = 1;
int lastPage = 1;
while ((page <= lastPage)) {
    CkoStringBuilder *url = [[CkoStringBuilder alloc] init];
    [url Append: @"https://api-m.sandbox.paypal.com/v2/invoicing/search-invoices?total_required=true&page_size=50&page="];
    [url AppendInt: [NSNumber numberWithInt: page]];
    success = [http HttpJson: @"POST" url: [url GetAsString] json: body contentType: @"application/json" 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;
    }

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

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