Sample code for 30+ languages & platforms
Delphi ActiveX Requires Chilkat v11.0.0+

PayPal: Build a worklist of overdue invoices

See more PayPal Examples

Find open invoices due in the past 90 days, including partial payments. Walk all result pages and display the invoice number, due date, and remaining balance for the collections team.

PayPal documentation

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
http: TChilkatHttp;
resp: TChilkatHttpResponse;
body: TChilkatJsonObject;
day: TCkDateTime;
page: Integer;
lastPage: Integer;
url: TChilkatStringBuilder;
errorInfo: TChilkatJsonObject;
result: TChilkatJsonObject;
parsedResult: Integer;
i: Integer;
count: Integer;

begin
success := 0;

//  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 := TChilkatHttp.Create(Self);
http.AuthToken := 'SANDBOX_ACCESS_TOKEN';
resp := TChilkatHttpResponse.Create(Self);
body := TChilkatJsonObject.Create(Self);
body.UpdateString('status[0]','SENT');
body.UpdateString('status[1]','PARTIALLY_PAID');
body.UpdateString('status[2]','UNPAID');
body.UpdateString('status[3]','SHARED');
//  Yesterday is the last overdue date. Include invoices due in the past 90 days.
day := TCkDateTime.Create(Self);
day.SetFromCurrentSystemTime();
day.AddDays(-1);
body.UpdateString('due_date_range.end',day.GetAsIso8601('YYYY-MM-DD',0));
day.AddDays(-89);
body.UpdateString('due_date_range.start',day.GetAsIso8601('YYYY-MM-DD',0));
page := 1;
lastPage := 1;
while (page <= lastPage) do
  begin
    url := TChilkatStringBuilder.Create(Self);
    url.Append('https://api-m.sandbox.paypal.com/v2/invoicing/search-invoices?total_required=true&page_size=50&page=');
    url.AppendInt(page);
    success := http.HttpJson('POST',url.GetAsString(),body.ControlInterface,'application/json',resp.ControlInterface);
    if (success <> 1) then
      begin
        Memo1.Lines.Add('No HTTP response: ' + http.LastErrorText);
        Exit;
      end;
    if ((resp.StatusCode < 200) or (resp.StatusCode >= 300)) then
      begin
        errorInfo := TChilkatJsonObject.Create(Self);
        errorInfo.Load(resp.BodyStr);
        Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ' ' + errorInfo.StringOf('name') + ' ' + errorInfo.StringOf('details[0].issue'));
        Memo1.Lines.Add('PayPal debug ID: ' + errorInfo.StringOf('debug_id'));
        Exit;
      end;
    result := TChilkatJsonObject.Create(Self);
    parsedResult := result.Load(resp.BodyStr);
    if (parsedResult <> 1) then
      begin
        Memo1.Lines.Add('Expected a JSON response; HTTP ' + IntToStr(resp.StatusCode));
        Exit;
      end;
    i := 0;
    count := result.SizeOfArray('items');
    while (i < count) do
      begin
        result.I := i;
        Memo1.Lines.Add(result.StringOf('items[i].detail.invoice_number') + '  due ' + result.StringOf('items[i].detail.payment_term.due_date') + '  ' + result.StringOf('items[i].due_amount.value')
             + ' ' + result.StringOf('items[i].due_amount.currency_code'));
        i := i + 1;
      end;

    lastPage := result.IntOf('total_pages');
    page := page + 1;
  end;