PHP Requires Chilkat v11.0.0+
PHP
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.
Chilkat PHP Downloads
<?php
use Chilkat2\BinData;
use Chilkat2\Http;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;
// 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.
$http = new Http();
$http->AuthToken = 'SANDBOX_ACCESS_TOKEN';
$resp = new HttpResponse();
// Use a sent, payable invoice. Width and height are JSON numbers, in pixels.
$http->SetUrlVar('id', 'INVOICE_ID');
$body = new JsonObject();
$body->UpdateInt('width', 300);
$body->UpdateInt('height', 300);
$body->UpdateString('action', 'pay');
try {
$http->HttpJson('POST', 'https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/generate-qr-code', $body, 'application/json', $resp);
} catch (\Chilkat2\Exception $e) {
echo 'No HTTP response: ', $e->getMessage(), "\n";
exit;
}
if (($resp->StatusCode < 200) || ($resp->StatusCode >= 300)) {
$errorInfo = new JsonObject();
$errorInfo->Load($resp->BodyStr);
echo 'HTTP ', $resp->StatusCode, ' ', $errorInfo->StringOf('name'), ' ', $errorInfo->StringOf('details[0].issue'), "\n";
echo 'PayPal debug ID: ', $errorInfo->StringOf('debug_id'), "\n";
exit;
}
$result = new JsonObject();
try {
$result->Load($resp->BodyStr);
} catch (\Chilkat2\Exception $e) {
echo 'Expected a JSON response; HTTP ', $resp->StatusCode, "\n";
exit;
}
$png = new BinData();
if (!$result->HasMember('image')) {
echo "QR response did not contain an image.\n";
exit;
}
try {
$png->AppendEncoded($result->StringOf('image'), 'base64');
} catch (\Chilkat2\Exception $e) {
echo "Invalid base64 image.\n";
exit;
}
try {
$png->WriteFile('paypal-invoice-qr.png');
} catch (\Chilkat2\Exception $e) {
echo "Could not write the invoice QR image.\n";
exit;
}
echo "Saved paypal-invoice-qr.png.\n";