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

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.BinData,
  Chilkat.HttpResponse,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  resp: THttpResponse;
  body: TJsonObject;
  errorInfo: TJsonObject;
  result: TJsonObject;
  parsed: Boolean;
  png: TBinData;
  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 := THttp.Create;
  http.AuthToken := 'SANDBOX_ACCESS_TOKEN';
  resp := THttpResponse.Create;

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

  http.SetUrlVar('id','INVOICE_ID');
  body := TJsonObject.Create;
  body.UpdateInt('width',300);
  body.UpdateInt('height',300);
  body.UpdateString('action','pay');

  success := http.HttpJson('POST','https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/generate-qr-code',body,'application/json',resp);
  if (success <> True) then
    begin
      WriteLn('No HTTP response: ' + http.LastErrorText);
      Exit;
    end;
  if ((resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
    begin
      errorInfo := TJsonObject.Create;
      errorInfo.Load(resp.BodyStr);
      WriteLn('HTTP ' + resp.StatusCode + ' ' + errorInfo.StringOf('name') + ' ' + errorInfo.StringOf('details[0].issue'));
      WriteLn('PayPal debug ID: ' + errorInfo.StringOf('debug_id'));
      Exit;
    end;

  result := TJsonObject.Create;
  parsed := result.Load(resp.BodyStr);
  if (parsed <> True) then
    begin
      WriteLn('Expected a JSON response; HTTP ' + resp.StatusCode);
      Exit;
    end;
  png := TBinData.Create;
  if (result.HasMember('image') <> True) then
    begin
      WriteLn('QR response did not contain an image.');
      Exit;
    end;
  decoded := png.AppendEncoded(result.StringOf('image'),'base64');
  if (decoded <> True) then
    begin
      WriteLn('Invalid base64 image.');
      Exit;
    end;
  savedImage := png.WriteFile('paypal-invoice-qr.png');
  if (savedImage <> True) then
    begin
      WriteLn('Could not write the invoice QR image.');
      Exit;
    end;
  WriteLn('Saved paypal-invoice-qr.png.');


  http.Free;
  resp.Free;
  body.Free;
      errorInfo.Free;
  result.Free;
  png.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.