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

PayPal: Pay with a previously saved card

See more PayPal Examples

Buyer interaction. Your application uses a browser for buyer approval or authentication when PayPal requests it. Configure the merchant return and cancel URLs for this flow.

When a returning buyer selects their saved card, look up its vault ID under their authenticated local account. Send the order price and currency from your own cart. Creating the order can capture immediately; inspect each capture before fulfillment. Follow payer-action if further authentication is required.

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

//  Resolve the saved token under this signed-in buyer. A vault ID from a different
//  customer must never be accepted from the browser.

//  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: @"intent" value: @"CAPTURE"];
[body UpdateString: @"purchase_units[0].amount.currency_code" value: @"USD"];
[body UpdateString: @"purchase_units[0].amount.value" value: @"30.00"];
[body UpdateString: @"payment_source.card.vault_id" value: @"SAVED_CARD_TOKEN"];
[body UpdateString: @"payment_source.card.stored_credential.payment_initiator" value: @"CUSTOMER"];
[body UpdateString: @"payment_source.card.stored_credential.payment_type" value: @"ONE_TIME"];
[body UpdateString: @"payment_source.card.stored_credential.usage" value: @"SUBSEQUENT"];
[body UpdateString: @"payment_source.card.experience_context.return_url" value: @"https://merchant.example/paypal/return"];
[body UpdateString: @"payment_source.card.experience_context.cancel_url" value: @"https://merchant.example/paypal/cancel"];

success = [http HttpJson: @"POST" url: @"https://api-m.sandbox.paypal.com/v2/checkout/orders" 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"]);
//  Link order is not fixed. Send the buyer to the matching URL in a browser.
int linkIndex = 0;
while ((linkIndex < [[result SizeOfArray: @"links"] intValue])) {
    result.I = [NSNumber numberWithInt: linkIndex];
    if ([result StringOfEquals: @"links[i].rel" value: @"payer-action" caseSensitive: YES]) {
        NSLog(@"%@%@",@"Approval URL: ",[result StringOf: @"links[i].href"]);
    }

    linkIndex = linkIndex + 1;
}

//  A purchase unit may have no captures yet, or more than one.
int i = 0;
while ((i < [[result SizeOfArray: @"purchase_units"] intValue])) {
    result.I = [NSNumber numberWithInt: i];
    int j = 0;
    while ((j < [[result SizeOfArray: @"purchase_units[i].payments.captures"] intValue])) {
        result.J = [NSNumber numberWithInt: j];
        NSLog(@"%@%@%@%@",@"Unit: ",[result StringOf: @"purchase_units[i].reference_id"],@"  capture: ",[result StringOf: @"purchase_units[i].payments.captures[j].id"]);
        NSLog(@"%@%@%@%@%@%@",@"  ",[result StringOf: @"purchase_units[i].payments.captures[j].status"],@"  ",[result StringOf: @"purchase_units[i].payments.captures[j].amount.value"]
            ,@" ",[result StringOf: @"purchase_units[i].payments.captures[j].amount.currency_code"]);
        j = j + 1;
    }

    i = i + 1;
}