Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

PayPal: Get a payable invoice link for your own email

See more PayPal Examples

Use a draft dated today or earlier. Make it payable with customer email suppressed, then retrieve the recipient view URL for the merchant's email. Future-dated invoices are scheduled for delivery and should use a different workflow.

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.StringBuilder,
  Chilkat.HttpResponse,
  Chilkat.JsonObject,
  Chilkat.CkDateTime;

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

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  resp: THttpResponse;
  errorInfo: TJsonObject;
  result: TJsonObject;
  parsedResult: Boolean;
  now: TCkDateTime;
  stamp: TStringBuilder;
  issue: TCkDateTime;
  validDate: Boolean;
  body: TJsonObject;
  sendError: TJsonObject;
  invoiceError: TJsonObject;
  invoice: TJsonObject;
  parsedInvoice: 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;

  http.SetUrlVar('id','INVOICE_ID');

  success := http.HttpNoBody('GET','https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}',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;
  parsedResult := result.Load(resp.BodyStr);
  if (parsedResult <> True) then
    begin
      WriteLn('Expected a JSON response; HTTP ' + resp.StatusCode);
      Exit;
    end;
  if (result.StringOfEquals('status','DRAFT',True) <> True) then
    begin
      WriteLn('Select an unsent draft.');
      Exit;
    end;
  //  Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
  now := TCkDateTime.Create;
  now.SetFromCurrentSystemTime();
  stamp := TStringBuilder.Create;
  stamp.Append(result.StringOf('detail.invoice_date'));
  stamp.Append('T00:00:00Z');
  issue := TCkDateTime.Create;
  validDate := issue.SetFromTimestamp(stamp.GetAsString());
  if ((validDate <> True) or (now.DiffSeconds(issue) < 0)) then
    begin
      WriteLn('Select a draft with an issue date of today or earlier.');
      Exit;
    end;
  body := TJsonObject.Create;
  body.UpdateBool('send_to_recipient',False);
  body.UpdateBool('send_to_invoicer',False);
  success := http.HttpJson('POST','https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send',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
      sendError := TJsonObject.Create;
      sendError.Load(resp.BodyStr);
      WriteLn('HTTP ' + resp.StatusCode + ' ' + sendError.StringOf('name') + ' ' + sendError.StringOf('details[0].issue'));
      WriteLn('PayPal debug ID: ' + sendError.StringOf('debug_id'));
      Exit;
    end;
  //  Read back the invoice: acceptance of the send alone does not guarantee it is payable.
  success := http.HttpNoBody('GET','https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}',resp);
  if (success <> True) then
    begin
      WriteLn('No HTTP response: ' + http.LastErrorText);
      Exit;
    end;
  if ((resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
    begin
      invoiceError := TJsonObject.Create;
      invoiceError.Load(resp.BodyStr);
      WriteLn('HTTP ' + resp.StatusCode + ' ' + invoiceError.StringOf('name') + ' ' + invoiceError.StringOf('details[0].issue'));
      WriteLn('PayPal debug ID: ' + invoiceError.StringOf('debug_id'));
      Exit;
    end;
  invoice := TJsonObject.Create;
  parsedInvoice := invoice.Load(resp.BodyStr);
  if (parsedInvoice <> True) then
    begin
      WriteLn('Expected a JSON response; HTTP ' + resp.StatusCode);
      Exit;
    end;
  if (((invoice.StringOfEquals('status','SENT',True) = True) or (invoice.StringOfEquals('status','UNPAID',True) = True) or (invoice.StringOfEquals('status','SHARED',True) = True)) and (invoice.HasMember('detail.metadata.recipient_view_url') = True)) then
    begin
      WriteLn('Customer payment page: ' + invoice.StringOf('detail.metadata.recipient_view_url'));
      Exit;
    end;
  WriteLn('Invoice is ' + invoice.StringOf('status') + '. Retrieve it again when payable.');


  http.Free;
  resp.Free;
      errorInfo.Free;
  result.Free;
  now.Free;
  stamp.Free;
  issue.Free;
  body.Free;
      sendError.Free;
      invoiceError.Free;
  invoice.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.