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

PayPal: Record a bank transfer against an invoice

See more PayPal Examples

Record USD 45 already received by bank transfer. The invoice may still have a balance. This makes a bookkeeping entry; it does not debit the customer. Save payment_id for reconciliation.

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

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

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  resp: THttpResponse;
  body: TJsonObject;
  date: TCkDateTime;
  errorInfo: TJsonObject;
  result: TJsonObject;
  parsed: 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;

  //  Record only a bank transfer you have already received. Reconcile first if a previous
  //  attempt timed out.

  http.SetUrlVar('id','INVOICE_ID');
  body := TJsonObject.Create;
  body.UpdateString('method','BANK_TRANSFER');
  body.UpdateString('amount.currency_code','USD');
  body.UpdateString('amount.value','45.00');

  //  Use the actual date of the transfer; this example records one made today.
  date := TCkDateTime.Create;
  date.SetFromCurrentSystemTime();
  body.UpdateString('payment_date',date.GetAsIso8601('YYYY-MM-DD',False));

  success := http.HttpJson('POST','https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/payments',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;
  WriteLn('Record ID: ' + result.StringOf('payment_id'));


  http.Free;
  resp.Free;
  body.Free;
  date.Free;
      errorInfo.Free;
  result.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.