Sample code for 30+ languages & platforms
Perl 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 Perl Downloads

Perl
use strict;
use warnings;
use Chilkat2;

# 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.
my $http = Chilkat2::Http->new;
$http->AuthToken('SANDBOX_ACCESS_TOKEN');
my $resp = Chilkat2::HttpResponse->new;

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

$http->SetUrlVar('id', 'INVOICE_ID');
my $body = Chilkat2::JsonObject->new;
$body->UpdateInt('width', 300);
$body->UpdateInt('height', 300);
$body->UpdateString('action', 'pay');

my $success = $http->HttpJson('POST', 'https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/generate-qr-code', $body, 'application/json', $resp);
if (!$success) {
    print 'No HTTP response: ', $http->LastErrorText, "\n";
    exit;
}

if (($resp->StatusCode < 200) || ($resp->StatusCode >= 300)) {
    my $errorInfo = Chilkat2::JsonObject->new;
    $errorInfo->Load($resp->BodyStr);
    print 'HTTP ', $resp->StatusCode, ' ', $errorInfo->StringOf('name'), ' ', $errorInfo->StringOf('details[0].issue'), "\n";
    print 'PayPal debug ID: ', $errorInfo->StringOf('debug_id'), "\n";
    exit;
}

my $result = Chilkat2::JsonObject->new;
my $parsed = $result->Load($resp->BodyStr);
if (!$parsed) {
    print 'Expected a JSON response; HTTP ', $resp->StatusCode, "\n";
    exit;
}

my $png = Chilkat2::BinData->new;
if (!$result->HasMember('image')) {
    print "QR response did not contain an image.\n";
    exit;
}

my $decoded = $png->AppendEncoded($result->StringOf('image'), 'base64');
if (!$decoded) {
    print "Invalid base64 image.\n";
    exit;
}

my $savedImage = $png->WriteFile('paypal-invoice-qr.png');
if (!$savedImage) {
    print "Could not write the invoice QR image.\n";
    exit;
}

print "Saved paypal-invoice-qr.png.\n";