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

PayPal: Update the price on a reusable payment link

See more PayPal Examples

Retrieve the simple USD inspection link from the create example. Check its item and currency, retain its configuration, and replace the price with USD 95. PUT sends the complete editable definition.

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

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

success = [http HttpNoBody: @"GET" url: @"https://api-m.sandbox.paypal.com/v1/checkout/payment-resources/{$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;
}

if (([[result SizeOfArray: @"line_items"] intValue] != 1) || ([result StringOfEquals: @"line_items[0].name" value: @"Annual inspection" caseSensitive: YES] != YES) || ([result StringOfEquals: @"line_items[0].unit_amount.currency_code" value: @"USD" caseSensitive: YES] != YES)) {
    NSLog(@"%@",@"Select the USD inspection link from your catalog.");
    return;
}

//  Preserve line items, taxes, shipping and checkout settings from the retrieved link.
//  Remove the resource metadata PayPal added to the GET response.
if ([result HasMember: @"id"] == YES) {
    [result Delete: @"id"];
}

if ([result HasMember: @"create_time"] == YES) {
    [result Delete: @"create_time"];
}

if ([result HasMember: @"update_time"] == YES) {
    [result Delete: @"update_time"];
}

if ([result HasMember: @"status"] == YES) {
    [result Delete: @"status"];
}

if ([result HasMember: @"links"] == YES) {
    [result Delete: @"links"];
}

if ([result HasMember: @"payment_link"] == YES) {
    [result Delete: @"payment_link"];
}

[result UpdateString: @"line_items[0].unit_amount.value" value: @"95.00"];
success = [http HttpJson: @"PUT" url: @"https://api-m.sandbox.paypal.com/v1/checkout/payment-resources/{$id}" json: result 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 *updateError = [[CkoJsonObject alloc] init];
    [updateError Load: resp.BodyStr];
    NSLog(@"%@%d%@%@%@%@",@"HTTP ",[resp.StatusCode intValue],@" ",[updateError StringOf: @"name"],@" ",[updateError StringOf: @"details[0].issue"]);
    NSLog(@"%@%@",@"PayPal debug ID: ",[updateError StringOf: @"debug_id"]);
    return;
}

NSLog(@"%@",@"The inspection price is now USD 95.00.");