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

PayPal: Save a payable invoice QR code as a PNG

See more PayPal Examples

Generate a 300-pixel payment QR code for a sent, payable invoice. Decode the base64 image into a PNG for a printed invoice; the code opens the payment page, and is not proof of payment.

PayPal documentation

Chilkat Objective-C Downloads

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

//  Use a sent, payable invoice. Width and height are JSON numbers, in pixels.

[http SetUrlVar: @"id" value: @"INVOICE_ID"];
CkoJsonObject *body = [[CkoJsonObject alloc] init];
[body UpdateInt: @"width" value: [NSNumber numberWithInt: 300]];
[body UpdateInt: @"height" value: [NSNumber numberWithInt: 300]];
[body UpdateString: @"action" value: @"pay"];

success = [http HttpJson: @"POST" url: @"https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/generate-qr-code" 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;
}

CkoBinData *png = [[CkoBinData alloc] init];
if ([result HasMember: @"image"] != YES) {
    NSLog(@"%@",@"QR response did not contain an image.");
    return;
}

BOOL decoded = [png AppendEncoded: [result StringOf: @"image"] encoding: @"base64"];
if (decoded != YES) {
    NSLog(@"%@",@"Invalid base64 image.");
    return;
}

BOOL savedImage = [png WriteFile: @"paypal-invoice-qr.png"];
if (savedImage != YES) {
    NSLog(@"%@",@"Could not write the invoice QR image.");
    return;
}

NSLog(@"%@",@"Saved paypal-invoice-qr.png.");