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

PayPal: Read an invoice and its outstanding balance

See more PayPal Examples

Use the PayPal invoice ID to retrieve its number, status, total and balance due. A partial external payment can reduce the balance without settling the whole invoice.

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];

//  The original total and the balance still due may differ after a payment.

[http SetUrlVar: @"id" value: @"INVOICE_ID"];

success = [http HttpNoBody: @"GET" url: @"https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}" 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;
}

NSLog(@"%@%@",@"ID: ",[result StringOf: @"id"]);
NSLog(@"%@%@",@"Status: ",[result StringOf: @"status"]);
NSLog(@"%@%@",@"Number: ",[result StringOf: @"detail.invoice_number"]);
NSLog(@"%@%@",@"Total: ",[result StringOf: @"amount.value"]);
NSLog(@"%@%@",@"Currency: ",[result StringOf: @"amount.currency_code"]);
NSLog(@"%@%@",@"Balance due: ",[result StringOf: @"due_amount.value"]);