Objective-C Requires Chilkat v11.0.0+
Objective-C
PayPal: Get a payable invoice link for your own email
See more PayPal Examples
Use a draft dated today or earlier. Make it payable with customer email suppressed, then retrieve the recipient view URL for the merchant's email. Future-dated invoices are scheduled for delivery and should use a different workflow.
Chilkat Objective-C Downloads
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoJsonObject.h>
#import <CkoDateTime.h>
#import <CkoStringBuilder.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: @"INVOICE_ID"];
success = [http HttpNoBody: @"GET" url: @"https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$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 StringOfEquals: @"status" value: @"DRAFT" caseSensitive: YES] != YES) {
NSLog(@"%@",@"Select an unsent draft.");
return;
}
// Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
CkoDateTime *now = [[CkoDateTime alloc] init];
[now SetFromCurrentSystemTime];
CkoStringBuilder *stamp = [[CkoStringBuilder alloc] init];
[stamp Append: [result StringOf: @"detail.invoice_date"]];
[stamp Append: @"T00:00:00Z"];
CkoDateTime *issue = [[CkoDateTime alloc] init];
BOOL validDate = [issue SetFromTimestamp: [stamp GetAsString]];
if ((validDate != YES) || ([[now DiffSeconds: issue] intValue] < 0)) {
NSLog(@"%@",@"Select a draft with an issue date of today or earlier.");
return;
}
CkoJsonObject *body = [[CkoJsonObject alloc] init];
[body UpdateBool: @"send_to_recipient" value: NO];
[body UpdateBool: @"send_to_invoicer" value: NO];
success = [http HttpJson: @"POST" url: @"https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send" 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 *sendError = [[CkoJsonObject alloc] init];
[sendError Load: resp.BodyStr];
NSLog(@"%@%d%@%@%@%@",@"HTTP ",[resp.StatusCode intValue],@" ",[sendError StringOf: @"name"],@" ",[sendError StringOf: @"details[0].issue"]);
NSLog(@"%@%@",@"PayPal debug ID: ",[sendError StringOf: @"debug_id"]);
return;
}
// Read back the invoice: acceptance of the send alone does not guarantee it is payable.
success = [http HttpNoBody: @"GET" url: @"https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}" response: resp];
if (success != YES) {
NSLog(@"%@%@",@"No HTTP response: ",http.LastErrorText);
return;
}
if (([resp.StatusCode intValue] < 200) || ([resp.StatusCode intValue] >= 300)) {
CkoJsonObject *invoiceError = [[CkoJsonObject alloc] init];
[invoiceError Load: resp.BodyStr];
NSLog(@"%@%d%@%@%@%@",@"HTTP ",[resp.StatusCode intValue],@" ",[invoiceError StringOf: @"name"],@" ",[invoiceError StringOf: @"details[0].issue"]);
NSLog(@"%@%@",@"PayPal debug ID: ",[invoiceError StringOf: @"debug_id"]);
return;
}
CkoJsonObject *invoice = [[CkoJsonObject alloc] init];
BOOL parsedInvoice = [invoice Load: resp.BodyStr];
if (parsedInvoice != YES) {
NSLog(@"%@%d",@"Expected a JSON response; HTTP ",[resp.StatusCode intValue]);
return;
}
if ((([invoice StringOfEquals: @"status" value: @"SENT" caseSensitive: YES] == YES) || ([invoice StringOfEquals: @"status" value: @"UNPAID" caseSensitive: YES] == YES) || ([invoice StringOfEquals: @"status" value: @"SHARED" caseSensitive: YES] == YES)) && ([invoice HasMember: @"detail.metadata.recipient_view_url"] == YES)) {
NSLog(@"%@%@",@"Customer payment page: ",[invoice StringOf: @"detail.metadata.recipient_view_url"]);
return;
}
NSLog(@"%@%@%@",@"Invoice is ",[invoice StringOf: @"status"],@". Retrieve it again when payable.");