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

PayPal: Partially capture an authorization

See more PayPal Examples

Capture USD 10 of the sample USD 30 authorization and leave the rest open. Multiple captures require merchant eligibility. Use a separate request ID for each intended capture.

PayPal documentation

Chilkat Objective-C Downloads

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

//  Keep final_capture false if another shipment will be billed against this hold.

[http SetUrlVar: @"id" value: @"AUTHORIZATION_ID"];
//  Create and persist this key once per business operation. On a timeout,
//  retry the same request with its saved key, not a newly generated one.
CkoCrypt2 *crypt = [[CkoCrypt2 alloc] init];
NSString *requestId = [crypt GenerateUuid];
[http SetRequestHeader: @"PayPal-Request-Id" value: requestId];
[http SetRequestHeader: @"Prefer" value: @"return=representation"];
CkoJsonObject *body = [[CkoJsonObject alloc] init];
[body UpdateString: @"amount.currency_code" value: @"USD"];
[body UpdateString: @"amount.value" value: @"10.00"];
[body UpdateBool: @"final_capture" value: NO];

success = [http HttpJson: @"POST" url: @"https://api-m.sandbox.paypal.com/v2/payments/authorizations/{$id}/capture" 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 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(@"%@%@",@"Amount: ",[result StringOf: @"amount.value"]);
NSLog(@"%@%@",@"Currency: ",[result StringOf: @"amount.currency_code"]);