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

Delphi DLL
var
success: Boolean;
http: HCkHttp;
resp: HCkHttpResponse;
body: HCkJsonObject;
errorInfo: HCkJsonObject;
result: HCkJsonObject;
parsed: Boolean;
png: HCkBinData;
decoded: Boolean;
savedImage: Boolean;

begin
success := False;

//  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 := CkHttp_Create();
CkHttp_putAuthToken(http,'SANDBOX_ACCESS_TOKEN');
resp := CkHttpResponse_Create();

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

CkHttp_SetUrlVar(http,'id','INVOICE_ID');
body := CkJsonObject_Create();
CkJsonObject_UpdateInt(body,'width',300);
CkJsonObject_UpdateInt(body,'height',300);
CkJsonObject_UpdateString(body,'action','pay');

success := CkHttp_HttpJson(http,'POST','https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/generate-qr-code',body,'application/json',resp);
if (success <> True) then
  begin
    Memo1.Lines.Add('No HTTP response: ' + CkHttp__lastErrorText(http));
    Exit;
  end;
if ((CkHttpResponse_getStatusCode(resp) < 200) or (CkHttpResponse_getStatusCode(resp) >= 300)) then
  begin
    errorInfo := CkJsonObject_Create();
    CkJsonObject_Load(errorInfo,CkHttpResponse__bodyStr(resp));
    Memo1.Lines.Add('HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ' ' + CkJsonObject__stringOf(errorInfo,'name') + ' ' + CkJsonObject__stringOf(errorInfo,'details[0].issue'));
    Memo1.Lines.Add('PayPal debug ID: ' + CkJsonObject__stringOf(errorInfo,'debug_id'));
    Exit;
  end;

result := CkJsonObject_Create();
parsed := CkJsonObject_Load(result,CkHttpResponse__bodyStr(resp));
if (parsed <> True) then
  begin
    Memo1.Lines.Add('Expected a JSON response; HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)));
    Exit;
  end;
png := CkBinData_Create();
if (CkJsonObject_HasMember(result,'image') <> True) then
  begin
    Memo1.Lines.Add('QR response did not contain an image.');
    Exit;
  end;
decoded := CkBinData_AppendEncoded(png,CkJsonObject__stringOf(result,'image'),'base64');
if (decoded <> True) then
  begin
    Memo1.Lines.Add('Invalid base64 image.');
    Exit;
  end;
savedImage := CkBinData_WriteFile(png,'paypal-invoice-qr.png');
if (savedImage <> True) then
  begin
    Memo1.Lines.Add('Could not write the invoice QR image.');
    Exit;
  end;
Memo1.Lines.Add('Saved paypal-invoice-qr.png.');

CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(body);
    CkJsonObject_Dispose(errorInfo);
CkJsonObject_Dispose(result);
CkBinData_Dispose(png);