Objective-C Requires Chilkat v11.0.0+
Objective-C
PayPal: Create a reusable payment link for a service
See more PayPal Examples
Offer an annual inspection for USD 90 through PayPal-hosted checkout. Store the returned resource ID with the catalog item and share the payment_link URL with customers.
Chilkat Objective-C Downloads
#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];
// Replace the thank-you URL with your business website. The same link can be shared
// with several customers.
// 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];
CkoJsonObject *body = [[CkoJsonObject alloc] init];
[body UpdateString: @"integration_mode" value: @"LINK"];
[body UpdateString: @"type" value: @"BUY_NOW"];
[body UpdateString: @"reusable" value: @"MULTIPLE"];
[body UpdateString: @"return_url" value: @"https://merchant.example/thank-you"];
[body UpdateString: @"line_items[0].name" value: @"Annual inspection"];
[body UpdateString: @"line_items[0].unit_amount.currency_code" value: @"USD"];
[body UpdateString: @"line_items[0].unit_amount.value" value: @"90.00"];
success = [http HttpJson: @"POST" url: @"https://api-m.sandbox.paypal.com/v1/checkout/payment-resources" 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 parsedResult = [result Load: resp.BodyStr];
if (parsedResult != YES) {
NSLog(@"%@%d",@"Expected a JSON response; HTTP ",[resp.StatusCode intValue]);
return;
}
NSLog(@"%@%@",@"Catalog payment resource ID: ",[result StringOf: @"id"]);
// Link order is not fixed. Find the customer-facing link to copy into an email.
int linkIndex = 0;
while ((linkIndex < [[result SizeOfArray: @"links"] intValue])) {
result.I = [NSNumber numberWithInt: linkIndex];
if ([result StringOfEquals: @"links[i].rel" value: @"payment_link" caseSensitive: YES]) {
NSLog(@"%@%@",@"Share this payment link: ",[result StringOf: @"links[i].href"]);
}
linkIndex = linkIndex + 1;
}