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

PayPal: Create an itemized draft invoice

See more PayPal Examples

Bill a sandbox recipient for two inspections at USD 45 each, due in 30 days. Request the invoice representation and save its ID for sending. Creating the draft does not notify the customer.

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

//  Replace buyer@example.com with your sandbox buyer. PayPal uses the authenticated
//  merchant as the invoicer.
[http SetRequestHeader: @"Prefer" value: @"return=representation"];
CkoJsonObject *body = [[CkoJsonObject alloc] init];
[body UpdateString: @"detail.currency_code" value: @"USD"];
[body UpdateString: @"detail.payment_term.term_type" value: @"NET_30"];
[body UpdateString: @"primary_recipients[0].billing_info.email_address" value: @"buyer@example.com"];
[body UpdateString: @"items[0].name" value: @"Equipment inspection"];
[body UpdateString: @"items[0].quantity" value: @"2"];
[body UpdateString: @"items[0].unit_amount.currency_code" value: @"USD"];
[body UpdateString: @"items[0].unit_amount.value" value: @"45.00"];

success = [http HttpJson: @"POST" url: @"https://api-m.sandbox.paypal.com/v2/invoicing/invoices" 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(@"%@%@",@"Invoice number: ",[result StringOf: @"detail.invoice_number"]);
NSLog(@"%@%@",@"Total: ",[result StringOf: @"amount.value"]);