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

PayPal: Look up a subscription and its recent payments

See more PayPal Examples

Start with the PayPal subscription ID saved with the local customer. Display its status and next billing time, then list transactions for the previous 30 days with each gross amount and currency.

PayPal documentation

Chilkat Objective-C Downloads

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

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

success = [http HttpNoBody: @"GET" url: @"https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$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 parsedResult = [result Load: resp.BodyStr];
if (parsedResult != YES) {
    NSLog(@"%@%d",@"Expected a JSON response; HTTP ",[resp.StatusCode intValue]);
    return;
}

NSLog(@"%@%@",@"Status: ",[result StringOf: @"status"]);
NSLog(@"%@%@",@"Next billing: ",[result StringOf: @"billing_info.next_billing_time"]);
CkoDateTime *now = [[CkoDateTime alloc] init];
[now SetFromCurrentSystemTime];
CkoJsonObject *query = [[CkoJsonObject alloc] init];
[query UpdateString: @"end_time" value: [now GetAsTimestamp: NO]];
[now AddDays: [NSNumber numberWithInt: -30]];
[query UpdateString: @"start_time" value: [now GetAsTimestamp: NO]];
success = [http HttpParams: @"GET" url: @"https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}/transactions" json: query response: resp];
if (success != YES) {
    NSLog(@"%@%@",@"No HTTP response: ",http.LastErrorText);
    return;
}

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

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

int i = 0;
while ((i < [[history SizeOfArray: @"transactions"] intValue])) {
    history.I = [NSNumber numberWithInt: i];
    NSLog(@"%@%@%@%@%@%@%@%@%@",[history StringOf: @"transactions[i].time"],@"  ",[history StringOf: @"transactions[i].id"],@"  ",[history StringOf: @"transactions[i].status"]
        ,@"  ",[history StringOf: @"transactions[i].amount_with_breakdown.gross_amount.value"],@" ",[history StringOf: @"transactions[i].amount_with_breakdown.gross_amount.currency_code"]);
    i = i + 1;
}